frontarm / navi

🧭 Declarative, asynchronous routing for React.
https://frontarm.com/navi/
MIT License
2.07k stars 71 forks source link

navigation.subscribe callback called twice #187

Open kmkr opened 4 years ago

kmkr commented 4 years ago

We use the navigation.subscribe method to log page views to Google Analytics. The callback we pass to the subscribe method runs twice every time a user clicks an internal link on the page, and we end up sending double page views. Any idea why that happens ?

Here's how we use it, slightly simplified:

// ...
const navigation = createBrowserNavigation({
  context: rootNaviContext,
  url: window.location.href,
  routes,
});
hydrate(
  <Router navigation={navigation}>
    <Web>
      <Body />
    </Web>
  </Router>,
  document.getElementById('root'),
  () => {
    logGaPageView(route.url.href); // logs initial page view
    navigation.subscribe(route => {
       logGaPageView(route.url.href); // logs subsequent page views twice!
    });
  },
);