Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Skipped
65 lines
1.7 KiB
Plaintext
65 lines
1.7 KiB
Plaintext
import minifiedScrollRestorationScript from '../scroll-restoration-inline?script-string'
|
|
import {
|
|
defaultGetScrollRestorationKey,
|
|
storageKey,
|
|
} from '../scroll-restoration'
|
|
import { escapeHtml } from '../utils'
|
|
import type { AnyRouter } from '../router'
|
|
|
|
type InlineScrollRestorationScriptOptions = {
|
|
storageKey: string
|
|
key?: string
|
|
behavior?: ScrollToOptions['behavior']
|
|
shouldScrollRestoration?: boolean
|
|
}
|
|
|
|
const defaultInlineScrollRestorationScript = `(${minifiedScrollRestorationScript})(${escapeHtml(
|
|
JSON.stringify({
|
|
storageKey,
|
|
shouldScrollRestoration: true,
|
|
} satisfies InlineScrollRestorationScriptOptions),
|
|
)})`
|
|
|
|
function getScrollRestorationScript(
|
|
options: InlineScrollRestorationScriptOptions,
|
|
) {
|
|
if (
|
|
options.storageKey === storageKey &&
|
|
options.shouldScrollRestoration === true &&
|
|
options.key === undefined &&
|
|
options.behavior === undefined
|
|
) {
|
|
return defaultInlineScrollRestorationScript
|
|
}
|
|
|
|
return `(${minifiedScrollRestorationScript})(${escapeHtml(JSON.stringify(options))})`
|
|
}
|
|
|
|
export function getScrollRestorationScriptForRouter(router: AnyRouter) {
|
|
if (
|
|
typeof router.options.scrollRestoration === 'function' &&
|
|
!router.options.scrollRestoration({ location: router.latestLocation })
|
|
) {
|
|
return null
|
|
}
|
|
|
|
const getKey = router.options.getScrollRestorationKey
|
|
if (!getKey) {
|
|
return defaultInlineScrollRestorationScript
|
|
}
|
|
|
|
const location = router.latestLocation
|
|
const userKey = getKey(location)
|
|
const defaultKey = defaultGetScrollRestorationKey(location)
|
|
|
|
if (userKey === defaultKey) {
|
|
return defaultInlineScrollRestorationScript
|
|
}
|
|
|
|
return getScrollRestorationScript({
|
|
storageKey,
|
|
shouldScrollRestoration: true,
|
|
key: userKey,
|
|
})
|
|
}
|