From c166822cdc6cadd9f3c01085b877c86d74362b95 Mon Sep 17 00:00:00 2001 From: hworld Date: Wed, 11 Oct 2017 13:14:47 -0600 Subject: [PATCH] fix: fix scroll when going back to initial route (#1586) * fix scroll when going back to initial route * Update scroll.js --- src/util/scroll.js | 2 ++ test/e2e/specs/scroll-behavior.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/util/scroll.js b/src/util/scroll.js index 18b62ee18..b50af05eb 100644 --- a/src/util/scroll.js +++ b/src/util/scroll.js @@ -7,6 +7,8 @@ import { getStateKey, setStateKey } from './push-state' const positionStore = Object.create(null) export function setupScroll () { + // Fix for #1585 for Firefox + window.history.replaceState({ key: getStateKey() }, '') window.addEventListener('popstate', e => { saveScrollPosition() if (e.state && e.state.key) { diff --git a/test/e2e/specs/scroll-behavior.js b/test/e2e/specs/scroll-behavior.js index 7075f681b..9a816c265 100644 --- a/test/e2e/specs/scroll-behavior.js +++ b/test/e2e/specs/scroll-behavior.js @@ -20,6 +20,26 @@ module.exports = { return window.pageYOffset === 100 }, null, 'restore scroll position on back') + // with manual scroll restoration + // https://developers.google.com/web/updates/2015/09/history-api-scroll-restoration + .execute(function () { + window.scrollTo(0, 100) + history.scrollRestoration = 'manual' + }) + .click('li:nth-child(2) a') + .assert.containsText('.view', 'foo') + .execute(function () { + window.scrollTo(0, 200) + window.history.back() + }) + .assert.containsText('.view', 'home') + .assert.evaluate(function () { + return window.pageYOffset === 100 + }, null, 'restore scroll position on back with manual restoration') + .execute(function () { + history.scrollRestoration = 'auto' + }) + // scroll on a popped entry .execute(function () { window.scrollTo(0, 50)