Router going back to default page on tab switch

Hi everyone,

In my app, I use NavLink links to navigate to different routes in my application. The NavLinks are set in a Layout called GlobalLayout. The default route is routes.dashboard(), and I have a number of other routes such as routes.about() and routes.settings().

When I click a NavLink I’m sent to the correct page. However, if I switch tabs in my browser and then come back to the tab that has the app open, it seems the app reverts back to the default route (routes.dashboard()). I’m wondering if anyone can help / might it be a caching issue of sorts?

Thank you!

1 Like

Hey @charliem! This does sound a little strange. I wouldn’t necessarily expect the route reseting to be the default behaviour.

I tried to quickly reproduce this but I wasn’t able to. I created a new test-project, ran yarn rw build and then yarn rw serve. I navigate away from the homepage and switched tabs back and forth. I didn’t observe any resetting.

It might help if you could provide some more information that might help us reproduce this. Some information that might be useful off the top of my head could be:

  • What does your routes.tsx file look like?
  • Is this reset happening between routes which are private/authenticated, or not, or a combination?
  • Is this yarn rw serve or yarn rw dev specific?

If you could catch this behaviour on a quick screen recording that might also be helpful in confirming what’s happening. I’ll let other people have an opportunity to jump in too as they might have experience with something like this before.

Also if you feel like this is a true bug in redwood feel free to open an issue on the github repository here. We’d definitely need some more reproduction information in that case otherwise we wouldn’t be able to make progress towards a fix.

Hi @Josh-Walker-GM,

Thanks for responding! I was actually able to get to the bottom of this with a bit of help from @dthyresson on Discord. For anyone stumbling upon this in the future - I had a useEffect listening to onAuthStateChange that was redirecting to the homepage, and this was causing a redirect every time I switched tabs. Removed that and it’s all working as expected now.

1 Like