Skip to content

Commit

Permalink
fix(vue-app): use browser to handle scrolling position on page reload…
Browse files Browse the repository at this point in the history
… and back-navigation from other sites (#5080)
  • Loading branch information
ahus1 authored and pooya parsa committed Feb 26, 2019
1 parent e4f1d59 commit 7001312
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/vue-app/template/router.js
Expand Up @@ -86,7 +86,21 @@ Vue.use(Router)
const scrollBehavior = <%= serializeFunction(router.scrollBehavior) %>
<% } else { %>
if (process.client) {
window.history.scrollRestoration = 'manual'
if ('scrollRestoration' in window.history) {
window.history.scrollRestoration = 'manual'

// reset scrollRestoration to auto when leaving page, allowing page reload
// and back-navigation from other pages to use the browser to restore the
// scrolling position.
window.addEventListener('beforeunload', () => {
window.history.scrollRestoration = 'auto'
})

// Setting scrollRestoration to manual again when returning to this page.
window.addEventListener('load', () => {
window.history.scrollRestoration = 'manual'
})
}
}
const scrollBehavior = function (to, from, savedPosition) {
// if the returned position is falsy or an empty object,
Expand Down

0 comments on commit 7001312

Please sign in to comment.