-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix and document client lifecycle events (onRouteUpdate...) #3399
Comments
I would like to work on this issue. Can you guide me @slorber about this. |
Hi Unfortunately I don't have more Infos. This requires reading the source code, understanding what it does exactly, and documenting the exposed api |
@slorber I like to take this up. |
@slorber can you help me with the file locations where to start |
onRouteChange is a navigation hook that would help you do run something when a route change. |
🥺 Thankyou @saadpasta for giving me the chance to contribute.😊 Yes I noted it. I will get back here itself, if I need any help . |
@saadpasta thanks for helping @imabp :) You can look for "onRouteUpdate" in the project's source code to find the relevant entry points to document :) However this is a v2 feature, so this must be documented in the v2 website ;) (not in the v1 navigation guide) I'd rather see it there: http://localhost:3000/docs/docusaurus-core |
Note: we should rather wait for documenting these things.
Until we have a better implementation, we should probably keep this undocumented. |
we can do this by putting But finally its actually, whenever we create a new route, ga analytics are set up for that route, so that whenever user clicks on that page, the analytics data is sent to the Google Analytics. right @slorber ? |
@imabp yes, but maybe we should wait for the page to render before sending the analytics.
Just tried that and it didn't work, as comparing prevProps.location to this.props.location always returned true, due to the hacky code in shouldComponentUpdate, so it never fired. Looks like a deeper refactor is needed. |
Yaa we need to deep dive into this. May I know, where you looked this ? |
Note, events are inspired from Gatsby (that also has a |
Wondering if anyone has figured out a workaround for this? We've just implemented a docs site with Docusaurus and we're at the point where we want to integrate our Segment analytics and noticed this bug. From what we can tell it seems like anyone currently using Docusaurus v2 and any of the analytics plugins are getting incorrect tracking data. |
For now I use a 0ms timeout, that worked fine, at least when I tested it on my machine 🤷 |
It feels to me like the naming of |
I can't really agree with that, this is quite subjective
It is definitively possible to SSR routes with a predefined set of query strings, I've done this in the past. We don't provide Docusaurus guides for that and it's probably not widely used, but it is technically possible. Note that some apis like IMHO Does it make sense? |
To me it makes sense :) |
Workaround for facebook/docusaurus#3399 Closes ory-corp/general#482
Workaround for facebook/docusaurus#3399 Closes ory-corp/general#482
I'm trying to use this with a different analytics provider, but it never gets triggered. I'm a bit lost. Frontend code is not my area of expertise :/
Any help would be greatly appreciated. |
sorry @slorber for the late reply... it makes sense. @matkoch could it be that you are missing the loading of the client side part? For the config part we're using And in the client part we have |
@matkoch The |
Workaround for facebook/docusaurus#3399 Closes ory-corp/general#482
Workaround for facebook/docusaurus#3399 Closes ory-corp/general#482
Workaround for facebook/docusaurus#3399 Closes ory-corp/general#482
Workaround for facebook/docusaurus#3399 Closes ory-corp/general#482
Workaround for facebook/docusaurus#3399 Closes ory-corp/general#482
For anyone interested in tracking page views in Docusaurus, we have solved it like this (using fathom analytics): in import React, { useEffect, useRef } from 'react';
import { useHistory } from '@docusaurus/router';
import * as Fathom from 'fathom-client';
export default function Root({ children }) {
const history = useHistory();
const pathname = useRef(history?.location?.pathname);
useEffect(() => {
Fathom.load('YOUR-FATHOM-CODE');
return history.listen((location) => {
if (location.pathname !== pathname.current) {
Fathom.trackPageview({ url: `https://your-domain.com${location.pathname}` });
}
pathname.current = location.pathname;
});
}, []);
return (
<div>
{children}
</div>
);
} Please note that we are passing the url of the next page to the |
So I've been implementing some analytics as well and have been using your GA plugin as a help (onRouteUpdate etc) to create my own. Now I am kinda stuck on retrieving the current version and language. Are there any hooks available for these or should I just infer them from the location? |
The client modules can't access hooks because they do not render React components If you want to track dynamic things with a hook, there's no other choice than to add a component in the tree (for example swizzle the layout) to put a hook here afaik. Now you can also access static things in the client modules with: import siteConfig from '@generated/docusaurus.config';
import globalData from '@generated/globalData';
import i18n from '@generated/i18n'; Those are not documented but it's quite stable so it's probably not too unsafe to be considered as public API. It's the same data you get when you use Regarding the data you want to access with hooks:
|
Cheers @slorber, exactly what I was looking for |
We did several changes to client module lifecycles in #6732 Read the doc at https://docusaurus.io/docs/advanced/client#client-module-lifecycles TL.DR:import type {ClientModule} from '@docusaurus/types';
const module: ClientModule = {
onRouteUpdate({location, previousLocation}) {
// this fires just after the user link click`
return () => {
// this fires when all the JS bundles for the page transition are ready,
// just before React renders the next page
}
},
onRouteDidUpdate({location, previousLocation}) {
// this fires after React renders and can access to updated DOM
},
};
export default module; Breaking changes:
Note: both If you implement things like analytics and those analytics send pageview events on script load, it's your responsibility to filter the first call to avoid a duplicate pageview analytics event. On first event, Note: those lifecycles fire for any location change IE these will also fire for changes such as hash, querystring, history state... It is also your responsibility to filter calls according to your need. Example analytics plugin implementation that:
import type {ClientModule} from '@docusaurus/types';
const clientModule: ClientModule = {
onRouteDidUpdate({location, previousLocation}) {
if (previousLocation && location.pathname !== previousLocation.pathname) {
emitAnalyticsEvent('pageview', {
page_title: document.title,
page_location: window.location.href,
page_path: location.pathname,
});
}
},
};
export default clientModule; |
Hi @slorber! I noticed that even I use You can read the source code here and browse online at https://town.korandoru.io/. const module: ClientModule = {
onRouteDidUpdate({location, previousLocation}) {
if (ExecutionEnvironment.canUseDOM) {
if (location.pathname != previousLocation?.pathname) {
_paq.push(['setCustomUrl', location.pathname])
_paq.push(['setDocumentTitle', document.title])
_paq.push(['trackPageView'])
console.log(`path = ${location.pathname}`)
console.log(`title = ${document.title}`)
}
}
}
} Looking forward to your feedback >_< |
Resolved as korandoru/town@eb510df. This seems the same as #7424 that can be common. |
Thanks @tisonkun, can you please open a dedicated issue so that we can investigate? Not 100% sure but I believe this is due to an undocumented behavior of Using Edit: actually created the issue: #8278 |
📚 Documentation
We have client lifecycle events that allow to hook into the navigation, but it's not documented.
#1591
Example usage in the google analytics plugin:
The text was updated successfully, but these errors were encountered: