-
-
Notifications
You must be signed in to change notification settings - Fork 163
fix: update url after route change #1459
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
fix: update url after route change #1459
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
Part of the fix for #1437. It turns out that #1437 is reporting multiple issues: 1. Showing preview element content instead of a transition when navigating to a previously visited page. That issue is replicated with the e2e tests in this PR and fixed. 2. Updating the url before changing the route. I'm expecting the url to not update until the new page has loaded. I opened #1459. 3. The changeRoute function completes before the page component has streamed any data. The third issue is hard to test without using unstable_events. --------- Co-authored-by: daishi <[email protected]>
4102547
to
9791f07
Compare
Why does 9791f07 cause e2e tests like "broken-links: static server > redirect" to fail? Is there some behavior that depends on the history update? I commented out the new e2e tests for scroll position and url updating so we can focus on that issue. Then maybe we can figure out how to fix the refetch promise in changeRoute and re-enable these e2e tests. |
@rmarscher Please merge main. |
I think I got a little closer by using So with the broken-links example, running it with a dev server, click on "Correct redirect" and the url history will go to /exists and then /redirect. It would be nice to know that a redirect changed it somehow and skip updating. I added RSC redirects in 4265f05 to be able to test it with a dev server. The RSC redirect was only in the public/serve.json. |
If things around redirect handling seem too hard to implement or too complicated to maintain, I think our idea should be to reduce the requirement. For example, my understanding around client redirect was not enough when we added the support for redirect config in serve.json, which is technically outside the framework and which depends on rsc path convention. Maybe, it's time to revisit. (and, should do before v1-alpha.) |
@rmarscher What's the current status? |
@dai-shi I think all of the tests are passing except one. Router events seem to be firing twice on the first navigation after loading a dynamic page. They do not do that on subsequent page loads or if the static page is loaded first. cc/ @tylersayshi e2e/use-router.spec.ts:119:5 › useRouter › calls route change event handlers › on dynamic pages
Error: expect(received).toEqual(expected) // deep equality
- Expected - 0
+ Received + 2
Array [
"Route change started",
+ "Route change started",
+ "Route change completed",
"Route change completed",
] |
I found that this is because of the "locationListeners". After the RSC fetch returns, it is looking at the routeData and then running locationListeners, if necessary. I think this is to support server-side redirects or just an extra check to make sure that the window.location and current page elements are in sync? Before this PR, the location was updated before this code was hit so the check to see if the location update passed. The one labeled - "FIXME this check here seems ad-hoc" ;-) Now it's failing so we'll need another way to determine if we are already in the middle of a route change. |
It's looking good now @dai-shi @tylersayshi. Maybe just a flaky windows test failure. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rmarscher Thanks for working on this! It looks pretty clean. Just left a few comments.
@tylersayshi Would you like to give a review with another eye?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rest lgtm 👍
const redirects = { | ||
'/redirect': '/exists', | ||
'/RSC/R/redirect.txt': '/RSC/R/exists.txt', | ||
'/broken-redirect': '/broken', | ||
'/RSC/R/broken-redirect.txt': '/RSC/R/broken.txt', | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be declared outside the function scope? or does it not really matter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷 Yeah, probably. technically the /RSC/R
prefix and .txt
extensions should probably be imported from waku constants or use a function that converts a normal path to an RSC path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dai-shi what do you think about exposing encodeRscPath()
for this?
@@ -482,6 +491,18 @@ const Redirect = ({ to, reset }: { to: string; reset: () => void }) => { | |||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious: Is the setTimeout hack still needed with the new route change behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried and there are e2e tests that break if I remove it.
packages/waku/src/router/client.ts
Outdated
try { | ||
await changeRoute(parseRoute(url), { | ||
skipRefetch: true, | ||
shouldScroll: false, | ||
}); | ||
} finally { | ||
if (path !== '/404') { | ||
window.history.pushState( | ||
{ | ||
...window.history.state, | ||
waku_new_path: url.pathname !== window.location.pathname, | ||
}, | ||
'', | ||
url, | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use .finally
here like the other use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I should have not switched it to an async def. I'll fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh... it's so the error can fall through. I think we need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rmarscher Can you clarify again when we need .finally {}
and when not?
packages/waku/src/router/client.ts
Outdated
unstable_startTransition: startTransitionFn, | ||
}); | ||
} catch (err) { | ||
console.error('Error while navigating to new route:', err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have console.error here too.
Now that It is not necessarily needed for the The The
I think that covers all of the uses of changeRoute in waku/router/client. |
commit: |
Hmm... the locationListeners use |
OK. I think I have addressed all of the comments. Thanks for the reviews. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on it. It's a little bit unfortunate to see things become complicated, but can't be helped. Any ideas to fix/mitigate later are welcome.
Maybe updating history should move into changeRoute.
The history option could be I'm not sure about |
Maybe we don't need it in the future. One motivation was to support view transition api, but it will be supported by React natively. |
For #1437.