-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
routeChangeComplete event called on page load for routes with params #11639
Comments
@timneutkens I can probably take a look into it myself if I can get pointed in the right direction. |
Same problem here. next.js/packages/next/client/index.js Line 103 in a0c6832
Additional condition was probably firstly introduced here 45832e4 I don't know what hydrateProps.__N_SSG means, probably Not Static Site Generation?
|
I've tried local development and static export and in both cases data.nextExport was false and __N_SSG was true. |
Similar problem #15082 |
Maybe someone from core team can explain what was intended here. |
Hi, check it out this discussion #12306 about avoid double analytics views on page load |
However this is a big problem when you must trigger something on change routing only: I have this need and at the moment I can't find a workaround to skip the first "bad" |
@valse that's great, but it is a workaround. It would be great to understand reasons behind the code and to fix root problem. |
Use useRouter over Router and add a workaround for the issue #11639
Still exists in next v10.2.3 |
This issue exists on |
Have you considered listening to See #11639 (comment) Outdated Solution useEffect(() => {
if (router.isReady) {
track()
}
const onRouteChangeComplete = (url, { shallow }) => {
track()
}
router.events.on('routeChangeComplete', onRouteChangeComplete)
return () => {
router.events.off('routeChangeComplete', onRouteChangeComplete)
}
}, []) |
This comment has been minimized.
This comment has been minimized.
@tpreusse Thanks, that worked for me! |
Adding my recent experience here as well. After seeing wildly inaccurate "page view" events in our event tracking system I determined it was because of this same issue. Completely agnostic of query params, To echo what's already been said here, the primary issue here is that, based on code and documentation, I still can't tell what's "expected" here vs. what may be a bug in the router. I guess my expectation would be that |
Is there any news on this? |
Is there any news on this issue? And could I not just use |
@yousefcisco watchout Next v12.2 changed the behaviour again: #37593 — in our case with middleware and incremental ssg pages we no longer got New solution: let lastTrackedPath
const track = path => {
// avoid double tracking
// - in case next router becomes ready and triggers a routeChangeComplete
if (path === lastTrackedPath) {
return
}
lastTrackedPath = path
// call to analytics api
}
const Track = () => {
useEffect(() => {
if (router.isReady) {
track(router.asPath)
}
}, [router.isReady])
useEffect(() => {
const onRouteChangeComplete = (asPath) => {
track(asPath)
}
router.events.on('routeChangeComplete', onRouteChangeComplete)
return () => {
router.events.off('routeChangeComplete', onRouteChangeComplete)
}
}, [])
return null
}
// render <Track /> in _app.js @shuding maybe it's a bug that |
Describe the bug
When ASO is enabled and you navigate to pages with params the
routeChangeComplete
is called. On page load:router.query
is{}
on first load, but apparently that's expected.routeChangeComplete
is called.This is a problem because
routeChangeComplete
isn't called when loading pages that don't have parameters. This inconsistency makes things difficult.In my case I'm trying to call analytics events on route change and page load. The page view event is called on page load (using an effect) but then the
routeChangeComplete
is immediately called which fires the page view again.To Reproduce
Reproduction example here
Steps to reproduce:
/
and see thatroute change
is NOT logged/?id=1
. Reload the page and noticeroute change
is called/posts/1
and see thatroute change
is logged on page loadExpected behavior
I would expect either:
(I'd also not expect the query object to be empty, but that's probably a separate issue)
Screenshots
N/A
System information
Additional context
N/A
The text was updated successfully, but these errors were encountered: