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
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)
It is worth checking this out in the
loaderas 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.
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 ofrouter.invalidate, and then adapt it to whatever your app needs are.The issue here is entirely related to the
trailingSlashissue in combination withReact.StrictMode.For example, consider the scenario where
trailingSlashis set toneverwithReact.StrictModeON./posts/the URL then gets written to/posts.Also, when
trailingSlashis set toalwayswithReact.StrictModeON./poststhe URL then gets written to/posts/.I’ve tested these behaviour here: https://stackblitz.com/edit/github-h31abz-hjnco9?file=src%2Fmain.tsx
📢 This doesn’t happen when the
trailingSlashbehaviour is set topreserve.🧱 The default
trailingSlashbehaviour isnever, so thebeforeLoadnot being awaited is what users thought was happening.This is the created issue: #1591
Looked further into this. The
beforeLoadnot being awaited hasNOTbeen fixed.But a new bug has been found, as mentioned by @frederikvonsperling.
The new bug found by @frederikvonsperling involves a combination of the
trailingSlashsetting andReact.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 ❤️