Skip to content

Strip basename from location provided to getKey #10550

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

Merged
merged 8 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/strip-basename-getkey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix `basename` not being stripped from the location provided to `<ScrollRestoration getKey>`
31 changes: 31 additions & 0 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6959,6 +6959,37 @@ describe("a router", () => {
expect(t.router.state.preventScrollReset).toBe(false);
});

it("strips the basename from the location provided to getKey", async () => {
let t = setup({
routes: SCROLL_ROUTES,
basename: "/base",
initialEntries: ["/base"],
hydrationData: {
loaderData: {
index: "INDEX_DATA",
},
},
});

let positions = { "/tasks": 100 };
let activeScrollPosition = 0;
let pathname;
t.router.enableScrollRestoration(
positions,
() => activeScrollPosition,
(l) => {
pathname = l.pathname;
return l.pathname;
}
);

let nav1 = await t.navigate("/base/tasks");
await nav1.loaders.tasks.resolve("TASKS");
expect(pathname).toBe("/tasks");
expect(t.router.state.restoreScrollPosition).toBe(100);
expect(t.router.state.preventScrollReset).toBe(false);
});

it("restores scroll on GET submissions", async () => {
let t = setup({
routes: SCROLL_ROUTES,
Expand Down
31 changes: 20 additions & 11 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2430,7 +2430,7 @@ export function createRouter(init: RouterInit): Router {
) {
savedScrollPositions = positions;
getScrollPosition = getPosition;
getScrollRestorationKey = getKey || ((location) => location.key);
getScrollRestorationKey = getKey || null;

// Perform initial hydration scroll restoration, since we miss the boat on
// the initial updateState() because we've not yet rendered <ScrollRestoration/>
Expand All @@ -2450,15 +2450,27 @@ export function createRouter(init: RouterInit): Router {
};
}

function getScrollKey(location: Location, matches: AgnosticDataRouteMatch[]) {
if (getScrollRestorationKey) {
let loc = {
...location,
pathname:
stripBasename(location.pathname, basename) || location.pathname,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: see about moving this to the RR layer since that's where the basename stripping should happen to stay consistent

};
let userMatches = matches.map((m) =>
createUseMatchesMatch(m, state.loaderData)
);
return getScrollRestorationKey(loc, userMatches) || location.key;
}
return location.key;
}

function saveScrollPosition(
location: Location,
matches: AgnosticDataRouteMatch[]
): void {
if (savedScrollPositions && getScrollRestorationKey && getScrollPosition) {
let userMatches = matches.map((m) =>
createUseMatchesMatch(m, state.loaderData)
);
let key = getScrollRestorationKey(location, userMatches) || location.key;
if (savedScrollPositions && getScrollPosition) {
let key = getScrollKey(location, matches);
savedScrollPositions[key] = getScrollPosition();
}
}
Expand All @@ -2467,11 +2479,8 @@ export function createRouter(init: RouterInit): Router {
location: Location,
matches: AgnosticDataRouteMatch[]
): number | null {
if (savedScrollPositions && getScrollRestorationKey && getScrollPosition) {
let userMatches = matches.map((m) =>
createUseMatchesMatch(m, state.loaderData)
);
let key = getScrollRestorationKey(location, userMatches) || location.key;
if (savedScrollPositions) {
let key = getScrollKey(location, matches);
let y = savedScrollPositions[key];
if (typeof y === "number") {
return y;
Expand Down