Skip to content

Commit b45a5bd

Browse files
authored
Ensure revalidations happen when hash is present (#10516)
1 parent 06f712f commit b45a5bd

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

.changeset/revalidate-with-hash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Ensure revalidations happen when hash is present

packages/router/__tests__/router-test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7456,6 +7456,49 @@ describe("a router", () => {
74567456
expect(t.history.replace).not.toHaveBeenCalled();
74577457
});
74587458

7459+
it("handles revalidation when a hash is present", async () => {
7460+
let t = setup({
7461+
routes: TASK_ROUTES,
7462+
initialEntries: ["/#hash"],
7463+
hydrationData: {
7464+
loaderData: {
7465+
root: "ROOT_DATA",
7466+
index: "INDEX_DATA",
7467+
},
7468+
},
7469+
});
7470+
7471+
let key = t.router.state.location.key;
7472+
let R = await t.revalidate();
7473+
expect(t.router.state).toMatchObject({
7474+
historyAction: "POP",
7475+
location: { pathname: "/" },
7476+
navigation: IDLE_NAVIGATION,
7477+
revalidation: "loading",
7478+
loaderData: {
7479+
root: "ROOT_DATA",
7480+
index: "INDEX_DATA",
7481+
},
7482+
});
7483+
7484+
await R.loaders.root.resolve("ROOT_DATA*");
7485+
await R.loaders.index.resolve("INDEX_DATA*");
7486+
expect(t.router.state).toMatchObject({
7487+
historyAction: "POP",
7488+
location: { pathname: "/" },
7489+
navigation: IDLE_NAVIGATION,
7490+
revalidation: "idle",
7491+
loaderData: {
7492+
root: "ROOT_DATA*",
7493+
index: "INDEX_DATA*",
7494+
},
7495+
});
7496+
expect(t.router.state.location.hash).toBe('#hash');
7497+
expect(t.router.state.location.key).toBe(key);
7498+
expect(t.history.push).not.toHaveBeenCalled();
7499+
expect(t.history.replace).not.toHaveBeenCalled();
7500+
});
7501+
74597502
it("handles revalidation interrupted by a <Link> navigation", async () => {
74607503
let t = setup({
74617504
routes: TASK_ROUTES,

packages/router/router.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,13 +1225,15 @@ export function createRouter(init: RouterInit): Router {
12251225
return;
12261226
}
12271227

1228-
// Short circuit if it's only a hash change and not a mutation submission.
1228+
// Short circuit if it's only a hash change and not a revalidation or
1229+
// mutation submission.
1230+
//
12291231
// Ignore on initial page loads because since the initial load will always
1230-
// be "same hash".
1231-
// For example, on /page#hash and submit a <Form method="post"> which will
1232-
// default to a navigation to /page
1232+
// be "same hash". For example, on /page#hash and submit a <Form method="post">
1233+
// which will default to a navigation to /page
12331234
if (
12341235
state.initialized &&
1236+
!isRevalidationRequired &&
12351237
isHashChangeOnly(state.location, location) &&
12361238
!(opts && opts.submission && isMutationMethod(opts.submission.formMethod))
12371239
) {

0 commit comments

Comments
 (0)