Skip to content

Commit

Permalink
Ensure revalidations happen when hash is present (#10516)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed May 23, 2023
1 parent 06f712f commit b45a5bd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
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

0 comments on commit b45a5bd

Please sign in to comment.