New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@redwoodjs/history #2212
@redwoodjs/history #2212
Conversation
@KrisCoulson I'm not going to have time to look at this for a while. Do you want to pick it up instead? |
0eba0b0
to
2ced364
Compare
Hi @Tobbe , Can we make a separate PR for the redwood/packages/router/src/history.tsx Line 28 in 2ced364
|
93d3af0
to
0b6612d
Compare
Hi @sangheestyle thanks for showing interest in this PR 🙂 I will spend some more time on this, and then discuss with the rest of the team about the best way forward. |
0b6612d
to
4409be1
Compare
Awesome! Thanks for the reply! |
ab153a0
to
c1488a6
Compare
@sangheestyle We decided to focus on getting this PR done as soon as possible instead of spending time on a separate PR for |
ce9af26
to
98d0329
Compare
98d0329
to
ee269c4
Compare
@jtoar confirming there are no breaking changes from this PR, correct? Also, is there anything we should do for documentation? Assuming "no". |
@thedavidprice no breaking changes. But ideally this PR will be superseded by #3055 before our next release (also no breaking changes). We could add to the router documentation for the new |
The Problem
Some auth providers want to redirect the user when authorizing. They do this by a standard
window.history.replaceState
call (orglobal.history.replaceState
). The address in the browser's address bar is updated, but the user isn't actually redirected.The Cause
A standard RW app might look something like this
https://github.com/redwoodjs/redwood-tutorial/blob/3b59bc12ca211688839b395b8d1364a4c111db26/web/src/App.js#L15-L23
Where
<Routes>
uses this code from the routerredwood/packages/router/src/router.tsx
Lines 213 to 225 in 4abbe45
A regular call to
navigate
looks like thishttps://github.com/redwoodjs/redwood-tutorial/blob/2941c967adb6d7251ce3ae6e34edc3161e3fe89c/web/src/components/Post/Post.js#L36
It will call this code where the address is updated (
history.pushState
) and all listeners are notified about the route changeredwood/packages/router/src/history.tsx
Lines 13 to 18 in 4abbe45
The listeners are located in
<LocationProvider>
. You can see that provider being used in the snippet from the router above. The provider's internal state is updated, and all its children are re-rendered. The router, being one of the children, sees the updated address and renders the new page ("posts" in this case).This, below, is the problem
The direct call to just
history.replaceState
by the auth client bypasses the<LocationProvider>
update, and so no re-rendering of the router happens.The Solution
The solution might sound obvious. Just call the location listeners, like with a regular
navigate()
call.We can't do this however, because the auth stuff is higher up in the hierarchy and so can't use the location context with its listeners.
My proposal
I propose that we introduce a new package, "history", that's responsible for keeping track of history, history changes and history change notification. This would be placed above auth in the hierarchy so auth can update the history (by calling
history.replace
), and the LocationProvider can keep listening to changes to history, and re-render its children when it changes.Closes #1892
Adding some more context from #1892
This is what a typical App.js file looks like with auth0 added
When signing up you do something like this
When
<AuthProvider>
from theApp
snippet above is mounted it callsrestoreAuthState
, which in turn does thisThe problem is with the
history.replace
call. The router doesn't know about this (doesn't get any notifications). So the correct page isn't rendered. In fact, no rerender happens at all. So the user just stays on the current page. But the browser knows, so the URL is updated.Here's a Github repo with a project that displays the issue https://github.com/tobbe/tobbe-redwood-history
Using the code in this PR the issue goes away. There's some info in the README in the repo on how to set it up.