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

Commit a68c62c

Browse files
committed
Fix hash link reliability (fix #434)
1 parent abcac75 commit a68c62c

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

templates/src/client/start/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ function handle_click(event: MouseEvent) {
8383
const svg = typeof a.href === 'object' && a.href.constructor.name === 'SVGAnimatedString';
8484
const href = String(svg ? (<SVGAElement>a).href.baseVal : a.href);
8585

86+
if (location.hash && href === location.href) {
87+
return;
88+
}
89+
8690
if (href === location.href) {
8791
event.preventDefault();
8892
return;

test/apps/scroll/src/routes/tall-page.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<a href="tall-page#foo">scroll to foo</a>
12
<div style="height: 9999px"></div>
23

34
<div id="foo">

test/apps/scroll/test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ describe('scroll', function() {
3333
assert.ok(scrollY > 0, scrollY);
3434
});
3535

36+
it('scrolls to any deeplink if it was already active', async () => {
37+
await page.goto(`${base}/tall-page#foo`);
38+
await start();
39+
40+
let scrollY = await page.evaluate(() => window.scrollY);
41+
assert.ok(scrollY > 0, scrollY);
42+
43+
scrollY = await page.evaluate(() => {
44+
window.scrollTo(0, 0)
45+
return window.scrollY
46+
});
47+
assert.ok(scrollY === 0, scrollY);
48+
49+
await page.click('[href="tall-page#foo"]');
50+
scrollY = await page.evaluate(() => window.scrollY);
51+
assert.ok(scrollY > 0, scrollY);
52+
});
53+
3654
it('resets scroll when a link is clicked', async () => {
3755
await page.goto(`${base}/tall-page#foo`);
3856
await start();

0 commit comments

Comments
 (0)