From f2931b5ff10d39131d00fbc8dd978ee56149be85 Mon Sep 17 00:00:00 2001 From: Benjamin GROENEVELD Date: Sat, 13 Oct 2018 15:09:39 +0200 Subject: [PATCH] Fix hash link reliability (fix #434) --- templates/src/client/start/index.ts | 5 +++++ test/apps/scroll/src/routes/tall-page.html | 1 + test/apps/scroll/test.ts | 17 +++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/templates/src/client/start/index.ts b/templates/src/client/start/index.ts index 17979a3b4..78beb4b40 100644 --- a/templates/src/client/start/index.ts +++ b/templates/src/client/start/index.ts @@ -83,6 +83,11 @@ function handle_click(event: MouseEvent) { const svg = typeof a.href === 'object' && a.href.constructor.name === 'SVGAnimatedString'; const href = String(svg ? (a).href.baseVal : a.href); + // If we are clicking on the same hash we still want to scroll to it anyway. + if (location.hash && href === location.href) { + return; + } + if (href === location.href) { event.preventDefault(); return; diff --git a/test/apps/scroll/src/routes/tall-page.html b/test/apps/scroll/src/routes/tall-page.html index 7e42ff94e..3a92a984b 100644 --- a/test/apps/scroll/src/routes/tall-page.html +++ b/test/apps/scroll/src/routes/tall-page.html @@ -1,3 +1,4 @@ +scroll to foo
diff --git a/test/apps/scroll/test.ts b/test/apps/scroll/test.ts index 0c0db39b0..cf400b22c 100644 --- a/test/apps/scroll/test.ts +++ b/test/apps/scroll/test.ts @@ -33,6 +33,23 @@ describe('scroll', function() { assert.ok(scrollY > 0, scrollY); }); + it('scrolls to any deeplink if it was already active', async () => { + await page.goto(`${base}/tall-page#foo`); + await start(); + + let scrollY = await page.evaluate(() => window.scrollY); + assert.ok(scrollY > 0, scrollY); + + await page.evaluate(() => window.scrollTo(0, 0)); + scrollY = await page.evaluate(() => window.scrollY); + assert.ok(scrollY === 0, scrollY); + + await page.click('[href="tall-page#foo"]'); + await wait(50); + scrollY = await page.evaluate(() => window.scrollY); + assert.ok(scrollY > 0, scrollY); + }); + it('resets scroll when a link is clicked', async () => { await page.goto(`${base}/tall-page#foo`); await start();