router: `beforeLoad` does not wait for async functions to resolve

Describe the bug

I have the same setup as the Authenticated Routes examples, but with firebase authentication. I have my auth in context, with a login and logout function. I have an _auth.tsx file in the routes folder. Then a _auth folder containing all protected routes.

When the auth context is set in localStorage, same as the example, routing happens as expected. However when wanting to set the user from the result of the signIn function from firebase the beforeLoad does not wait for the result and the context in the _auth file remains unchanged.

Your Example Website or App

Steps to Reproduce the Bug or Issue

With the same setup as the Authenticated Routes example, the only change required would be to have something like firebase authentication and then changing setStoredUser(username) to

const loggedInUser = await signIn(email, password);
setStoredUser(loggedInUser)

Expected behavior

The beforeLoad should allow for resolved async values being set into context

Screenshots or Videos

Screenshot 2024-05-03 at 12 06 25 PM

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: closed
  • Created 3 years ago
  • Reactions: 18
  • Comments: 19 (10 by maintainers)

Most upvoted comments

It is worth checking this out in the loader as well since #1551 has a similar issue where if the loader was awaited on correctly, then the correct component would have been rendered.

I believe we have the same issue. Awaiting promises with Tanstack Query (ensureQueryData) does not resolve.

CleanShot 2024-05-07 at 16 05 07@2x

CleanShot 2024-05-07 at 16 06 15@2x

With the “sleep” you put into the example, the router invalidation and navigation are put behind the react updates (due to the setTimeout), right? To make this work, the “sleep” must be placed after the call to auth.login and before navigate.

You are more-or-less on the right track. React concurrent mode is also at play here, so the way it handles the batching of state updates can also come into play.

I don’t really use these kinds of auth flows in my apps, rather relying on oauth2 flows instead. So this is just a simple auth flow that I came up with a few months back just to demonstrate the router concepts.

Don’t look to copy the auth flow in the example. Rather just look at the concepts behind the usage of the Router’s context, throws in the beforeLoad, and the usage of router.invalidate, and then adapt it to whatever your app needs are.

The issue here is entirely related to the trailingSlash issue in combination with React.StrictMode.

For example, consider the scenario where trailingSlash is set to never with React.StrictMode ON.

  1. When visiting /posts/ the URL then gets written to /posts.
  2. However, a loader which instantly resolves, finishes before the beforeLoad call.
  3. A white-screen is then shown.

Also, when trailingSlash is set to always with React.StrictMode ON.

  1. When visiting /posts the URL then gets written to /posts/.
  2. However, a loader which instantly resolves, finishes before the beforeLoad call.
  3. A white-screen is then shown.

I’ve tested these behaviour here: https://stackblitz.com/edit/github-h31abz-hjnco9?file=src%2Fmain.tsx


📢 This doesn’t happen when the trailingSlash behaviour is set to preserve.

🧱 The default trailingSlash behaviour is never, so the beforeLoad not being awaited is what users thought was happening.


This is the created issue: #1591

Looked further into this. The beforeLoad not being awaited has NOT been fixed.

But a new bug has been found, as mentioned by @frederikvonsperling.

The new bug found by @frederikvonsperling involves a combination of the trailingSlash setting and React.StrictMode.

I’ll open a new issue for this new bug shortly.

Edit: This has been fixed.

I have the exact same issue for the exact same case. Would love a good answer there ❤️