return teardown
}
+ function beforeUnloadListener() {
+ const { history } = window
+ if (!history.state) return
+ history.replaceState(
+ {
+ ...history.state,
+ scroll: computeScrollPosition(),
+ },
+ ''
+ )
+ }
+
function destroy() {
for (const teardown of teardowns) teardown()
teardowns = []
window.removeEventListener('popstate', popStateHandler)
+ window.removeEventListener('beforeunload', beforeUnloadListener)
}
- // settup the listener and prepare teardown callbacks
+ // settup the listeners and prepare teardown callbacks
window.addEventListener('popstate', popStateHandler)
+ window.addEventListener('beforeunload', beforeUnloadListener)
return {
pauseListeners,
}
export default function createHistory(base: string = ''): RouterHistory {
- if ('scrollRestoration' in window.history) {
- history.scrollRestoration = 'manual'
- }
-
const historyNavigation = useHistoryStateNavigation(base)
const historyListeners = useHistoryListeners(
base,
isReady(): Promise<void>
}
+const isClient = typeof window !== 'undefined'
+
export function createRouter({
history,
routes,
let errorHandlers: ErrorHandler[] = []
let ready: boolean = false
+ if (isClient && 'scrollRestoration' in window.history) {
+ window.history.scrollRestoration = 'manual'
+ }
+
function resolve(
to: RouteLocation,
currentLocation?: RouteLocationNormalized /*, append?: boolean */
)
}
- // TODO: should we allow partial redirects? I think we should because it's impredictable if
+ // TODO: should we allow partial redirects? I think we should not because it's impredictable if
// there was a redirect before
// if (!('path' in newLocation) && !('name' in newLocation)) throw new Error('TODO: redirect canot be relative')
const from = currentRoute.value
currentRoute.value = markNonReactive(toLocation)
+ // TODO: this doesn't work on first load. Moving it to RouterView could allow automatically handling transitions too maybe
if (!isFirstNavigation)
handleScroll(toLocation, from).catch(err => triggerError(err, false))