router: Router context invalidate Promise resolve does not invalidate context

Describe the bug

In the code below, which was suppose to invalidate the context and navigate to the following route. for context i’m following the example given in tanstack authenticated routes example. Even after navigation the context isn’t invalidated which I can see in console log. It results in redirect to login page not working properly after clicking logout and just stuck in the “/” page.

router.invalidate().finally(() => { navigate({ to: "/" }); });

It only invalidates or I can see it invalidates when I manually reload the page, which means during the navigation the context doesn’t invalidate.

PS: I do have an api request in the logout function.

Your Example Website or App

…

Steps to Reproduce the Bug or Issue

Following the tanstack example locally.

https://stackblitz.com/edit/tanstack-router-4achrw?file=src%2Froutes%2F_auth.tsx

Expected behavior

Router context should’ve been already invalid in .finally or .then even before navigating.

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 19
  • Comments: 15 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I’m currently using the flushSync solution as a workaround following suggestion by SzelamC at https://github.com/TanStack/router/discussions/1668#discussioncomment-9726727

Got same issue here

“@tanstack/react-router”: “^1.95.3”, “react”: “^19.0.0”, “react-dom”: “^19.0.0”,

update guys, the current hack seems to use sleep(1000) as explained here : https://github.com/TanStack/router/discussions/1668

// This is just a hack being used to wait for the auth state to update
// in a real app, you'd want to use a more robust solution
      await sleep(1)

This trick works on my side, but this is a serious issue with Context.

PS: the sleep function is quite obvious :


export async function sleep(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}