11import { isServer } from '@tanstack/router-core/isServer'
2- import minifiedScrollRestorationScript from './scroll-restoration-inline?script-string'
3- import { escapeHtml , functionalUpdate , isPlainObject } from './utils'
2+ import { functionalUpdate , isPlainObject } from './utils'
43import type { AnyRouter } from './router'
54import type { ParsedLocation } from './location'
65import type { NonNullableUpdater } from './utils'
@@ -22,13 +21,6 @@ export type ScrollRestorationOptions = {
2221 scrollBehavior ?: ScrollToOptions [ 'behavior' ]
2322}
2423
25- type InlineScrollRestorationScriptOptions = {
26- storageKey : string
27- key ?: string
28- behavior ?: ScrollToOptions [ 'behavior' ]
29- shouldScrollRestoration ?: boolean
30- }
31-
3224function getSafeSessionStorage ( ) {
3325 try {
3426 return typeof window !== 'undefined' &&
@@ -86,56 +78,6 @@ function createScrollRestorationCache(): ScrollRestorationCache | null {
8678
8779export const scrollRestorationCache = createScrollRestorationCache ( )
8880
89- const defaultInlineScrollRestorationScript = `(${ minifiedScrollRestorationScript } )(${ escapeHtml (
90- JSON . stringify ( {
91- storageKey,
92- shouldScrollRestoration : true ,
93- } satisfies InlineScrollRestorationScriptOptions ) ,
94- ) } )`
95-
96- export function getScrollRestorationScript (
97- options : InlineScrollRestorationScriptOptions ,
98- ) {
99- if (
100- options . storageKey === storageKey &&
101- options . shouldScrollRestoration === true &&
102- options . key === undefined &&
103- options . behavior === undefined
104- ) {
105- return defaultInlineScrollRestorationScript
106- }
107-
108- return `(${ minifiedScrollRestorationScript } )(${ escapeHtml ( JSON . stringify ( options ) ) } )`
109- }
110-
111- export function getScrollRestorationScriptForRouter ( router : AnyRouter ) {
112- if (
113- typeof router . options . scrollRestoration === 'function' &&
114- ! router . options . scrollRestoration ( { location : router . latestLocation } )
115- ) {
116- return null
117- }
118-
119- const getKey = router . options . getScrollRestorationKey
120- if ( ! getKey ) {
121- return defaultInlineScrollRestorationScript
122- }
123-
124- const location = router . latestLocation
125- const userKey = getKey ( location )
126- const defaultKey = defaultGetScrollRestorationKey ( location )
127-
128- if ( userKey === defaultKey ) {
129- return defaultInlineScrollRestorationScript
130- }
131-
132- return getScrollRestorationScript ( {
133- storageKey,
134- shouldScrollRestoration : true ,
135- key : userKey ,
136- } )
137- }
138-
13981/**
14082 * The default `getKey` function for `useScrollRestoration`.
14183 * It returns the `key` from the location state or the `href` of the location.
0 commit comments