Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-router/.changes/patch.hash-history-mask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix hash history not restoring the router location of a masked entry on Back/Forward
66 changes: 65 additions & 1 deletion packages/react-router/__tests__/router/hash-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable jest/expect-expect */

import type { HashHistory } from "../../lib/router/history";
import type { HashHistory, Location } from "../../lib/router/history";
import { createHashHistory } from "../../lib/router/history";

import Listen from "./TestSequences/Listen";
Expand Down Expand Up @@ -166,4 +166,68 @@ describe("a hash history", () => {
await GoForward(history);
});
});

describe("masked locations", () => {
let gallery: Location = {
pathname: "/gallery",
search: "?photo=123",
hash: "#photo",
state: { source: "gallery" },
key: "gallery",
mask: {
pathname: "/images/123",
search: "?size=large",
hash: "#preview",
},
};

it("shows the mask in the URL and keeps the router location on push and replace", () => {
history.push(gallery);
expect(testWindow.location.hash).toEqual(
"#/images/123?size=large#preview",
);
expect(history.location).toEqual(gallery);

let nextGallery: Location = {
...gallery,
search: "?photo=456",
key: "next-gallery",
mask: { pathname: "/images/456", search: "", hash: "" },
};
history.replace(nextGallery);
expect(testWindow.location.hash).toEqual("#/images/456");
expect(history.location).toEqual(nextGallery);
});

it("restores masked and unmasked locations on POP", async () => {
history.push(gallery);
history.push("/other");
let other = history.location;

// JSDom doesn't fire the listener synchronously
let back = new Promise<Location>((resolve) => {
let unlisten = history.listen(({ location }) => {
unlisten();
resolve(location);
});
});
history.go(-1);
expect(await back).toEqual(gallery);
expect(history.location).toEqual(gallery);
expect(testWindow.location.hash).toEqual(
"#/images/123?size=large#preview",
);

let forward = new Promise<Location>((resolve) => {
let unlisten = history.listen(({ location }) => {
unlisten();
resolve(location);
});
});
history.go(1);
expect(await forward).toEqual(other);
expect(history.location.mask).toBeUndefined();
expect(testWindow.location.hash).toEqual("#/other");
});
});
});
4 changes: 3 additions & 1 deletion packages/react-router/lib/router/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,14 @@ export function createHashHistory(
pathname = "/" + pathname;
}

let maskedLocation = (globalHistory.state as HistoryState)?.masked;
return createLocation(
"",
{ pathname, search, hash },
maskedLocation || { pathname, search, hash },
// state defaults to `null` because `window.history.state` does
(globalHistory.state && globalHistory.state.usr) || null,
(globalHistory.state && globalHistory.state.key) || "default",
maskedLocation ? { pathname, search, hash } : undefined,
);
}

Expand Down