@tanstack/router-core 0.0.1-beta.163 → 0.0.1-beta.164
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/build/cjs/index.js +3 -0
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/scroll-restoration.js +136 -0
- package/build/cjs/scroll-restoration.js.map +1 -0
- package/build/esm/index.js +120 -1
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +139 -114
- package/build/types/index.d.ts +7 -1
- package/build/umd/index.development.js +121 -0
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/scroll-restoration.ts +175 -0
package/build/cjs/index.js
CHANGED
|
@@ -22,6 +22,7 @@ var fileRoute = require('./fileRoute.js');
|
|
|
22
22
|
var router = require('./router.js');
|
|
23
23
|
var searchParams = require('./searchParams.js');
|
|
24
24
|
var utils = require('./utils.js');
|
|
25
|
+
var scrollRestoration = require('./scroll-restoration.js');
|
|
25
26
|
|
|
26
27
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
27
28
|
|
|
@@ -75,4 +76,6 @@ exports.last = utils.last;
|
|
|
75
76
|
exports.partialDeepEqual = utils.partialDeepEqual;
|
|
76
77
|
exports.pick = utils.pick;
|
|
77
78
|
exports.replaceEqualDeep = utils.replaceEqualDeep;
|
|
79
|
+
exports.restoreScrollPositions = scrollRestoration.restoreScrollPositions;
|
|
80
|
+
exports.watchScrollPositions = scrollRestoration.watchScrollPositions;
|
|
78
81
|
//# sourceMappingURL=index.js.map
|
package/build/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @tanstack/router-core/src/index.ts
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) TanStack
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the MIT license found in the
|
|
7
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
8
|
+
*
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
|
|
15
|
+
const windowKey = 'window';
|
|
16
|
+
const delimiter = '___';
|
|
17
|
+
let weakScrolledElementsByRestoreKey = {};
|
|
18
|
+
let cache;
|
|
19
|
+
let pathDidChange = false;
|
|
20
|
+
const sessionsStorage = typeof window !== 'undefined' && window.sessionStorage;
|
|
21
|
+
const defaultGetKey = location => location.key;
|
|
22
|
+
function watchScrollPositions(router, opts) {
|
|
23
|
+
const getKey = opts?.getKey || defaultGetKey;
|
|
24
|
+
if (sessionsStorage) {
|
|
25
|
+
if (!cache) {
|
|
26
|
+
cache = (() => {
|
|
27
|
+
const storageKey = 'tsr-scroll-restoration-v1';
|
|
28
|
+
const current = JSON.parse(window.sessionStorage.getItem(storageKey) || '{}');
|
|
29
|
+
return {
|
|
30
|
+
current,
|
|
31
|
+
set: (key, value) => {
|
|
32
|
+
current[key] = value;
|
|
33
|
+
window.sessionStorage.setItem(storageKey, JSON.stringify(cache));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
})();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const {
|
|
40
|
+
history
|
|
41
|
+
} = window;
|
|
42
|
+
if (history.scrollRestoration) {
|
|
43
|
+
history.scrollRestoration = 'manual';
|
|
44
|
+
}
|
|
45
|
+
const onScroll = event => {
|
|
46
|
+
const restoreKey = getKey(router.state.resolvedLocation);
|
|
47
|
+
if (!weakScrolledElementsByRestoreKey[restoreKey]) {
|
|
48
|
+
weakScrolledElementsByRestoreKey[restoreKey] = new WeakSet();
|
|
49
|
+
}
|
|
50
|
+
const set = weakScrolledElementsByRestoreKey[restoreKey];
|
|
51
|
+
if (set.has(event.target)) return;
|
|
52
|
+
set.add(event.target);
|
|
53
|
+
const cacheKey = [restoreKey, event.target === document || event.target === window ? windowKey : getCssSelector(event.target)].join(delimiter);
|
|
54
|
+
if (!cache.current[cacheKey]) {
|
|
55
|
+
cache.set(cacheKey, {
|
|
56
|
+
scrollX: NaN,
|
|
57
|
+
scrollY: NaN
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const getCssSelector = el => {
|
|
62
|
+
let path = [],
|
|
63
|
+
parent;
|
|
64
|
+
while (parent = el.parentNode) {
|
|
65
|
+
path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el) + 1})`);
|
|
66
|
+
el = parent;
|
|
67
|
+
}
|
|
68
|
+
return `${path.join(' > ')}`.toLowerCase();
|
|
69
|
+
};
|
|
70
|
+
const onPathWillChange = from => {
|
|
71
|
+
const restoreKey = getKey(from);
|
|
72
|
+
for (const cacheKey in cache.current) {
|
|
73
|
+
const entry = cache.current[cacheKey];
|
|
74
|
+
const [key, elementSelector] = cacheKey.split(delimiter);
|
|
75
|
+
if (restoreKey === key) {
|
|
76
|
+
if (elementSelector === windowKey) {
|
|
77
|
+
entry.scrollX = window.scrollX || 0;
|
|
78
|
+
entry.scrollY = window.scrollY || 0;
|
|
79
|
+
} else if (elementSelector) {
|
|
80
|
+
const element = document.querySelector(elementSelector);
|
|
81
|
+
entry.scrollX = element?.scrollLeft || 0;
|
|
82
|
+
entry.scrollY = element?.scrollTop || 0;
|
|
83
|
+
}
|
|
84
|
+
cache.set(cacheKey, entry);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const onPathChange = () => {
|
|
89
|
+
pathDidChange = true;
|
|
90
|
+
};
|
|
91
|
+
if (typeof document !== 'undefined') {
|
|
92
|
+
document.addEventListener('scroll', onScroll, true);
|
|
93
|
+
}
|
|
94
|
+
const unsubOnBeforeLoad = router.subscribe('onBeforeLoad', event => {
|
|
95
|
+
if (event.pathChanged) onPathWillChange(event.from);
|
|
96
|
+
});
|
|
97
|
+
const unsubOnLoad = router.subscribe('onLoad', event => {
|
|
98
|
+
if (event.pathChanged) onPathChange();
|
|
99
|
+
});
|
|
100
|
+
return () => {
|
|
101
|
+
document.removeEventListener('scroll', onScroll);
|
|
102
|
+
unsubOnBeforeLoad();
|
|
103
|
+
unsubOnLoad();
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function restoreScrollPositions(router, opts) {
|
|
107
|
+
if (pathDidChange) {
|
|
108
|
+
const getKey = opts?.getKey || defaultGetKey;
|
|
109
|
+
pathDidChange = false;
|
|
110
|
+
const restoreKey = getKey(router.state.location);
|
|
111
|
+
let windowRestored = false;
|
|
112
|
+
for (const cacheKey in cache.current) {
|
|
113
|
+
const entry = cache.current[cacheKey];
|
|
114
|
+
const [key, elementSelector] = cacheKey.split(delimiter);
|
|
115
|
+
if (key === restoreKey) {
|
|
116
|
+
if (elementSelector === windowKey) {
|
|
117
|
+
windowRestored = true;
|
|
118
|
+
window.scrollTo(entry.scrollX, entry.scrollY);
|
|
119
|
+
} else if (elementSelector) {
|
|
120
|
+
const element = document.querySelector(elementSelector);
|
|
121
|
+
if (element) {
|
|
122
|
+
element.scrollLeft = entry.scrollX;
|
|
123
|
+
element.scrollTop = entry.scrollY;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (!windowRestored) {
|
|
129
|
+
window.scrollTo(0, 0);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
exports.restoreScrollPositions = restoreScrollPositions;
|
|
135
|
+
exports.watchScrollPositions = watchScrollPositions;
|
|
136
|
+
//# sourceMappingURL=scroll-restoration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scroll-restoration.js","sources":["../../src/scroll-restoration.ts"],"sourcesContent":["import { AnyRouter, ParsedLocation } from './router'\n\nconst windowKey = 'window'\nconst delimiter = '___'\n\nlet weakScrolledElementsByRestoreKey: Record<string, WeakSet<any>> = {}\n\ntype CacheValue = Record<string, { scrollX: number; scrollY: number }>\n\ntype Cache = {\n current: CacheValue\n set: (key: string, value: any) => void\n}\n\nlet cache: Cache\n\nlet pathDidChange = false\n\nconst sessionsStorage = typeof window !== 'undefined' && window.sessionStorage\n\nexport type ScrollRestorationOptions = {\n getKey?: (location: ParsedLocation) => string\n}\n\nconst defaultGetKey = (location: ParsedLocation) => location.key!\n\nexport function watchScrollPositions(\n router: AnyRouter,\n opts?: ScrollRestorationOptions,\n) {\n const getKey = opts?.getKey || defaultGetKey\n\n if (sessionsStorage) {\n if (!cache) {\n cache = (() => {\n const storageKey = 'tsr-scroll-restoration-v1'\n\n const current: CacheValue = JSON.parse(\n window.sessionStorage.getItem(storageKey) || '{}',\n )\n\n return {\n current,\n set: (key: string, value: any) => {\n current[key] = value\n window.sessionStorage.setItem(storageKey, JSON.stringify(cache))\n },\n }\n })()\n }\n }\n\n const { history } = window\n if (history.scrollRestoration) {\n history.scrollRestoration = 'manual'\n }\n\n const onScroll = (event: Event) => {\n const restoreKey = getKey(router.state.resolvedLocation)\n\n if (!weakScrolledElementsByRestoreKey[restoreKey]) {\n weakScrolledElementsByRestoreKey[restoreKey] = new WeakSet()\n }\n\n const set = weakScrolledElementsByRestoreKey[restoreKey]!\n\n if (set.has(event.target)) return\n set.add(event.target)\n\n const cacheKey = [\n restoreKey,\n event.target === document || event.target === window\n ? windowKey\n : getCssSelector(event.target),\n ].join(delimiter)\n\n if (!cache.current[cacheKey]) {\n cache.set(cacheKey, {\n scrollX: NaN,\n scrollY: NaN,\n })\n }\n }\n\n const getCssSelector = (el: any): string => {\n let path = [],\n 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\n const onPathWillChange = (from: ParsedLocation) => {\n const restoreKey = getKey(from)\n for (const cacheKey in cache.current) {\n const entry = cache.current[cacheKey]!\n const [key, elementSelector] = cacheKey.split(delimiter)\n if (restoreKey === key) {\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(cacheKey, entry)\n }\n }\n }\n\n const onPathChange = () => {\n pathDidChange = true\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.pathChanged) onPathWillChange(event.from)\n })\n\n const unsubOnLoad = router.subscribe('onLoad', (event) => {\n if (event.pathChanged) onPathChange()\n })\n\n return () => {\n document.removeEventListener('scroll', onScroll)\n unsubOnBeforeLoad()\n unsubOnLoad()\n }\n}\n\nexport function restoreScrollPositions(\n router: AnyRouter,\n opts?: ScrollRestorationOptions,\n) {\n if (pathDidChange) {\n const getKey = opts?.getKey || defaultGetKey\n\n pathDidChange = false\n\n const restoreKey = getKey(router.state.location)\n let windowRestored = false\n\n for (const cacheKey in cache.current) {\n const entry = cache.current[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}\n"],"names":["windowKey","delimiter","weakScrolledElementsByRestoreKey","cache","pathDidChange","sessionsStorage","window","sessionStorage","defaultGetKey","location","key","watchScrollPositions","router","opts","getKey","storageKey","current","JSON","parse","getItem","set","value","setItem","stringify","history","scrollRestoration","onScroll","event","restoreKey","state","resolvedLocation","WeakSet","has","target","add","cacheKey","document","getCssSelector","join","scrollX","NaN","scrollY","el","path","parent","parentNode","unshift","tagName","indexOf","call","children","toLowerCase","onPathWillChange","from","entry","elementSelector","split","element","querySelector","scrollLeft","scrollTop","onPathChange","addEventListener","unsubOnBeforeLoad","subscribe","pathChanged","unsubOnLoad","removeEventListener","restoreScrollPositions","windowRestored","scrollTo"],"mappings":";;;;;;;;;;;;;;AAEA,MAAMA,SAAS,GAAG,QAAQ,CAAA;AAC1B,MAAMC,SAAS,GAAG,KAAK,CAAA;AAEvB,IAAIC,gCAA8D,GAAG,EAAE,CAAA;AASvE,IAAIC,KAAY,CAAA;AAEhB,IAAIC,aAAa,GAAG,KAAK,CAAA;AAEzB,MAAMC,eAAe,GAAG,OAAOC,MAAM,KAAK,WAAW,IAAIA,MAAM,CAACC,cAAc,CAAA;AAM9E,MAAMC,aAAa,GAAIC,QAAwB,IAAKA,QAAQ,CAACC,GAAI,CAAA;AAE1D,SAASC,oBAAoBA,CAClCC,MAAiB,EACjBC,IAA+B,EAC/B;AACA,EAAA,MAAMC,MAAM,GAAGD,IAAI,EAAEC,MAAM,IAAIN,aAAa,CAAA;AAE5C,EAAA,IAAIH,eAAe,EAAE;IACnB,IAAI,CAACF,KAAK,EAAE;MACVA,KAAK,GAAG,CAAC,MAAM;QACb,MAAMY,UAAU,GAAG,2BAA2B,CAAA;AAE9C,QAAA,MAAMC,OAAmB,GAAGC,IAAI,CAACC,KAAK,CACpCZ,MAAM,CAACC,cAAc,CAACY,OAAO,CAACJ,UAAU,CAAC,IAAI,IAC/C,CAAC,CAAA;QAED,OAAO;UACLC,OAAO;AACPI,UAA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ED
|
@@ -1816,5 +1816,124 @@ function lazyFn(fn, key) {
|
|
|
1816
1816
|
};
|
|
1817
1817
|
}
|
|
1818
1818
|
|
|
1819
|
-
|
|
1819
|
+
const windowKey = 'window';
|
|
1820
|
+
const delimiter = '___';
|
|
1821
|
+
let weakScrolledElementsByRestoreKey = {};
|
|
1822
|
+
let cache;
|
|
1823
|
+
let pathDidChange = false;
|
|
1824
|
+
const sessionsStorage = typeof window !== 'undefined' && window.sessionStorage;
|
|
1825
|
+
const defaultGetKey = location => location.key;
|
|
1826
|
+
function watchScrollPositions(router, opts) {
|
|
1827
|
+
const getKey = opts?.getKey || defaultGetKey;
|
|
1828
|
+
if (sessionsStorage) {
|
|
1829
|
+
if (!cache) {
|
|
1830
|
+
cache = (() => {
|
|
1831
|
+
const storageKey = 'tsr-scroll-restoration-v1';
|
|
1832
|
+
const current = JSON.parse(window.sessionStorage.getItem(storageKey) || '{}');
|
|
1833
|
+
return {
|
|
1834
|
+
current,
|
|
1835
|
+
set: (key, value) => {
|
|
1836
|
+
current[key] = value;
|
|
1837
|
+
window.sessionStorage.setItem(storageKey, JSON.stringify(cache));
|
|
1838
|
+
}
|
|
1839
|
+
};
|
|
1840
|
+
})();
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
const {
|
|
1844
|
+
history
|
|
1845
|
+
} = window;
|
|
1846
|
+
if (history.scrollRestoration) {
|
|
1847
|
+
history.scrollRestoration = 'manual';
|
|
1848
|
+
}
|
|
1849
|
+
const onScroll = event => {
|
|
1850
|
+
const restoreKey = getKey(router.state.resolvedLocation);
|
|
1851
|
+
if (!weakScrolledElementsByRestoreKey[restoreKey]) {
|
|
1852
|
+
weakScrolledElementsByRestoreKey[restoreKey] = new WeakSet();
|
|
1853
|
+
}
|
|
1854
|
+
const set = weakScrolledElementsByRestoreKey[restoreKey];
|
|
1855
|
+
if (set.has(event.target)) return;
|
|
1856
|
+
set.add(event.target);
|
|
1857
|
+
const cacheKey = [restoreKey, event.target === document || event.target === window ? windowKey : getCssSelector(event.target)].join(delimiter);
|
|
1858
|
+
if (!cache.current[cacheKey]) {
|
|
1859
|
+
cache.set(cacheKey, {
|
|
1860
|
+
scrollX: NaN,
|
|
1861
|
+
scrollY: NaN
|
|
1862
|
+
});
|
|
1863
|
+
}
|
|
1864
|
+
};
|
|
1865
|
+
const getCssSelector = el => {
|
|
1866
|
+
let path = [],
|
|
1867
|
+
parent;
|
|
1868
|
+
while (parent = el.parentNode) {
|
|
1869
|
+
path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el) + 1})`);
|
|
1870
|
+
el = parent;
|
|
1871
|
+
}
|
|
1872
|
+
return `${path.join(' > ')}`.toLowerCase();
|
|
1873
|
+
};
|
|
1874
|
+
const onPathWillChange = from => {
|
|
1875
|
+
const restoreKey = getKey(from);
|
|
1876
|
+
for (const cacheKey in cache.current) {
|
|
1877
|
+
const entry = cache.current[cacheKey];
|
|
1878
|
+
const [key, elementSelector] = cacheKey.split(delimiter);
|
|
1879
|
+
if (restoreKey === key) {
|
|
1880
|
+
if (elementSelector === windowKey) {
|
|
1881
|
+
entry.scrollX = window.scrollX || 0;
|
|
1882
|
+
entry.scrollY = window.scrollY || 0;
|
|
1883
|
+
} else if (elementSelector) {
|
|
1884
|
+
const element = document.querySelector(elementSelector);
|
|
1885
|
+
entry.scrollX = element?.scrollLeft || 0;
|
|
1886
|
+
entry.scrollY = element?.scrollTop || 0;
|
|
1887
|
+
}
|
|
1888
|
+
cache.set(cacheKey, entry);
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
};
|
|
1892
|
+
const onPathChange = () => {
|
|
1893
|
+
pathDidChange = true;
|
|
1894
|
+
};
|
|
1895
|
+
if (typeof document !== 'undefined') {
|
|
1896
|
+
document.addEventListener('scroll', onScroll, true);
|
|
1897
|
+
}
|
|
1898
|
+
const unsubOnBeforeLoad = router.subscribe('onBeforeLoad', event => {
|
|
1899
|
+
if (event.pathChanged) onPathWillChange(event.from);
|
|
1900
|
+
});
|
|
1901
|
+
const unsubOnLoad = router.subscribe('onLoad', event => {
|
|
1902
|
+
if (event.pathChanged) onPathChange();
|
|
1903
|
+
});
|
|
1904
|
+
return () => {
|
|
1905
|
+
document.removeEventListener('scroll', onScroll);
|
|
1906
|
+
unsubOnBeforeLoad();
|
|
1907
|
+
unsubOnLoad();
|
|
1908
|
+
};
|
|
1909
|
+
}
|
|
1910
|
+
function restoreScrollPositions(router, opts) {
|
|
1911
|
+
if (pathDidChange) {
|
|
1912
|
+
const getKey = opts?.getKey || defaultGetKey;
|
|
1913
|
+
pathDidChange = false;
|
|
1914
|
+
const restoreKey = getKey(router.state.location);
|
|
1915
|
+
let windowRestored = false;
|
|
1916
|
+
for (const cacheKey in cache.current) {
|
|
1917
|
+
const entry = cache.current[cacheKey];
|
|
1918
|
+
const [key, elementSelector] = cacheKey.split(delimiter);
|
|
1919
|
+
if (key === restoreKey) {
|
|
1920
|
+
if (elementSelector === windowKey) {
|
|
1921
|
+
windowRestored = true;
|
|
1922
|
+
window.scrollTo(entry.scrollX, entry.scrollY);
|
|
1923
|
+
} else if (elementSelector) {
|
|
1924
|
+
const element = document.querySelector(elementSelector);
|
|
1925
|
+
if (element) {
|
|
1926
|
+
element.scrollLeft = entry.scrollX;
|
|
1927
|
+
element.scrollTop = entry.scrollY;
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
if (!windowRestored) {
|
|
1933
|
+
window.scrollTo(0, 0);
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
export { FileRoute, PathParamError, RootRoute, Route, Router, RouterContext, SearchParamError, cleanPath, componentTypes, createBrowserHistory, createHashHistory, createMemoryHistory, decode, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, isPlainObject, isRedirect, joinPaths, last, lazyFn, matchByPath, matchPathname, parsePathname, parseSearchWith, partialDeepEqual, pick, redirect, replaceEqualDeep, resolvePath, restoreScrollPositions, rootRouteId, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, watchScrollPositions };
|
|
1820
1939
|
//# sourceMappingURL=index.js.map
|