routeChangeComplete event called on page load for routes with params
See original GitHub issueDescribe 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
Steps to reproduce:
- Start the server
- Go to
/
and see thatroute change
is NOT logged - Add any query param, e.g.
/?id=1
. Reload the page and noticeroute change
is called - Go to
/posts/1
and see thatroute change
is logged on page load - Add getInitialProps to _app and this doesn’t happen any more
Expected behavior
I would expect either:
- Route change is always called on page load while ASO is enabled.
- Route change is not called on page load for pages with params
(I’d also not expect the query object to be empty, but that’s probably a separate issue)
Screenshots
N/A
System information
- OS: macOS
- Browser (if applies) Chrome
- Version of Next.js: 9.3.4
Additional context
N/A
Issue Analytics
- State:
- Created 3 years ago
- Reactions:18
- Comments:17 (6 by maintainers)
Top Results From Across the Web
Understanding Next.js routeChangeStart and router events
For example, the event routeChangeStart fires up when a route starts to change; in other words, when a user navigates to a new...
Read more >reactjs - How to access the url or page that is currently loading ...
You can access the path in the routeChangeStart handler function as it receives the URL of the route you're navigating to when fired...
Read more >next/router | Next.js
query : Object - The query string parsed to an object, including dynamic route parameters. It will be an empty object during prerendering...
Read more >Helpers - Routify
Absolute, relative or named path. params. type: {Object} (default value: {} ) ... You can also call the function $url() on any page...
Read more >Next.js - Redirect to Login Page if Unauthenticated
js app component. Client-side authorization is implemented in the authCheck() function which is executed on initial app load and on each route ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Have you considered listening to
router.isReady
?See https://github.com/vercel/next.js/issues/11639#issuecomment-1219654377
Outdated Solution
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,
routeChangeComplete
was firing on the initial page load for all of our SSG pages but not for our SSR pages. I too have now determined the aboverouter.isReady
to be the best workaround.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
routeChange<anything>
wouldn’t fire at all on initial page load for any type of page, but rather only once user interaction or my own code has triggered a history change.