Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Fix deeplinks no matter previous scroll position #1139

Merged
merged 1 commit into from
May 4, 2020
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
2 changes: 1 addition & 1 deletion runtime/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export async function navigate(target: Target, id: number, noscroll?: boolean, h
if (deep_linked) {
scroll = {
x: 0,
y: deep_linked.getBoundingClientRect().top
y: deep_linked.getBoundingClientRect().top + scrollY
};
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/apps/scroll/src/routes/a-third-tall-page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>A third tall page</h1>

<a href="tall-page#foo" id="top">link</a>
<div style="height: 9999px"></div>
<a href="tall-page#foo" id="bottom">link</a>
20 changes: 20 additions & 0 deletions test/apps/scroll/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ describe('scroll', function() {
assert.ok(scrollY > 0);
});

it('scrolls to a deeplink on a new page no matter the previous scroll position', async () => {
await r.load('/a-third-tall-page#top');
await r.sapper.start();
await r.sapper.prefetchRoutes();

await r.page.click('a#top');
await r.wait();
const firstScrollY = await r.page.evaluate(() => window.scrollY);

await r.load('/a-third-tall-page#bottom');
await r.sapper.start();
await r.sapper.prefetchRoutes();

await r.page.click('a#bottom');
await r.wait();
const secondScrollY = await r.page.evaluate(() => window.scrollY);

assert.equal(firstScrollY, secondScrollY);
});

it('survives the tests with no server errors', () => {
assert.deepEqual(r.errors, []);
});
Expand Down