@tanstack/router-core 1.171.2 → 1.171.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +0 -3
- package/dist/cjs/index.d.cts +2 -3
- package/dist/cjs/rewrite.cjs +1 -6
- package/dist/cjs/rewrite.cjs.map +1 -1
- package/dist/cjs/rewrite.d.cts +0 -4
- package/dist/cjs/router.cjs +9 -2
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +5 -1
- package/dist/cjs/scroll-restoration-inline.cjs +1 -1
- package/dist/cjs/scroll-restoration-inline.cjs.map +1 -1
- package/dist/cjs/scroll-restoration-inline.d.cts +0 -2
- package/dist/cjs/scroll-restoration-script/server.cjs +2 -6
- package/dist/cjs/scroll-restoration-script/server.cjs.map +1 -1
- package/dist/cjs/scroll-restoration.cjs +113 -107
- package/dist/cjs/scroll-restoration.cjs.map +1 -1
- package/dist/cjs/scroll-restoration.d.cts +0 -10
- package/dist/cjs/ssr/ssr-server.cjs +4 -3
- package/dist/cjs/ssr/ssr-server.cjs.map +1 -1
- package/dist/esm/index.d.ts +2 -3
- package/dist/esm/index.js +2 -3
- package/dist/esm/rewrite.d.ts +0 -4
- package/dist/esm/rewrite.js +1 -6
- package/dist/esm/rewrite.js.map +1 -1
- package/dist/esm/router.d.ts +5 -1
- package/dist/esm/router.js +9 -3
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/scroll-restoration-inline.d.ts +0 -2
- package/dist/esm/scroll-restoration-inline.js +1 -1
- package/dist/esm/scroll-restoration-inline.js.map +1 -1
- package/dist/esm/scroll-restoration-script/server.js +2 -6
- package/dist/esm/scroll-restoration-script/server.js.map +1 -1
- package/dist/esm/scroll-restoration.d.ts +0 -10
- package/dist/esm/scroll-restoration.js +114 -107
- package/dist/esm/scroll-restoration.js.map +1 -1
- package/dist/esm/ssr/ssr-server.js +4 -3
- package/dist/esm/ssr/ssr-server.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -3
- package/src/rewrite.ts +1 -8
- package/src/router.ts +28 -4
- package/src/scroll-restoration-inline.ts +7 -16
- package/src/scroll-restoration-script/server.ts +1 -10
- package/src/scroll-restoration.ts +188 -167
- package/src/ssr/ssr-server.ts +12 -8
- package/dist/cjs/hash-scroll.cjs +0 -20
- package/dist/cjs/hash-scroll.cjs.map +0 -1
- package/dist/cjs/hash-scroll.d.cts +0 -7
- package/dist/esm/hash-scroll.d.ts +0 -7
- package/dist/esm/hash-scroll.js +0 -20
- package/dist/esm/hash-scroll.js.map +0 -1
- package/src/hash-scroll.ts +0 -21
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -185,6 +185,7 @@ export type {
|
|
|
185
185
|
RouteContextFn,
|
|
186
186
|
ContextOptions,
|
|
187
187
|
RouteContextOptions,
|
|
188
|
+
SsrContextOptions,
|
|
188
189
|
BeforeLoadContextOptions,
|
|
189
190
|
RootRouteOptions,
|
|
190
191
|
RootRouteOptionsExtensions,
|
|
@@ -406,12 +407,9 @@ export {
|
|
|
406
407
|
defaultGetScrollRestorationKey,
|
|
407
408
|
getElementScrollRestorationEntry,
|
|
408
409
|
storageKey,
|
|
409
|
-
scrollRestorationCache,
|
|
410
410
|
setupScrollRestoration,
|
|
411
411
|
} from './scroll-restoration'
|
|
412
412
|
|
|
413
|
-
export { handleHashScroll } from './hash-scroll'
|
|
414
|
-
|
|
415
413
|
export type {
|
|
416
414
|
ScrollRestorationOptions,
|
|
417
415
|
ScrollRestorationEntry,
|
package/src/rewrite.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { joinPaths, trimPath } from './path'
|
|
2
2
|
import type { LocationRewrite } from './router'
|
|
3
3
|
|
|
4
|
-
/** Compose multiple rewrite pairs into a single in/out rewrite. */
|
|
5
4
|
/** Compose multiple rewrite pairs into a single in/out rewrite. */
|
|
6
5
|
export function composeRewrites(rewrites: Array<LocationRewrite>) {
|
|
7
6
|
return {
|
|
@@ -20,7 +19,6 @@ export function composeRewrites(rewrites: Array<LocationRewrite>) {
|
|
|
20
19
|
} satisfies LocationRewrite
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
/** Create a rewrite pair that strips/adds a basepath on input/output. */
|
|
24
22
|
/** Create a rewrite pair that strips/adds a basepath on input/output. */
|
|
25
23
|
export function rewriteBasepath(opts: {
|
|
26
24
|
basepath: string
|
|
@@ -28,13 +26,10 @@ export function rewriteBasepath(opts: {
|
|
|
28
26
|
}) {
|
|
29
27
|
const trimmedBasepath = trimPath(opts.basepath)
|
|
30
28
|
const normalizedBasepath = `/${trimmedBasepath}`
|
|
31
|
-
const normalizedBasepathWithSlash = `${normalizedBasepath}/`
|
|
32
29
|
const checkBasepath = opts.caseSensitive
|
|
33
30
|
? normalizedBasepath
|
|
34
31
|
: normalizedBasepath.toLowerCase()
|
|
35
|
-
const checkBasepathWithSlash =
|
|
36
|
-
? normalizedBasepathWithSlash
|
|
37
|
-
: normalizedBasepathWithSlash.toLowerCase()
|
|
32
|
+
const checkBasepathWithSlash = `${checkBasepath}/`
|
|
38
33
|
|
|
39
34
|
return {
|
|
40
35
|
input: ({ url }) => {
|
|
@@ -58,7 +53,6 @@ export function rewriteBasepath(opts: {
|
|
|
58
53
|
} satisfies LocationRewrite
|
|
59
54
|
}
|
|
60
55
|
|
|
61
|
-
/** Execute a location input rewrite if provided. */
|
|
62
56
|
/** Execute a location input rewrite if provided. */
|
|
63
57
|
export function executeRewriteInput(
|
|
64
58
|
rewrite: LocationRewrite | undefined,
|
|
@@ -75,7 +69,6 @@ export function executeRewriteInput(
|
|
|
75
69
|
return url
|
|
76
70
|
}
|
|
77
71
|
|
|
78
|
-
/** Execute a location output rewrite if provided. */
|
|
79
72
|
/** Execute a location output rewrite if provided. */
|
|
80
73
|
export function executeRewriteOutput(
|
|
81
74
|
rewrite: LocationRewrite | undefined,
|
package/src/router.ts
CHANGED
|
@@ -52,6 +52,7 @@ import type {
|
|
|
52
52
|
import type { SearchParser, SearchSerializer } from './searchParams'
|
|
53
53
|
import type { AnyRedirect, ResolvedRedirect } from './redirect'
|
|
54
54
|
import type {
|
|
55
|
+
HistoryAction,
|
|
55
56
|
HistoryLocation,
|
|
56
57
|
HistoryState,
|
|
57
58
|
ParsedHistoryState,
|
|
@@ -740,7 +741,10 @@ export type GetMatchRoutesFn = (pathname: string) => {
|
|
|
740
741
|
|
|
741
742
|
export type EmitFn = (routerEvent: RouterEvent) => void
|
|
742
743
|
|
|
743
|
-
export type LoadFn = (opts?: {
|
|
744
|
+
export type LoadFn = (opts?: {
|
|
745
|
+
sync?: boolean
|
|
746
|
+
action?: { type: HistoryAction }
|
|
747
|
+
}) => Promise<void>
|
|
744
748
|
|
|
745
749
|
export type CommitLocationFn = ({
|
|
746
750
|
viewTransition,
|
|
@@ -883,6 +887,11 @@ export function getLocationChangeInfo(
|
|
|
883
887
|
return { fromLocation, toLocation, pathChanged, hrefChanged, hashChanged }
|
|
884
888
|
}
|
|
885
889
|
|
|
890
|
+
export const locationHistoryActions = new WeakMap<
|
|
891
|
+
ParsedLocation,
|
|
892
|
+
HistoryAction
|
|
893
|
+
>()
|
|
894
|
+
|
|
886
895
|
export type CreateRouterFn = <
|
|
887
896
|
TRouteTree extends AnyRoute,
|
|
888
897
|
TTrailingSlashOption extends TrailingSlashOption = 'never',
|
|
@@ -2116,6 +2125,7 @@ export class RouterCore<
|
|
|
2116
2125
|
ignoreBlocker,
|
|
2117
2126
|
...next
|
|
2118
2127
|
}) => {
|
|
2128
|
+
let historyAction: HistoryAction | undefined
|
|
2119
2129
|
const isSameState = () => {
|
|
2120
2130
|
// the following props are ignored but may still be provided when navigating,
|
|
2121
2131
|
// temporarily add the previous values to the next state so they don't affect
|
|
@@ -2191,7 +2201,9 @@ export class RouterCore<
|
|
|
2191
2201
|
|
|
2192
2202
|
this.shouldViewTransition = viewTransition
|
|
2193
2203
|
|
|
2194
|
-
|
|
2204
|
+
historyAction = next.replace ? 'REPLACE' : 'PUSH'
|
|
2205
|
+
|
|
2206
|
+
this.history[historyAction === 'REPLACE' ? 'replace' : 'push'](
|
|
2195
2207
|
nextHistory.publicHref,
|
|
2196
2208
|
nextHistory.state,
|
|
2197
2209
|
{ ignoreBlocker },
|
|
@@ -2201,7 +2213,13 @@ export class RouterCore<
|
|
|
2201
2213
|
this.resetNextScroll = next.resetScroll ?? true
|
|
2202
2214
|
|
|
2203
2215
|
if (!this.history.subscribers.size) {
|
|
2204
|
-
this.load(
|
|
2216
|
+
this.load(
|
|
2217
|
+
historyAction
|
|
2218
|
+
? {
|
|
2219
|
+
action: { type: historyAction },
|
|
2220
|
+
}
|
|
2221
|
+
: undefined,
|
|
2222
|
+
)
|
|
2205
2223
|
}
|
|
2206
2224
|
|
|
2207
2225
|
return this.commitLocationPromise
|
|
@@ -2403,7 +2421,8 @@ export class RouterCore<
|
|
|
2403
2421
|
})
|
|
2404
2422
|
}
|
|
2405
2423
|
|
|
2406
|
-
load: LoadFn = async (opts
|
|
2424
|
+
load: LoadFn = async (opts): Promise<void> => {
|
|
2425
|
+
const historyAction = opts?.action?.type
|
|
2407
2426
|
let redirect: AnyRedirect | undefined
|
|
2408
2427
|
let notFound: NotFoundError | undefined
|
|
2409
2428
|
let loadPromise: Promise<void>
|
|
@@ -2415,6 +2434,11 @@ export class RouterCore<
|
|
|
2415
2434
|
this.startTransition(async () => {
|
|
2416
2435
|
try {
|
|
2417
2436
|
this.beforeLoad()
|
|
2437
|
+
if (historyAction) {
|
|
2438
|
+
locationHistoryActions.set(this.latestLocation, historyAction)
|
|
2439
|
+
} else {
|
|
2440
|
+
locationHistoryActions.delete(this.latestLocation)
|
|
2441
|
+
}
|
|
2418
2442
|
const next = this.latestLocation
|
|
2419
2443
|
const prevLocation = this.stores.resolvedLocation.get()
|
|
2420
2444
|
const locationChangeInfo = getLocationChangeInfo(next, prevLocation)
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
export default function (options: {
|
|
2
|
-
storageKey: string
|
|
3
|
-
key?: string
|
|
4
|
-
behavior?: ScrollToOptions['behavior']
|
|
5
|
-
shouldScrollRestoration?: boolean
|
|
6
|
-
}) {
|
|
1
|
+
export default function (options: { storageKey: string; key?: string }) {
|
|
7
2
|
let byKey
|
|
8
3
|
|
|
9
4
|
try {
|
|
@@ -15,13 +10,9 @@ export default function (options: {
|
|
|
15
10
|
|
|
16
11
|
const resolvedKey = options.key || window.history.state?.__TSR_key
|
|
17
12
|
const elementEntries = resolvedKey ? byKey[resolvedKey] : undefined
|
|
13
|
+
let windowRestored = false
|
|
18
14
|
|
|
19
|
-
if (
|
|
20
|
-
options.shouldScrollRestoration &&
|
|
21
|
-
elementEntries &&
|
|
22
|
-
typeof elementEntries === 'object' &&
|
|
23
|
-
Object.keys(elementEntries).length > 0
|
|
24
|
-
) {
|
|
15
|
+
if (elementEntries && typeof elementEntries === 'object') {
|
|
25
16
|
for (const elementSelector in elementEntries) {
|
|
26
17
|
const entry = elementEntries[elementSelector]
|
|
27
18
|
|
|
@@ -40,8 +31,8 @@ export default function (options: {
|
|
|
40
31
|
window.scrollTo({
|
|
41
32
|
top: scrollY,
|
|
42
33
|
left: scrollX,
|
|
43
|
-
behavior: options.behavior,
|
|
44
34
|
})
|
|
35
|
+
windowRestored = true
|
|
45
36
|
} else if (elementSelector) {
|
|
46
37
|
let element
|
|
47
38
|
|
|
@@ -57,10 +48,10 @@ export default function (options: {
|
|
|
57
48
|
}
|
|
58
49
|
}
|
|
59
50
|
}
|
|
60
|
-
|
|
61
|
-
return
|
|
62
51
|
}
|
|
63
52
|
|
|
53
|
+
if (windowRestored) return
|
|
54
|
+
|
|
64
55
|
const hash = window.location.hash.split('#', 2)[1]
|
|
65
56
|
|
|
66
57
|
if (hash) {
|
|
@@ -77,5 +68,5 @@ export default function (options: {
|
|
|
77
68
|
return
|
|
78
69
|
}
|
|
79
70
|
|
|
80
|
-
window.scrollTo({ top: 0, left: 0
|
|
71
|
+
window.scrollTo({ top: 0, left: 0 })
|
|
81
72
|
}
|
|
@@ -9,26 +9,18 @@ import type { AnyRouter } from '../router'
|
|
|
9
9
|
type InlineScrollRestorationScriptOptions = {
|
|
10
10
|
storageKey: string
|
|
11
11
|
key?: string
|
|
12
|
-
behavior?: ScrollToOptions['behavior']
|
|
13
|
-
shouldScrollRestoration?: boolean
|
|
14
12
|
}
|
|
15
13
|
|
|
16
14
|
const defaultInlineScrollRestorationScript = `(${minifiedScrollRestorationScript})(${escapeHtml(
|
|
17
15
|
JSON.stringify({
|
|
18
16
|
storageKey,
|
|
19
|
-
shouldScrollRestoration: true,
|
|
20
17
|
} satisfies InlineScrollRestorationScriptOptions),
|
|
21
18
|
)})`
|
|
22
19
|
|
|
23
20
|
function getScrollRestorationScript(
|
|
24
21
|
options: InlineScrollRestorationScriptOptions,
|
|
25
22
|
) {
|
|
26
|
-
if (
|
|
27
|
-
options.storageKey === storageKey &&
|
|
28
|
-
options.shouldScrollRestoration === true &&
|
|
29
|
-
options.key === undefined &&
|
|
30
|
-
options.behavior === undefined
|
|
31
|
-
) {
|
|
23
|
+
if (options.storageKey === storageKey && options.key === undefined) {
|
|
32
24
|
return defaultInlineScrollRestorationScript
|
|
33
25
|
}
|
|
34
26
|
|
|
@@ -58,7 +50,6 @@ export function getScrollRestorationScriptForRouter(router: AnyRouter) {
|
|
|
58
50
|
|
|
59
51
|
return getScrollRestorationScript({
|
|
60
52
|
storageKey,
|
|
61
|
-
shouldScrollRestoration: true,
|
|
62
53
|
key: userKey,
|
|
63
54
|
})
|
|
64
55
|
}
|