Skip to content

Commit e9f34f6

Browse files
committed
fix: avoid replaceState on pagehide if scrollRestoration is manual
Fix #2631
1 parent 062fa6d commit e9f34f6

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

‎packages/router/src/history/html5.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ function useHistoryListeners(
131131

132132
function beforeUnloadListener() {
133133
const { history } = window
134-
if (!history.state) return
134+
// calling replaceState() during a pagehide can break the bfcache
135+
if (!history.state || history.scrollRestoration === 'auto') return
135136
history.replaceState(
136137
assign({}, history.state, {
137138
scroll: computeScrollPosition(),
@@ -149,6 +150,7 @@ function useHistoryListeners(
149150

150151
// set up the listeners and prepare teardown callbacks
151152
window.addEventListener('popstate', popStateHandler)
153+
// TODO: remove the scroll computation in v6 since it's doen in the ScrollRestoration plugin
152154
// https://developer.chrome.com/blog/page-lifecycle-api/
153155
// note: iOS safari does not fire beforeunload, so we use pagehide instead
154156
window.addEventListener('pagehide', beforeUnloadListener)

0 commit comments

Comments
 (0)