question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

routeChangeComplete event called on page load for routes with params

See original GitHub issue

Describe the bug

When ASO is enabled and you navigate to pages with params the routeChangeComplete is called. On page load:

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:

  1. Start the server
  2. Go to / and see that route change is NOT logged
  3. Add any query param, e.g. /?id=1. Reload the page and notice route change is called
  4. Go to /posts/1 and see that route change is logged on page load
  5. Add getInitialProps to _app and this doesn’t happen any more

Expected behavior

I would expect either:

  1. Route change is always called on page load while ASO is enabled.
  2. 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:open
  • Created 3 years ago
  • Reactions:18
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
tpreussecommented, Aug 18, 2022

Have you considered listening to router.isReady?

See https://github.com/vercel/next.js/issues/11639#issuecomment-1219654377

Outdated Solution
  useEffect(() => {
    if (router.isReady) {
      track()
    }

    const onRouteChangeComplete = (url, { shallow }) => {
      track()
    }
    router.events.on('routeChangeComplete', onRouteChangeComplete)
    return () => {
      router.events.off('routeChangeComplete', onRouteChangeComplete)
    }
  }, [])
7reactions
barberdtcommented, Nov 10, 2021

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 above router.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found