@tanstack/react-router 1.91.2 → 1.92.1
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/router.cjs +28 -17
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/scroll-restoration.cjs +5 -1
- package/dist/cjs/scroll-restoration.cjs.map +1 -1
- package/dist/cjs/scroll-restoration.d.cts +1 -0
- package/dist/esm/router.js +28 -17
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/scroll-restoration.d.ts +1 -0
- package/dist/esm/scroll-restoration.js +5 -1
- package/dist/esm/scroll-restoration.js.map +1 -1
- package/package.json +1 -1
- package/src/router.ts +34 -20
- package/src/scroll-restoration.tsx +6 -1
|
@@ -123,7 +123,11 @@ function useScrollRestoration(options) {
|
|
|
123
123
|
if (key === restoreKey) {
|
|
124
124
|
if (elementSelector === windowKey) {
|
|
125
125
|
windowRestored = true;
|
|
126
|
-
window.scrollTo(
|
|
126
|
+
window.scrollTo({
|
|
127
|
+
top: entry.scrollY,
|
|
128
|
+
left: entry.scrollX,
|
|
129
|
+
behavior: options == null ? void 0 : options.scrollBehavior
|
|
130
|
+
});
|
|
127
131
|
} else if (elementSelector) {
|
|
128
132
|
const element = document.querySelector(elementSelector);
|
|
129
133
|
if (element) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scroll-restoration.cjs","sources":["../../src/scroll-restoration.tsx"],"sourcesContent":["import * as React from 'react'\nimport { useRouter } from './useRouter'\nimport { functionalUpdate } from './utils'\nimport type { ParsedLocation } from './location'\nimport type { NonNullableUpdater } from './utils'\n\nconst useLayoutEffect =\n typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nconst windowKey = 'window'\nconst delimiter = '___'\n\nlet weakScrolledElements = new WeakSet<any>()\n\ntype CacheValue = Record<string, { scrollX: number; scrollY: number }>\ntype CacheState = {\n cached: CacheValue\n next: CacheValue\n}\n\ntype Cache = {\n state: CacheState\n set: (updater: NonNullableUpdater<CacheState>) => void\n}\n\nconst sessionsStorage = typeof window !== 'undefined' && window.sessionStorage\n\nconst cache: Cache = sessionsStorage\n ? (() => {\n const storageKey = 'tsr-scroll-restoration-v2'\n\n const state: CacheState = JSON.parse(\n window.sessionStorage.getItem(storageKey) || 'null',\n ) || { cached: {}, next: {} }\n\n return {\n state,\n set: (updater) => {\n cache.state = functionalUpdate(updater, cache.state)\n window.sessionStorage.setItem(storageKey, JSON.stringify(cache.state))\n },\n }\n })()\n : (undefined as any)\n\nexport type ScrollRestorationOptions = {\n getKey?: (location: ParsedLocation) => string\n}\n\n/**\n * The default `getKey` function for `useScrollRestoration`.\n * It returns the `key` from the location state or the `href` of the location.\n *\n * The `location.href` is used as a fallback to support the use case where the location state is not available like the initial render.\n */\nconst defaultGetKey = (location: ParsedLocation) => {\n return location.state.key! || location.href\n}\n\nexport function useScrollRestoration(options?: ScrollRestorationOptions) {\n const router = useRouter()\n\n useLayoutEffect(() => {\n const getKey = options?.getKey || defaultGetKey\n\n const { history } = window\n history.scrollRestoration = 'manual'\n\n const onScroll = (event: Event) => {\n if (weakScrolledElements.has(event.target)) return\n weakScrolledElements.add(event.target)\n\n let elementSelector = ''\n\n if (event.target === document || event.target === window) {\n elementSelector = windowKey\n } else {\n const attrId = (event.target as Element).getAttribute(\n 'data-scroll-restoration-id',\n )\n\n if (attrId) {\n elementSelector = `[data-scroll-restoration-id=\"${attrId}\"]`\n } else {\n elementSelector = getCssSelector(event.target)\n }\n }\n\n if (!cache.state.next[elementSelector]) {\n cache.set((c) => ({\n ...c,\n next: {\n ...c.next,\n [elementSelector]: {\n scrollX: NaN,\n scrollY: NaN,\n },\n },\n }))\n }\n }\n\n if (typeof document !== 'undefined') {\n document.addEventListener('scroll', onScroll, true)\n }\n\n const unsubOnBeforeLoad = router.subscribe('onBeforeLoad', (event) => {\n if (event.hrefChanged) {\n const restoreKey = getKey(event.fromLocation)\n for (const elementSelector in cache.state.next) {\n const entry = cache.state.next[elementSelector]!\n if (elementSelector === windowKey) {\n entry.scrollX = window.scrollX || 0\n entry.scrollY = window.scrollY || 0\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n entry.scrollX = element?.scrollLeft || 0\n entry.scrollY = element?.scrollTop || 0\n }\n\n cache.set((c) => {\n const next = { ...c.next }\n delete next[elementSelector]\n\n return {\n ...c,\n next,\n cached: {\n ...c.cached,\n [[restoreKey, elementSelector].join(delimiter)]: entry,\n },\n }\n })\n }\n }\n })\n\n const unsubOnBeforeRouteMount = router.subscribe(\n 'onBeforeRouteMount',\n (event) => {\n if (event.hrefChanged) {\n if (!router.resetNextScroll) {\n return\n }\n\n router.resetNextScroll = true\n\n const restoreKey = getKey(event.toLocation)\n let windowRestored = false\n\n for (const cacheKey in cache.state.cached) {\n const entry = cache.state.cached[cacheKey]!\n const [key, elementSelector] = cacheKey.split(delimiter)\n if (key === restoreKey) {\n if (elementSelector === windowKey) {\n windowRestored = true\n window.scrollTo(entry.scrollX, entry.scrollY)\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n element.scrollLeft = entry.scrollX\n element.scrollTop = entry.scrollY\n }\n }\n }\n }\n\n if (!windowRestored) {\n window.scrollTo(0, 0)\n }\n\n cache.set((c) => ({ ...c, next: {} }))\n weakScrolledElements = new WeakSet<any>()\n }\n },\n )\n\n return () => {\n document.removeEventListener('scroll', onScroll)\n unsubOnBeforeLoad()\n unsubOnBeforeRouteMount()\n }\n }, [options?.getKey, router])\n}\n\nexport function ScrollRestoration(props: ScrollRestorationOptions) {\n useScrollRestoration(props)\n return null\n}\n\nexport function useElementScrollRestoration(\n options: (\n | {\n id: string\n getElement?: () => Element | undefined | null\n }\n | {\n id?: string\n getElement: () => Element | undefined | null\n }\n ) & {\n getKey?: (location: ParsedLocation) => string\n },\n) {\n const router = useRouter()\n const getKey = options.getKey || defaultGetKey\n\n let elementSelector = ''\n\n if (options.id) {\n elementSelector = `[data-scroll-restoration-id=\"${options.id}\"]`\n } else {\n const element = options.getElement?.()\n if (!element) {\n return\n }\n elementSelector = getCssSelector(element)\n }\n\n const restoreKey = getKey(router.latestLocation)\n const cacheKey = [restoreKey, elementSelector].join(delimiter)\n return cache.state.cached[cacheKey]\n}\n\nfunction getCssSelector(el: any): string {\n const path = []\n let parent\n while ((parent = el.parentNode)) {\n path.unshift(\n `${el.tagName}:nth-child(${\n ([].indexOf as any).call(parent.children, el) + 1\n })`,\n )\n el = parent\n }\n return `${path.join(' > ')}`.toLowerCase()\n}\n"],"names":["React","functionalUpdate","useRouter"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAMA,MAAM,kBACJ,OAAO,WAAW,cAAcA,iBAAM,kBAAkBA,iBAAM;AAEhE,MAAM,YAAY;AAClB,MAAM,YAAY;AAElB,IAAI,2CAA2B,QAAa;AAa5C,MAAM,kBAAkB,OAAO,WAAW,eAAe,OAAO;AAEhE,MAAM,QAAe,mBAChB,MAAM;AACL,QAAM,aAAa;AAEnB,QAAM,QAAoB,KAAK;AAAA,IAC7B,OAAO,eAAe,QAAQ,UAAU,KAAK;AAAA,OAC1C,EAAE,QAAQ,IAAI,MAAM,CAAA,EAAG;AAErB,SAAA;AAAA,IACL;AAAA,IACA,KAAK,CAAC,YAAY;AAChB,YAAM,QAAQC,MAAAA,iBAAiB,SAAS,MAAM,KAAK;AACnD,aAAO,eAAe,QAAQ,YAAY,KAAK,UAAU,MAAM,KAAK,CAAC;AAAA,IAAA;AAAA,EAEzE;AACF,GAAA,IACC;AAYL,MAAM,gBAAgB,CAAC,aAA6B;AAC3C,SAAA,SAAS,MAAM,OAAQ,SAAS;AACzC;AAEO,SAAS,qBAAqB,SAAoC;AACvE,QAAM,SAASC,UAAAA,UAAU;AAEzB,kBAAgB,MAAM;AACd,UAAA,UAAS,mCAAS,WAAU;AAE5B,UAAA,EAAE,YAAY;AACpB,YAAQ,oBAAoB;AAEtB,UAAA,WAAW,CAAC,UAAiB;AACjC,UAAI,qBAAqB,IAAI,MAAM,MAAM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,SAAA;AACT;AAEO,SAAS,4BACd,SAYA;;AACA,QAAM,SAASA,UAAAA,UAAU;AACnB,QAAA,SAAS,QAAQ,UAAU;AAEjC,MAAI,kBAAkB;AAEtB,MAAI,QAAQ,IAAI;AACI,sBAAA,gCAAgC,QAAQ,EAAE;AAAA,EAAA,OACvD;AACC,UAAA,WAAU,aAAQ,eAAR;AAChB,QAAI,CAAC,SAAS;AACZ;AAAA,IAAA;AAEF,sBAAkB,eAAe,OAAO;AAAA,EAAA;AAGpC,QAAA,aAAa,OAAO,OAAO,cAAc;AAC/C,QAAM,WAAW,CAAC,YAAY,eAAe,EAAE,KAAK,SAAS;AACtD,SAAA,MAAM,MAAM,OAAO,QAAQ;AACpC;AAEA,SAAS,eAAe,IAAiB;AACvC,QAAM,OAAO,CAAC;AACV,MAAA;AACI,SAAA,SAAS,GAAG,YAAa;AAC1B,SAAA;AAAA,MACH,GAAG,GAAG,OAAO,cACV,CAAA,EAAG,QAAgB,KAAK,OAAO,UAAU,EAAE,IAAI,CAClD;AAAA,IACF;AACK,SAAA;AAAA,EAAA;AAEP,SAAO,GAAG,KAAK,KAAK,KAAK,CAAC,GAAG,YAAY;AAC3C;;;;"}
|
|
1
|
+
{"version":3,"file":"scroll-restoration.cjs","sources":["../../src/scroll-restoration.tsx"],"sourcesContent":["import * as React from 'react'\nimport { useRouter } from './useRouter'\nimport { functionalUpdate } from './utils'\nimport type { ParsedLocation } from './location'\nimport type { NonNullableUpdater } from './utils'\n\nconst useLayoutEffect =\n typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nconst windowKey = 'window'\nconst delimiter = '___'\n\nlet weakScrolledElements = new WeakSet<any>()\n\ntype CacheValue = Record<string, { scrollX: number; scrollY: number }>\ntype CacheState = {\n cached: CacheValue\n next: CacheValue\n}\n\ntype Cache = {\n state: CacheState\n set: (updater: NonNullableUpdater<CacheState>) => void\n}\n\nconst sessionsStorage = typeof window !== 'undefined' && window.sessionStorage\n\nconst cache: Cache = sessionsStorage\n ? (() => {\n const storageKey = 'tsr-scroll-restoration-v2'\n\n const state: CacheState = JSON.parse(\n window.sessionStorage.getItem(storageKey) || 'null',\n ) || { cached: {}, next: {} }\n\n return {\n state,\n set: (updater) => {\n cache.state = functionalUpdate(updater, cache.state)\n window.sessionStorage.setItem(storageKey, JSON.stringify(cache.state))\n },\n }\n })()\n : (undefined as any)\n\nexport type ScrollRestorationOptions = {\n getKey?: (location: ParsedLocation) => string\n scrollBehavior?: ScrollToOptions['behavior']\n}\n\n/**\n * The default `getKey` function for `useScrollRestoration`.\n * It returns the `key` from the location state or the `href` of the location.\n *\n * The `location.href` is used as a fallback to support the use case where the location state is not available like the initial render.\n */\nconst defaultGetKey = (location: ParsedLocation) => {\n return location.state.key! || location.href\n}\n\nexport function useScrollRestoration(options?: ScrollRestorationOptions) {\n const router = useRouter()\n\n useLayoutEffect(() => {\n const getKey = options?.getKey || defaultGetKey\n\n const { history } = window\n history.scrollRestoration = 'manual'\n\n const onScroll = (event: Event) => {\n if (weakScrolledElements.has(event.target)) return\n weakScrolledElements.add(event.target)\n\n let elementSelector = ''\n\n if (event.target === document || event.target === window) {\n elementSelector = windowKey\n } else {\n const attrId = (event.target as Element).getAttribute(\n 'data-scroll-restoration-id',\n )\n\n if (attrId) {\n elementSelector = `[data-scroll-restoration-id=\"${attrId}\"]`\n } else {\n elementSelector = getCssSelector(event.target)\n }\n }\n\n if (!cache.state.next[elementSelector]) {\n cache.set((c) => ({\n ...c,\n next: {\n ...c.next,\n [elementSelector]: {\n scrollX: NaN,\n scrollY: NaN,\n },\n },\n }))\n }\n }\n\n if (typeof document !== 'undefined') {\n document.addEventListener('scroll', onScroll, true)\n }\n\n const unsubOnBeforeLoad = router.subscribe('onBeforeLoad', (event) => {\n if (event.hrefChanged) {\n const restoreKey = getKey(event.fromLocation)\n for (const elementSelector in cache.state.next) {\n const entry = cache.state.next[elementSelector]!\n if (elementSelector === windowKey) {\n entry.scrollX = window.scrollX || 0\n entry.scrollY = window.scrollY || 0\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n entry.scrollX = element?.scrollLeft || 0\n entry.scrollY = element?.scrollTop || 0\n }\n\n cache.set((c) => {\n const next = { ...c.next }\n delete next[elementSelector]\n\n return {\n ...c,\n next,\n cached: {\n ...c.cached,\n [[restoreKey, elementSelector].join(delimiter)]: entry,\n },\n }\n })\n }\n }\n })\n\n const unsubOnBeforeRouteMount = router.subscribe(\n 'onBeforeRouteMount',\n (event) => {\n if (event.hrefChanged) {\n if (!router.resetNextScroll) {\n return\n }\n\n router.resetNextScroll = true\n\n const restoreKey = getKey(event.toLocation)\n let windowRestored = false\n\n for (const cacheKey in cache.state.cached) {\n const entry = cache.state.cached[cacheKey]!\n const [key, elementSelector] = cacheKey.split(delimiter)\n if (key === restoreKey) {\n if (elementSelector === windowKey) {\n windowRestored = true\n window.scrollTo({\n top: entry.scrollY,\n left: entry.scrollX,\n behavior: options?.scrollBehavior,\n })\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n element.scrollLeft = entry.scrollX\n element.scrollTop = entry.scrollY\n }\n }\n }\n }\n\n if (!windowRestored) {\n window.scrollTo(0, 0)\n }\n\n cache.set((c) => ({ ...c, next: {} }))\n weakScrolledElements = new WeakSet<any>()\n }\n },\n )\n\n return () => {\n document.removeEventListener('scroll', onScroll)\n unsubOnBeforeLoad()\n unsubOnBeforeRouteMount()\n }\n }, [options?.getKey, router])\n}\n\nexport function ScrollRestoration(props: ScrollRestorationOptions) {\n useScrollRestoration(props)\n return null\n}\n\nexport function useElementScrollRestoration(\n options: (\n | {\n id: string\n getElement?: () => Element | undefined | null\n }\n | {\n id?: string\n getElement: () => Element | undefined | null\n }\n ) & {\n getKey?: (location: ParsedLocation) => string\n },\n) {\n const router = useRouter()\n const getKey = options.getKey || defaultGetKey\n\n let elementSelector = ''\n\n if (options.id) {\n elementSelector = `[data-scroll-restoration-id=\"${options.id}\"]`\n } else {\n const element = options.getElement?.()\n if (!element) {\n return\n }\n elementSelector = getCssSelector(element)\n }\n\n const restoreKey = getKey(router.latestLocation)\n const cacheKey = [restoreKey, elementSelector].join(delimiter)\n return cache.state.cached[cacheKey]\n}\n\nfunction getCssSelector(el: any): string {\n const path = []\n let parent\n while ((parent = el.parentNode)) {\n path.unshift(\n `${el.tagName}:nth-child(${\n ([].indexOf as any).call(parent.children, el) + 1\n })`,\n )\n el = parent\n }\n return `${path.join(' > ')}`.toLowerCase()\n}\n"],"names":["React","functionalUpdate","useRouter"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAMA,MAAM,kBACJ,OAAO,WAAW,cAAcA,iBAAM,kBAAkBA,iBAAM;AAEhE,MAAM,YAAY;AAClB,MAAM,YAAY;AAElB,IAAI,2CAA2B,QAAa;AAa5C,MAAM,kBAAkB,OAAO,WAAW,eAAe,OAAO;AAEhE,MAAM,QAAe,mBAChB,MAAM;AACL,QAAM,aAAa;AAEnB,QAAM,QAAoB,KAAK;AAAA,IAC7B,OAAO,eAAe,QAAQ,UAAU,KAAK;AAAA,OAC1C,EAAE,QAAQ,IAAI,MAAM,CAAA,EAAG;AAErB,SAAA;AAAA,IACL;AAAA,IACA,KAAK,CAAC,YAAY;AAChB,YAAM,QAAQC,MAAAA,iBAAiB,SAAS,MAAM,KAAK;AACnD,aAAO,eAAe,QAAQ,YAAY,KAAK,UAAU,MAAM,KAAK,CAAC;AAAA,IAAA;AAAA,EAEzE;AACF,GAAA,IACC;AAaL,MAAM,gBAAgB,CAAC,aAA6B;AAC3C,SAAA,SAAS,MAAM,OAAQ,SAAS;AACzC;AAEO,SAAS,qBAAqB,SAAoC;AACvE,QAAM,SAASC,UAAAA,UAAU;AAEzB,kBAAgB,MAAM;AACd,UAAA,UAAS,mCAAS,WAAU;AAE5B,UAAA,EAAE,YAAY;AACpB,YAAQ,oBAAoB;AAEtB,UAAA,WAAW,CAAC,UAAiB;AACjC,UAAI,qBAAqB,IAAI,MAAM,MAAM,EAAG;AACvB,2BAAA,IAAI,MAAM,M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kB,OAAiC;AACjE,uBAAqB,KAAK;AACnB,SAAA;AACT;AAEO,SAAS,4BACd,SAYA;;AACA,QAAM,SAASA,UAAAA,UAAU;AACnB,QAAA,SAAS,QAAQ,UAAU;AAEjC,MAAI,kBAAkB;AAEtB,MAAI,QAAQ,IAAI;AACI,sBAAA,gCAAgC,QAAQ,EAAE;AAAA,EAAA,OACvD;AACC,UAAA,WAAU,aAAQ,eAAR;AAChB,QAAI,CAAC,SAAS;AACZ;AAAA,IAAA;AAEF,sBAAkB,eAAe,OAAO;AAAA,EAAA;AAGpC,QAAA,aAAa,OAAO,OAAO,cAAc;AAC/C,QAAM,WAAW,CAAC,YAAY,eAAe,EAAE,KAAK,SAAS;AACtD,SAAA,MAAM,MAAM,OAAO,QAAQ;AACpC;AAEA,SAAS,eAAe,IAAiB;AACvC,QAAM,OAAO,CAAC;AACV,MAAA;AACI,SAAA,SAAS,GAAG,YAAa;AAC1B,SAAA;AAAA,MACH,GAAG,GAAG,OAAO,cACV,CAAA,EAAG,QAAgB,KAAK,OAAO,UAAU,EAAE,IAAI,CAClD;AAAA,IACF;AACK,SAAA;AAAA,EAAA;AAEP,SAAO,GAAG,KAAK,KAAK,KAAK,CAAC,GAAG,YAAY;AAC3C;;;;"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ParsedLocation } from './location.cjs';
|
|
2
2
|
export type ScrollRestorationOptions = {
|
|
3
3
|
getKey?: (location: ParsedLocation) => string;
|
|
4
|
+
scrollBehavior?: ScrollToOptions['behavior'];
|
|
4
5
|
};
|
|
5
6
|
export declare function useScrollRestoration(options?: ScrollRestorationOptions): void;
|
|
6
7
|
export declare function ScrollRestoration(props: ScrollRestorationOptions): null;
|
package/dist/esm/router.js
CHANGED
|
@@ -1568,8 +1568,13 @@ ${script}\`)` : ""}; if (typeof __TSR__ !== 'undefined') __TSR__.cleanScripts()<
|
|
|
1568
1568
|
return;
|
|
1569
1569
|
});
|
|
1570
1570
|
const matches = [];
|
|
1571
|
+
const getParentContext = (parentMatch) => {
|
|
1572
|
+
const parentMatchId = parentMatch == null ? void 0 : parentMatch.id;
|
|
1573
|
+
const parentContext = !parentMatchId ? this.options.context ?? {} : parentMatch.context ?? this.options.context ?? {};
|
|
1574
|
+
return parentContext;
|
|
1575
|
+
};
|
|
1571
1576
|
matchedRoutes.forEach((route, index) => {
|
|
1572
|
-
var _a, _b, _c, _d
|
|
1577
|
+
var _a, _b, _c, _d;
|
|
1573
1578
|
const parentMatch = matches[index - 1];
|
|
1574
1579
|
const [preMatchSearch, searchError] = (() => {
|
|
1575
1580
|
const parentSearch = (parentMatch == null ? void 0 : parentMatch.search) ?? next.search;
|
|
@@ -1652,17 +1657,8 @@ ${script}\`)` : ""}; if (typeof __TSR__ !== 'undefined') __TSR__.cleanScripts()<
|
|
|
1652
1657
|
fullPath: route.fullPath
|
|
1653
1658
|
};
|
|
1654
1659
|
}
|
|
1655
|
-
const headFnContent = (_d = (_c = route.options).head) == null ? void 0 : _d.call(_c, {
|
|
1656
|
-
matches,
|
|
1657
|
-
match,
|
|
1658
|
-
params: match.params,
|
|
1659
|
-
loaderData: match.loaderData ?? void 0
|
|
1660
|
-
});
|
|
1661
|
-
match.links = headFnContent == null ? void 0 : headFnContent.links;
|
|
1662
|
-
match.scripts = headFnContent == null ? void 0 : headFnContent.scripts;
|
|
1663
|
-
match.meta = headFnContent == null ? void 0 : headFnContent.meta;
|
|
1664
1660
|
if (match.status === "success") {
|
|
1665
|
-
match.headers = (
|
|
1661
|
+
match.headers = (_d = (_c = route.options).headers) == null ? void 0 : _d.call(_c, {
|
|
1666
1662
|
loaderData: match.loaderData
|
|
1667
1663
|
});
|
|
1668
1664
|
}
|
|
@@ -1670,18 +1666,25 @@ ${script}\`)` : ""}; if (typeof __TSR__ !== 'undefined') __TSR__.cleanScripts()<
|
|
|
1670
1666
|
match.globalNotFound = globalNotFoundRouteId === route.id;
|
|
1671
1667
|
}
|
|
1672
1668
|
match.searchError = searchError;
|
|
1673
|
-
const
|
|
1674
|
-
const parentContext = !parentMatchId ? this.options.context ?? {} : parentMatch.context ?? this.options.context ?? {};
|
|
1669
|
+
const parentContext = getParentContext(parentMatch);
|
|
1675
1670
|
match.context = {
|
|
1676
1671
|
...parentContext,
|
|
1677
1672
|
...match.__routeContext,
|
|
1678
1673
|
...match.__beforeLoadContext
|
|
1679
1674
|
};
|
|
1675
|
+
matches.push(match);
|
|
1676
|
+
});
|
|
1677
|
+
matches.forEach((match, index) => {
|
|
1678
|
+
var _a, _b, _c, _d;
|
|
1679
|
+
const route = this.looseRoutesById[match.routeId];
|
|
1680
|
+
const existingMatch = this.getMatch(match.id);
|
|
1680
1681
|
if (!existingMatch && (opts == null ? void 0 : opts._buildLocation) !== true) {
|
|
1682
|
+
const parentMatch = matches[index - 1];
|
|
1683
|
+
const parentContext = getParentContext(parentMatch);
|
|
1681
1684
|
const contextFnContext = {
|
|
1682
|
-
deps: loaderDeps,
|
|
1685
|
+
deps: match.loaderDeps,
|
|
1683
1686
|
params: match.params,
|
|
1684
|
-
context:
|
|
1687
|
+
context: parentContext,
|
|
1685
1688
|
location: next,
|
|
1686
1689
|
navigate: (opts2) => this.navigate({ ...opts2, _fromLocation: next }),
|
|
1687
1690
|
buildLocation: this.buildLocation,
|
|
@@ -1690,14 +1693,22 @@ ${script}\`)` : ""}; if (typeof __TSR__ !== 'undefined') __TSR__.cleanScripts()<
|
|
|
1690
1693
|
preload: !!match.preload,
|
|
1691
1694
|
matches
|
|
1692
1695
|
};
|
|
1693
|
-
match.__routeContext = ((
|
|
1696
|
+
match.__routeContext = ((_b = (_a = route.options).context) == null ? void 0 : _b.call(_a, contextFnContext)) ?? {};
|
|
1694
1697
|
match.context = {
|
|
1695
1698
|
...parentContext,
|
|
1696
1699
|
...match.__routeContext,
|
|
1697
1700
|
...match.__beforeLoadContext
|
|
1698
1701
|
};
|
|
1699
1702
|
}
|
|
1700
|
-
|
|
1703
|
+
const headFnContent = (_d = (_c = route.options).head) == null ? void 0 : _d.call(_c, {
|
|
1704
|
+
matches,
|
|
1705
|
+
match,
|
|
1706
|
+
params: match.params,
|
|
1707
|
+
loaderData: match.loaderData ?? void 0
|
|
1708
|
+
});
|
|
1709
|
+
match.links = headFnContent == null ? void 0 : headFnContent.links;
|
|
1710
|
+
match.scripts = headFnContent == null ? void 0 : headFnContent.scripts;
|
|
1711
|
+
match.meta = headFnContent == null ? void 0 : headFnContent.meta;
|
|
1701
1712
|
});
|
|
1702
1713
|
return matches;
|
|
1703
1714
|
}
|