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.
Steps to reproduce:
/ and see that route change is NOT logged/?id=1. Reload the page and notice route change is called/posts/1 and see that route change is logged on page loadI would expect either:
(I'd also not expect the query object to be empty, but that's probably a separate issue)
N/A
N/A
@timneutkens I can probably take a look into it myself if I can get pointed in the right direction.
Same problem here.
routeChangeComplete event is called because this evaluates to true https://github.com/vercel/next.js/blob/a0c683208abdce84934061f6efd2b90de52d99b9/packages/next/client/index.js#L103
Additional condition was probably firstly introduced here https://github.com/vercel/next.js/commit/45832e44219b281eab5569374f3b2cb2f651e755
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 https://github.com/vercel/next.js/issues/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" routeChangeComplete trigger :(
@valse that's great, but it is a workaround. It would be great to understand reasons behind the code and to fix root problem.
Most helpful comment
@valse that's great, but it is a workaround. It would be great to understand reasons behind the code and to fix root problem.