Skip to content
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

Ensure revalidations happen when hash is present #10516

Merged
merged 1 commit into from May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/revalidate-with-hash.md
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Ensure revalidations happen when hash is present
43 changes: 43 additions & 0 deletions packages/router/__tests__/router-test.ts
Expand Up @@ -7456,6 +7456,49 @@ describe("a router", () => {
expect(t.history.replace).not.toHaveBeenCalled();
});

it("handles revalidation when a hash is present", async () => {
let t = setup({
routes: TASK_ROUTES,
initialEntries: ["/#hash"],
hydrationData: {
loaderData: {
root: "ROOT_DATA",
index: "INDEX_DATA",
},
},
});

let key = t.router.state.location.key;
let R = await t.revalidate();
expect(t.router.state).toMatchObject({
historyAction: "POP",
location: { pathname: "/" },
navigation: IDLE_NAVIGATION,
revalidation: "loading",
loaderData: {
root: "ROOT_DATA",
index: "INDEX_DATA",
},
});

await R.loaders.root.resolve("ROOT_DATA*");
await R.loaders.index.resolve("INDEX_DATA*");
expect(t.router.state).toMatchObject({
historyAction: "POP",
location: { pathname: "/" },
navigation: IDLE_NAVIGATION,
revalidation: "idle",
loaderData: {
root: "ROOT_DATA*",
index: "INDEX_DATA*",
},
});
expect(t.router.state.location.hash).toBe('#hash');
expect(t.router.state.location.key).toBe(key);
expect(t.history.push).not.toHaveBeenCalled();
expect(t.history.replace).not.toHaveBeenCalled();
});

it("handles revalidation interrupted by a <Link> navigation", async () => {
let t = setup({
routes: TASK_ROUTES,
Expand Down
10 changes: 6 additions & 4 deletions packages/router/router.ts
Expand Up @@ -1225,13 +1225,15 @@ export function createRouter(init: RouterInit): Router {
return;
}

// Short circuit if it's only a hash change and not a mutation submission.
// Short circuit if it's only a hash change and not a revalidation or
// mutation submission.
//
// Ignore on initial page loads because since the initial load will always
// be "same hash".
// For example, on /page#hash and submit a <Form method="post"> which will
// default to a navigation to /page
// be "same hash". For example, on /page#hash and submit a <Form method="post">
// which will default to a navigation to /page
if (
state.initialized &&
!isRevalidationRequired &&
isHashChangeOnly(state.location, location) &&
!(opts && opts.submission && isMutationMethod(opts.submission.formMethod))
) {
Expand Down