fix(vue-app): scrollRestoration hasn't set (#8055)

This commit is contained in:
Dmitriy 2020-09-30 12:57:36 +03:00 committed by GitHub
parent 3aa02cf00c
commit 79440f3ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -2,27 +2,27 @@
<%= isTest ? '/* eslint-disable quotes, semi, indent, comma-spacing, key-spacing, object-curly-spacing, space-before-function-paren */' : '' %> <%= isTest ? '/* eslint-disable quotes, semi, indent, comma-spacing, key-spacing, object-curly-spacing, space-before-function-paren */' : '' %>
export default <%= serializeFunction(router.scrollBehavior) %> export default <%= serializeFunction(router.scrollBehavior) %>
<%= isTest ? '/* eslint-enable quotes, semi, indent, comma-spacing, key-spacing, object-curly-spacing, space-before-function-paren */' : '' %> <%= isTest ? '/* eslint-enable quotes, semi, indent, comma-spacing, key-spacing, object-curly-spacing, space-before-function-paren */' : '' %>
<% } else { %>import { getMatchedComponents } from './utils' <% } else { %>import { getMatchedComponents, setScrollRestoration } from './utils'
if (process.client) { if (process.client) {
if ('scrollRestoration' in window.history) { if ('scrollRestoration' in window.history) {
window.history.scrollRestoration = 'manual' setScrollRestoration('manual')
// reset scrollRestoration to auto when leaving page, allowing page reload // reset scrollRestoration to auto when leaving page, allowing page reload
// and back-navigation from other pages to use the browser to restore the // and back-navigation from other pages to use the browser to restore the
// scrolling position. // scrolling position.
window.addEventListener('beforeunload', () => { window.addEventListener('beforeunload', () => {
window.history.scrollRestoration = 'auto' setScrollRestoration('auto')
}) })
// Setting scrollRestoration to manual again when returning to this page. // Setting scrollRestoration to manual again when returning to this page.
window.addEventListener('load', () => { window.addEventListener('load', () => {
window.history.scrollRestoration = 'manual' setScrollRestoration('manual')
}) })
} }
} }
export default function (to, from, savedPosition) { export default function (to, from, savedPosition) {
// If the returned position is falsy or an empty object, will retain current scroll position // If the returned position is falsy or an empty object, will retain current scroll position
let position = false let position = false

View File

@ -663,3 +663,9 @@ export function stripTrailingSlash (path) {
export function isSamePath (p1, p2) { export function isSamePath (p1, p2) {
return stripTrailingSlash(p1) === stripTrailingSlash(p2) return stripTrailingSlash(p1) === stripTrailingSlash(p2)
} }
export function setScrollRestoration (newVal) {
try {
window.history.scrollRestoration = newVal;
} catch(e) {}
}