@tinkoff/router 0.5.27 → 0.5.46
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/lib/components/react/providers/default.browser.js +2 -2
- package/lib/components/react/providers/default.es.js +2 -2
- package/lib/components/react/providers/default.js +2 -2
- package/lib/components/react/providers/transitions.browser.js +4 -7
- package/lib/components/react/providers/transitions.es.js +4 -7
- package/lib/components/react/providers/transitions.js +4 -7
- package/lib/components/react/useNavigate.browser.js +1 -1
- package/lib/components/react/useNavigate.es.js +1 -1
- package/lib/components/react/useNavigate.js +1 -1
- package/lib/history/client.browser.js +7 -10
- package/lib/history/client.es.js +7 -10
- package/lib/history/client.js +7 -10
- package/lib/router/abstract.browser.js +17 -28
- package/lib/router/abstract.es.js +17 -28
- package/lib/router/abstract.js +17 -28
- package/lib/router/browser.browser.js +5 -9
- package/lib/router/client.browser.js +4 -6
- package/lib/router/client.es.js +4 -6
- package/lib/router/client.js +4 -6
- package/lib/router/server.es.js +1 -2
- package/lib/router/server.js +1 -2
- package/lib/tree/tree.browser.js +3 -3
- package/lib/tree/tree.es.js +3 -3
- package/lib/tree/tree.js +3 -3
- package/lib/tree/utils.browser.js +1 -2
- package/lib/tree/utils.es.js +1 -2
- package/lib/tree/utils.js +1 -2
- package/package.json +2 -2
|
@@ -8,8 +8,8 @@ const DefaultProvider = ({ router, children, serverState, }) => {
|
|
|
8
8
|
const lastRoute = useMemo(() => router.getCurrentRoute(), [router]);
|
|
9
9
|
const lastUrl = useMemo(() => router.getCurrentUrl(), [router]);
|
|
10
10
|
const subscription = useCallback((cb) => router.registerSyncHook('change', cb), [router]);
|
|
11
|
-
const route = useSyncExternalStore(subscription, () =>
|
|
12
|
-
const url = useSyncExternalStore(subscription, () =>
|
|
11
|
+
const route = useSyncExternalStore(subscription, () => router.getLastRoute?.() ?? lastRoute, serverState ? () => serverState.route : () => router.getLastRoute?.() ?? lastRoute);
|
|
12
|
+
const url = useSyncExternalStore(subscription, () => router.getLastUrl?.() ?? lastUrl, serverState ? () => serverState.url : () => router.getLastUrl?.() ?? lastUrl);
|
|
13
13
|
return (jsx(RouterContext.Provider, { value: router, children: jsx(RouteContext.Provider, { value: route, children: jsx(UrlContext.Provider, { value: url, children: children }) }) }));
|
|
14
14
|
};
|
|
15
15
|
DefaultProvider.displayName = 'TinkoffRouterProvider';
|
|
@@ -8,8 +8,8 @@ const DefaultProvider = ({ router, children, serverState, }) => {
|
|
|
8
8
|
const lastRoute = useMemo(() => router.getCurrentRoute(), [router]);
|
|
9
9
|
const lastUrl = useMemo(() => router.getCurrentUrl(), [router]);
|
|
10
10
|
const subscription = useCallback((cb) => router.registerSyncHook('change', cb), [router]);
|
|
11
|
-
const route = useSyncExternalStore(subscription, () =>
|
|
12
|
-
const url = useSyncExternalStore(subscription, () =>
|
|
11
|
+
const route = useSyncExternalStore(subscription, () => router.getLastRoute?.() ?? lastRoute, serverState ? () => serverState.route : () => router.getLastRoute?.() ?? lastRoute);
|
|
12
|
+
const url = useSyncExternalStore(subscription, () => router.getLastUrl?.() ?? lastUrl, serverState ? () => serverState.url : () => router.getLastUrl?.() ?? lastUrl);
|
|
13
13
|
return (jsx(RouterContext.Provider, { value: router, children: jsx(RouteContext.Provider, { value: route, children: jsx(UrlContext.Provider, { value: url, children: children }) }) }));
|
|
14
14
|
};
|
|
15
15
|
DefaultProvider.displayName = 'TinkoffRouterProvider';
|
|
@@ -12,8 +12,8 @@ const DefaultProvider = ({ router, children, serverState, }) => {
|
|
|
12
12
|
const lastRoute = react.useMemo(() => router.getCurrentRoute(), [router]);
|
|
13
13
|
const lastUrl = react.useMemo(() => router.getCurrentUrl(), [router]);
|
|
14
14
|
const subscription = react.useCallback((cb) => router.registerSyncHook('change', cb), [router]);
|
|
15
|
-
const route = shim.useSyncExternalStore(subscription, () =>
|
|
16
|
-
const url = shim.useSyncExternalStore(subscription, () =>
|
|
15
|
+
const route = shim.useSyncExternalStore(subscription, () => router.getLastRoute?.() ?? lastRoute, serverState ? () => serverState.route : () => router.getLastRoute?.() ?? lastRoute);
|
|
16
|
+
const url = shim.useSyncExternalStore(subscription, () => router.getLastUrl?.() ?? lastUrl, serverState ? () => serverState.url : () => router.getLastUrl?.() ?? lastUrl);
|
|
17
17
|
return (jsxRuntime.jsx(context.RouterContext.Provider, { value: router, children: jsxRuntime.jsx(context.RouteContext.Provider, { value: route, children: jsxRuntime.jsx(context.UrlContext.Provider, { value: url, children: children }) }) }));
|
|
18
18
|
};
|
|
19
19
|
DefaultProvider.displayName = 'TinkoffRouterProvider';
|
|
@@ -17,13 +17,10 @@ const startReactTransition = typeof startTransition === 'function' && process.en
|
|
|
17
17
|
};
|
|
18
18
|
const TransitionsProvider = ({ router, children, }) => {
|
|
19
19
|
// fallback to outdated router implementation
|
|
20
|
-
const [state, setState] = useState(() => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
url: (_b = router.getLastUrl()) !== null && _b !== void 0 ? _b : router.getCurrentUrl(),
|
|
25
|
-
});
|
|
26
|
-
});
|
|
20
|
+
const [state, setState] = useState(() => ({
|
|
21
|
+
route: router.getLastRoute() ?? router.getCurrentRoute(),
|
|
22
|
+
url: router.getLastUrl() ?? router.getCurrentUrl(),
|
|
23
|
+
}));
|
|
27
24
|
// Router state that will be applied to the DOM.
|
|
28
25
|
const [pendingState, setPendingState] = useState(null);
|
|
29
26
|
// View transition state for context.
|
|
@@ -17,13 +17,10 @@ const startReactTransition = typeof startTransition === 'function' && process.en
|
|
|
17
17
|
};
|
|
18
18
|
const TransitionsProvider = ({ router, children, }) => {
|
|
19
19
|
// fallback to outdated router implementation
|
|
20
|
-
const [state, setState] = useState(() => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
url: (_b = router.getLastUrl()) !== null && _b !== void 0 ? _b : router.getCurrentUrl(),
|
|
25
|
-
});
|
|
26
|
-
});
|
|
20
|
+
const [state, setState] = useState(() => ({
|
|
21
|
+
route: router.getLastRoute() ?? router.getCurrentRoute(),
|
|
22
|
+
url: router.getLastUrl() ?? router.getCurrentUrl(),
|
|
23
|
+
}));
|
|
27
24
|
// Router state that will be applied to the DOM.
|
|
28
25
|
const [pendingState, setPendingState] = useState(null);
|
|
29
26
|
// View transition state for context.
|
|
@@ -21,13 +21,10 @@ const startReactTransition = typeof startTransition === 'function' && process.en
|
|
|
21
21
|
};
|
|
22
22
|
const TransitionsProvider = ({ router, children, }) => {
|
|
23
23
|
// fallback to outdated router implementation
|
|
24
|
-
const [state, setState] = react.useState(() => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
url: (_b = router.getLastUrl()) !== null && _b !== void 0 ? _b : router.getCurrentUrl(),
|
|
29
|
-
});
|
|
30
|
-
});
|
|
24
|
+
const [state, setState] = react.useState(() => ({
|
|
25
|
+
route: router.getLastRoute() ?? router.getCurrentRoute(),
|
|
26
|
+
url: router.getLastUrl() ?? router.getCurrentUrl(),
|
|
27
|
+
}));
|
|
31
28
|
// Router state that will be applied to the DOM.
|
|
32
29
|
const [pendingState, setPendingState] = react.useState(null);
|
|
33
30
|
// View transition state for context.
|
|
@@ -9,7 +9,7 @@ const useNavigate = (rootOptions) => {
|
|
|
9
9
|
const router = useRouter();
|
|
10
10
|
const rootOpts = useShallowEqual(convertToNavigateOptions(rootOptions));
|
|
11
11
|
return useCallback((specificOptions) => {
|
|
12
|
-
const opts = rootOpts
|
|
12
|
+
const opts = rootOpts ?? convertToNavigateOptions(specificOptions);
|
|
13
13
|
return router.navigate(opts);
|
|
14
14
|
}, [rootOpts, router]);
|
|
15
15
|
};
|
|
@@ -9,7 +9,7 @@ const useNavigate = (rootOptions) => {
|
|
|
9
9
|
const router = useRouter();
|
|
10
10
|
const rootOpts = useShallowEqual(convertToNavigateOptions(rootOptions));
|
|
11
11
|
return useCallback((specificOptions) => {
|
|
12
|
-
const opts = rootOpts
|
|
12
|
+
const opts = rootOpts ?? convertToNavigateOptions(specificOptions);
|
|
13
13
|
return router.navigate(opts);
|
|
14
14
|
}, [rootOpts, router]);
|
|
15
15
|
};
|
|
@@ -13,7 +13,7 @@ const useNavigate = (rootOptions) => {
|
|
|
13
13
|
const router = useRouter.useRouter();
|
|
14
14
|
const rootOpts = reactHooks.useShallowEqual(convertToNavigateOptions(rootOptions));
|
|
15
15
|
return react.useCallback((specificOptions) => {
|
|
16
|
-
const opts = rootOpts
|
|
16
|
+
const opts = rootOpts ?? convertToNavigateOptions(specificOptions);
|
|
17
17
|
return router.navigate(opts);
|
|
18
18
|
}, [rootOpts, router]);
|
|
19
19
|
};
|
|
@@ -13,8 +13,8 @@ const generateKey = (navigation) => {
|
|
|
13
13
|
const generatePreviousNavigateState = (navigation, currentState) => {
|
|
14
14
|
const state = {};
|
|
15
15
|
if (navigation.replace) {
|
|
16
|
-
const previousRoute = currentState
|
|
17
|
-
const previousUrl = currentState
|
|
16
|
+
const previousRoute = currentState?.navigateState.previousRoute;
|
|
17
|
+
const previousUrl = currentState?.navigateState.previousUrl;
|
|
18
18
|
if (previousRoute) {
|
|
19
19
|
state.previousRoute = previousRoute;
|
|
20
20
|
}
|
|
@@ -64,14 +64,12 @@ class ClientHistory extends History {
|
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
init(navigation) {
|
|
67
|
-
|
|
68
|
-
this.currentState = isHistoryState((_a = window.history) === null || _a === void 0 ? void 0 : _a.state)
|
|
67
|
+
this.currentState = isHistoryState(window.history?.state)
|
|
69
68
|
? window.history.state
|
|
70
69
|
: generateState(navigation);
|
|
71
70
|
this.currentIndex = this.currentState.index;
|
|
72
71
|
this.historyWrapper.init(this.currentState);
|
|
73
72
|
this.historyWrapper.subscribe(async ({ path, state }) => {
|
|
74
|
-
var _a, _b;
|
|
75
73
|
try {
|
|
76
74
|
let navigationType;
|
|
77
75
|
let navigateState;
|
|
@@ -98,10 +96,10 @@ class ClientHistory extends History {
|
|
|
98
96
|
url: path,
|
|
99
97
|
navigateState,
|
|
100
98
|
});
|
|
101
|
-
|
|
99
|
+
this.goPromiseResolve?.();
|
|
102
100
|
}
|
|
103
101
|
catch (err) {
|
|
104
|
-
|
|
102
|
+
this.goPromiseReject?.(err);
|
|
105
103
|
}
|
|
106
104
|
});
|
|
107
105
|
}
|
|
@@ -118,9 +116,8 @@ class ClientHistory extends History {
|
|
|
118
116
|
});
|
|
119
117
|
}
|
|
120
118
|
go(to, options) {
|
|
121
|
-
var _a;
|
|
122
119
|
if (this.currentIndex < 1) {
|
|
123
|
-
if (options
|
|
120
|
+
if (options?.historyFallback) {
|
|
124
121
|
return this.listener({
|
|
125
122
|
url: options.historyFallback,
|
|
126
123
|
type: 'navigate',
|
|
@@ -128,7 +125,7 @@ class ClientHistory extends History {
|
|
|
128
125
|
replace: options.replace,
|
|
129
126
|
});
|
|
130
127
|
}
|
|
131
|
-
const historyFallbackRoute =
|
|
128
|
+
const historyFallbackRoute = this.tree?.getHistoryFallback(window.location.pathname);
|
|
132
129
|
if (historyFallbackRoute) {
|
|
133
130
|
return this.listener({
|
|
134
131
|
url: historyFallbackRoute.actualPath,
|
package/lib/history/client.es.js
CHANGED
|
@@ -13,8 +13,8 @@ const generateKey = (navigation) => {
|
|
|
13
13
|
const generatePreviousNavigateState = (navigation, currentState) => {
|
|
14
14
|
const state = {};
|
|
15
15
|
if (navigation.replace) {
|
|
16
|
-
const previousRoute = currentState
|
|
17
|
-
const previousUrl = currentState
|
|
16
|
+
const previousRoute = currentState?.navigateState.previousRoute;
|
|
17
|
+
const previousUrl = currentState?.navigateState.previousUrl;
|
|
18
18
|
if (previousRoute) {
|
|
19
19
|
state.previousRoute = previousRoute;
|
|
20
20
|
}
|
|
@@ -64,14 +64,12 @@ class ClientHistory extends History {
|
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
init(navigation) {
|
|
67
|
-
|
|
68
|
-
this.currentState = isHistoryState((_a = window.history) === null || _a === void 0 ? void 0 : _a.state)
|
|
67
|
+
this.currentState = isHistoryState(window.history?.state)
|
|
69
68
|
? window.history.state
|
|
70
69
|
: generateState(navigation);
|
|
71
70
|
this.currentIndex = this.currentState.index;
|
|
72
71
|
this.historyWrapper.init(this.currentState);
|
|
73
72
|
this.historyWrapper.subscribe(async ({ path, state }) => {
|
|
74
|
-
var _a, _b;
|
|
75
73
|
try {
|
|
76
74
|
let navigationType;
|
|
77
75
|
let navigateState;
|
|
@@ -98,10 +96,10 @@ class ClientHistory extends History {
|
|
|
98
96
|
url: path,
|
|
99
97
|
navigateState,
|
|
100
98
|
});
|
|
101
|
-
|
|
99
|
+
this.goPromiseResolve?.();
|
|
102
100
|
}
|
|
103
101
|
catch (err) {
|
|
104
|
-
|
|
102
|
+
this.goPromiseReject?.(err);
|
|
105
103
|
}
|
|
106
104
|
});
|
|
107
105
|
}
|
|
@@ -118,9 +116,8 @@ class ClientHistory extends History {
|
|
|
118
116
|
});
|
|
119
117
|
}
|
|
120
118
|
go(to, options) {
|
|
121
|
-
var _a;
|
|
122
119
|
if (this.currentIndex < 1) {
|
|
123
|
-
if (options
|
|
120
|
+
if (options?.historyFallback) {
|
|
124
121
|
return this.listener({
|
|
125
122
|
url: options.historyFallback,
|
|
126
123
|
type: 'navigate',
|
|
@@ -128,7 +125,7 @@ class ClientHistory extends History {
|
|
|
128
125
|
replace: options.replace,
|
|
129
126
|
});
|
|
130
127
|
}
|
|
131
|
-
const historyFallbackRoute =
|
|
128
|
+
const historyFallbackRoute = this.tree?.getHistoryFallback(window.location.pathname);
|
|
132
129
|
if (historyFallbackRoute) {
|
|
133
130
|
return this.listener({
|
|
134
131
|
url: historyFallbackRoute.actualPath,
|
package/lib/history/client.js
CHANGED
|
@@ -17,8 +17,8 @@ const generateKey = (navigation) => {
|
|
|
17
17
|
const generatePreviousNavigateState = (navigation, currentState) => {
|
|
18
18
|
const state = {};
|
|
19
19
|
if (navigation.replace) {
|
|
20
|
-
const previousRoute = currentState
|
|
21
|
-
const previousUrl = currentState
|
|
20
|
+
const previousRoute = currentState?.navigateState.previousRoute;
|
|
21
|
+
const previousUrl = currentState?.navigateState.previousUrl;
|
|
22
22
|
if (previousRoute) {
|
|
23
23
|
state.previousRoute = previousRoute;
|
|
24
24
|
}
|
|
@@ -68,14 +68,12 @@ class ClientHistory extends base.History {
|
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
70
|
init(navigation) {
|
|
71
|
-
|
|
72
|
-
this.currentState = isHistoryState((_a = window.history) === null || _a === void 0 ? void 0 : _a.state)
|
|
71
|
+
this.currentState = isHistoryState(window.history?.state)
|
|
73
72
|
? window.history.state
|
|
74
73
|
: generateState(navigation);
|
|
75
74
|
this.currentIndex = this.currentState.index;
|
|
76
75
|
this.historyWrapper.init(this.currentState);
|
|
77
76
|
this.historyWrapper.subscribe(async ({ path, state }) => {
|
|
78
|
-
var _a, _b;
|
|
79
77
|
try {
|
|
80
78
|
let navigationType;
|
|
81
79
|
let navigateState;
|
|
@@ -102,10 +100,10 @@ class ClientHistory extends base.History {
|
|
|
102
100
|
url: path,
|
|
103
101
|
navigateState,
|
|
104
102
|
});
|
|
105
|
-
|
|
103
|
+
this.goPromiseResolve?.();
|
|
106
104
|
}
|
|
107
105
|
catch (err) {
|
|
108
|
-
|
|
106
|
+
this.goPromiseReject?.(err);
|
|
109
107
|
}
|
|
110
108
|
});
|
|
111
109
|
}
|
|
@@ -122,9 +120,8 @@ class ClientHistory extends base.History {
|
|
|
122
120
|
});
|
|
123
121
|
}
|
|
124
122
|
go(to, options) {
|
|
125
|
-
var _a;
|
|
126
123
|
if (this.currentIndex < 1) {
|
|
127
|
-
if (options
|
|
124
|
+
if (options?.historyFallback) {
|
|
128
125
|
return this.listener({
|
|
129
126
|
url: options.historyFallback,
|
|
130
127
|
type: 'navigate',
|
|
@@ -132,7 +129,7 @@ class ClientHistory extends base.History {
|
|
|
132
129
|
replace: options.replace,
|
|
133
130
|
});
|
|
134
131
|
}
|
|
135
|
-
const historyFallbackRoute =
|
|
132
|
+
const historyFallbackRoute = this.tree?.getHistoryFallback(window.location.pathname);
|
|
136
133
|
if (historyFallbackRoute) {
|
|
137
134
|
return this.listener({
|
|
138
135
|
url: historyFallbackRoute.actualPath,
|
|
@@ -12,10 +12,10 @@ class AbstractRouter {
|
|
|
12
12
|
this.strictTrailingSlash = true;
|
|
13
13
|
this.viewTransitionsEnabled = false;
|
|
14
14
|
this.mergeSlashes = false;
|
|
15
|
-
this.trailingSlash = trailingSlash
|
|
15
|
+
this.trailingSlash = trailingSlash ?? false;
|
|
16
16
|
this.strictTrailingSlash = typeof trailingSlash === 'undefined';
|
|
17
|
-
this.mergeSlashes = mergeSlashes
|
|
18
|
-
this.viewTransitionsEnabled = enableViewTransitions
|
|
17
|
+
this.mergeSlashes = mergeSlashes ?? false;
|
|
18
|
+
this.viewTransitionsEnabled = enableViewTransitions ?? false;
|
|
19
19
|
this.hooks = new Map([
|
|
20
20
|
['beforeResolve', new Set(beforeResolve)],
|
|
21
21
|
['beforeNavigate', new Set(beforeNavigate)],
|
|
@@ -39,23 +39,19 @@ class AbstractRouter {
|
|
|
39
39
|
this.started = true;
|
|
40
40
|
}
|
|
41
41
|
getCurrentRoute() {
|
|
42
|
-
var _a, _b, _c;
|
|
43
42
|
// when something will try to get currentRoute while navigating, it will get route which router currently navigating
|
|
44
43
|
// in case some handler supposed to load data of route or similar
|
|
45
|
-
return
|
|
44
|
+
return this.currentNavigation?.to ?? this.lastNavigation?.to;
|
|
46
45
|
}
|
|
47
46
|
getCurrentUrl() {
|
|
48
|
-
var _a, _b, _c;
|
|
49
47
|
// same as getCurrentRoute
|
|
50
|
-
return
|
|
48
|
+
return this.currentNavigation?.url ?? this.lastNavigation?.url;
|
|
51
49
|
}
|
|
52
50
|
getLastRoute() {
|
|
53
|
-
|
|
54
|
-
return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.to;
|
|
51
|
+
return this.lastNavigation?.to;
|
|
55
52
|
}
|
|
56
53
|
getLastUrl() {
|
|
57
|
-
|
|
58
|
-
return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.url;
|
|
54
|
+
return this.lastNavigation?.url;
|
|
59
55
|
}
|
|
60
56
|
commitNavigation(navigation) {
|
|
61
57
|
logger.debug({
|
|
@@ -74,8 +70,7 @@ class AbstractRouter {
|
|
|
74
70
|
return this.internalUpdateCurrentRoute(updateRouteOptions, {});
|
|
75
71
|
}
|
|
76
72
|
async internalUpdateCurrentRoute(updateRouteOptions, { history }) {
|
|
77
|
-
|
|
78
|
-
const prevNavigation = (_a = this.currentNavigation) !== null && _a !== void 0 ? _a : this.lastNavigation;
|
|
73
|
+
const prevNavigation = this.currentNavigation ?? this.lastNavigation;
|
|
79
74
|
if (!prevNavigation) {
|
|
80
75
|
throw new Error('updateCurrentRoute should only be called after navigate to some route');
|
|
81
76
|
}
|
|
@@ -109,16 +104,15 @@ class AbstractRouter {
|
|
|
109
104
|
return this.internalNavigate(makeNavigateOptions(navigateOptions), {});
|
|
110
105
|
}
|
|
111
106
|
async internalNavigate(navigateOptions, { history, redirect }) {
|
|
112
|
-
var _a;
|
|
113
107
|
const { url, replace, params, navigateState, code, viewTransition } = navigateOptions;
|
|
114
108
|
const prevNavigation = redirect
|
|
115
109
|
? this.lastNavigation
|
|
116
|
-
:
|
|
110
|
+
: this.currentNavigation ?? this.lastNavigation;
|
|
117
111
|
if (!url && !prevNavigation) {
|
|
118
112
|
throw new Error('Navigate url should be specified and cannot be omitted for first navigation');
|
|
119
113
|
}
|
|
120
114
|
const resolvedUrl = this.resolveUrl(navigateOptions);
|
|
121
|
-
const { to: from, url: fromUrl } = prevNavigation
|
|
115
|
+
const { to: from, url: fromUrl } = prevNavigation ?? {};
|
|
122
116
|
const redirectFrom = redirect ? this.currentNavigation.to : undefined;
|
|
123
117
|
let navigation = {
|
|
124
118
|
type: 'navigate',
|
|
@@ -199,17 +193,15 @@ class AbstractRouter {
|
|
|
199
193
|
throw new Error('Not implemented');
|
|
200
194
|
}
|
|
201
195
|
addRoute(route) {
|
|
202
|
-
|
|
203
|
-
(_a = this.tree) === null || _a === void 0 ? void 0 : _a.addRoute(route);
|
|
196
|
+
this.tree?.addRoute(route);
|
|
204
197
|
}
|
|
205
198
|
async redirect(navigation, target) {
|
|
206
|
-
var _a;
|
|
207
199
|
logger.debug({
|
|
208
200
|
event: 'redirect',
|
|
209
201
|
navigation,
|
|
210
202
|
target,
|
|
211
203
|
});
|
|
212
|
-
return
|
|
204
|
+
return this.onRedirect?.({
|
|
213
205
|
...navigation,
|
|
214
206
|
from: navigation.to,
|
|
215
207
|
fromUrl: navigation.url,
|
|
@@ -218,12 +210,11 @@ class AbstractRouter {
|
|
|
218
210
|
});
|
|
219
211
|
}
|
|
220
212
|
async notfound(navigation) {
|
|
221
|
-
var _a;
|
|
222
213
|
logger.debug({
|
|
223
214
|
event: 'not-found',
|
|
224
215
|
navigation,
|
|
225
216
|
});
|
|
226
|
-
return
|
|
217
|
+
return this.onNotFound?.(navigation);
|
|
227
218
|
}
|
|
228
219
|
async block(navigation) {
|
|
229
220
|
logger.debug({
|
|
@@ -248,10 +239,9 @@ class AbstractRouter {
|
|
|
248
239
|
return normalized;
|
|
249
240
|
}
|
|
250
241
|
resolveUrl({ url, query = {}, params, preserveQuery, hash }) {
|
|
251
|
-
var _a;
|
|
252
242
|
const currentRoute = this.getCurrentRoute();
|
|
253
243
|
const currentUrl = this.getCurrentUrl();
|
|
254
|
-
const resultUrl = url ? rawResolveUrl(
|
|
244
|
+
const resultUrl = url ? rawResolveUrl(currentUrl?.href ?? '', url) : rawParse(currentUrl.href);
|
|
255
245
|
let { pathname } = resultUrl;
|
|
256
246
|
if (params) {
|
|
257
247
|
if (url) {
|
|
@@ -271,15 +261,14 @@ class AbstractRouter {
|
|
|
271
261
|
...(preserveQuery ? this.getCurrentUrl().query : {}),
|
|
272
262
|
...query,
|
|
273
263
|
},
|
|
274
|
-
hash: hash
|
|
264
|
+
hash: hash ?? resultUrl.hash,
|
|
275
265
|
}));
|
|
276
266
|
}
|
|
277
267
|
resolveRoute({ url, params, navigateState }, { wildcard } = {}) {
|
|
278
|
-
|
|
279
|
-
let route = url ? (_a = this.tree) === null || _a === void 0 ? void 0 : _a.getRoute(url.pathname) : this.getCurrentRoute();
|
|
268
|
+
let route = url ? this.tree?.getRoute(url.pathname) : this.getCurrentRoute();
|
|
280
269
|
if (wildcard && !route && url) {
|
|
281
270
|
// if ordinary route not found look for a wildcard route
|
|
282
|
-
route =
|
|
271
|
+
route = this.tree?.getWildcard(url.pathname);
|
|
283
272
|
}
|
|
284
273
|
if (!route) {
|
|
285
274
|
return;
|
|
@@ -12,10 +12,10 @@ class AbstractRouter {
|
|
|
12
12
|
this.strictTrailingSlash = true;
|
|
13
13
|
this.viewTransitionsEnabled = false;
|
|
14
14
|
this.mergeSlashes = false;
|
|
15
|
-
this.trailingSlash = trailingSlash
|
|
15
|
+
this.trailingSlash = trailingSlash ?? false;
|
|
16
16
|
this.strictTrailingSlash = typeof trailingSlash === 'undefined';
|
|
17
|
-
this.mergeSlashes = mergeSlashes
|
|
18
|
-
this.viewTransitionsEnabled = enableViewTransitions
|
|
17
|
+
this.mergeSlashes = mergeSlashes ?? false;
|
|
18
|
+
this.viewTransitionsEnabled = enableViewTransitions ?? false;
|
|
19
19
|
this.hooks = new Map([
|
|
20
20
|
['beforeResolve', new Set(beforeResolve)],
|
|
21
21
|
['beforeNavigate', new Set(beforeNavigate)],
|
|
@@ -39,23 +39,19 @@ class AbstractRouter {
|
|
|
39
39
|
this.started = true;
|
|
40
40
|
}
|
|
41
41
|
getCurrentRoute() {
|
|
42
|
-
var _a, _b, _c;
|
|
43
42
|
// when something will try to get currentRoute while navigating, it will get route which router currently navigating
|
|
44
43
|
// in case some handler supposed to load data of route or similar
|
|
45
|
-
return
|
|
44
|
+
return this.currentNavigation?.to ?? this.lastNavigation?.to;
|
|
46
45
|
}
|
|
47
46
|
getCurrentUrl() {
|
|
48
|
-
var _a, _b, _c;
|
|
49
47
|
// same as getCurrentRoute
|
|
50
|
-
return
|
|
48
|
+
return this.currentNavigation?.url ?? this.lastNavigation?.url;
|
|
51
49
|
}
|
|
52
50
|
getLastRoute() {
|
|
53
|
-
|
|
54
|
-
return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.to;
|
|
51
|
+
return this.lastNavigation?.to;
|
|
55
52
|
}
|
|
56
53
|
getLastUrl() {
|
|
57
|
-
|
|
58
|
-
return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.url;
|
|
54
|
+
return this.lastNavigation?.url;
|
|
59
55
|
}
|
|
60
56
|
commitNavigation(navigation) {
|
|
61
57
|
logger.debug({
|
|
@@ -74,8 +70,7 @@ class AbstractRouter {
|
|
|
74
70
|
return this.internalUpdateCurrentRoute(updateRouteOptions, {});
|
|
75
71
|
}
|
|
76
72
|
async internalUpdateCurrentRoute(updateRouteOptions, { history }) {
|
|
77
|
-
|
|
78
|
-
const prevNavigation = (_a = this.currentNavigation) !== null && _a !== void 0 ? _a : this.lastNavigation;
|
|
73
|
+
const prevNavigation = this.currentNavigation ?? this.lastNavigation;
|
|
79
74
|
if (!prevNavigation) {
|
|
80
75
|
throw new Error('updateCurrentRoute should only be called after navigate to some route');
|
|
81
76
|
}
|
|
@@ -109,16 +104,15 @@ class AbstractRouter {
|
|
|
109
104
|
return this.internalNavigate(makeNavigateOptions(navigateOptions), {});
|
|
110
105
|
}
|
|
111
106
|
async internalNavigate(navigateOptions, { history, redirect }) {
|
|
112
|
-
var _a;
|
|
113
107
|
const { url, replace, params, navigateState, code, viewTransition } = navigateOptions;
|
|
114
108
|
const prevNavigation = redirect
|
|
115
109
|
? this.lastNavigation
|
|
116
|
-
:
|
|
110
|
+
: this.currentNavigation ?? this.lastNavigation;
|
|
117
111
|
if (!url && !prevNavigation) {
|
|
118
112
|
throw new Error('Navigate url should be specified and cannot be omitted for first navigation');
|
|
119
113
|
}
|
|
120
114
|
const resolvedUrl = this.resolveUrl(navigateOptions);
|
|
121
|
-
const { to: from, url: fromUrl } = prevNavigation
|
|
115
|
+
const { to: from, url: fromUrl } = prevNavigation ?? {};
|
|
122
116
|
const redirectFrom = redirect ? this.currentNavigation.to : undefined;
|
|
123
117
|
let navigation = {
|
|
124
118
|
type: 'navigate',
|
|
@@ -199,17 +193,15 @@ class AbstractRouter {
|
|
|
199
193
|
throw new Error('Not implemented');
|
|
200
194
|
}
|
|
201
195
|
addRoute(route) {
|
|
202
|
-
|
|
203
|
-
(_a = this.tree) === null || _a === void 0 ? void 0 : _a.addRoute(route);
|
|
196
|
+
this.tree?.addRoute(route);
|
|
204
197
|
}
|
|
205
198
|
async redirect(navigation, target) {
|
|
206
|
-
var _a;
|
|
207
199
|
logger.debug({
|
|
208
200
|
event: 'redirect',
|
|
209
201
|
navigation,
|
|
210
202
|
target,
|
|
211
203
|
});
|
|
212
|
-
return
|
|
204
|
+
return this.onRedirect?.({
|
|
213
205
|
...navigation,
|
|
214
206
|
from: navigation.to,
|
|
215
207
|
fromUrl: navigation.url,
|
|
@@ -218,12 +210,11 @@ class AbstractRouter {
|
|
|
218
210
|
});
|
|
219
211
|
}
|
|
220
212
|
async notfound(navigation) {
|
|
221
|
-
var _a;
|
|
222
213
|
logger.debug({
|
|
223
214
|
event: 'not-found',
|
|
224
215
|
navigation,
|
|
225
216
|
});
|
|
226
|
-
return
|
|
217
|
+
return this.onNotFound?.(navigation);
|
|
227
218
|
}
|
|
228
219
|
async block(navigation) {
|
|
229
220
|
logger.debug({
|
|
@@ -248,10 +239,9 @@ class AbstractRouter {
|
|
|
248
239
|
return normalized;
|
|
249
240
|
}
|
|
250
241
|
resolveUrl({ url, query = {}, params, preserveQuery, hash }) {
|
|
251
|
-
var _a;
|
|
252
242
|
const currentRoute = this.getCurrentRoute();
|
|
253
243
|
const currentUrl = this.getCurrentUrl();
|
|
254
|
-
const resultUrl = url ? rawResolveUrl(
|
|
244
|
+
const resultUrl = url ? rawResolveUrl(currentUrl?.href ?? '', url) : rawParse(currentUrl.href);
|
|
255
245
|
let { pathname } = resultUrl;
|
|
256
246
|
if (params) {
|
|
257
247
|
if (url) {
|
|
@@ -271,15 +261,14 @@ class AbstractRouter {
|
|
|
271
261
|
...(preserveQuery ? this.getCurrentUrl().query : {}),
|
|
272
262
|
...query,
|
|
273
263
|
},
|
|
274
|
-
hash: hash
|
|
264
|
+
hash: hash ?? resultUrl.hash,
|
|
275
265
|
}));
|
|
276
266
|
}
|
|
277
267
|
resolveRoute({ url, params, navigateState }, { wildcard } = {}) {
|
|
278
|
-
|
|
279
|
-
let route = url ? (_a = this.tree) === null || _a === void 0 ? void 0 : _a.getRoute(url.pathname) : this.getCurrentRoute();
|
|
268
|
+
let route = url ? this.tree?.getRoute(url.pathname) : this.getCurrentRoute();
|
|
280
269
|
if (wildcard && !route && url) {
|
|
281
270
|
// if ordinary route not found look for a wildcard route
|
|
282
|
-
route =
|
|
271
|
+
route = this.tree?.getWildcard(url.pathname);
|
|
283
272
|
}
|
|
284
273
|
if (!route) {
|
|
285
274
|
return;
|
package/lib/router/abstract.js
CHANGED
|
@@ -21,10 +21,10 @@ class AbstractRouter {
|
|
|
21
21
|
this.strictTrailingSlash = true;
|
|
22
22
|
this.viewTransitionsEnabled = false;
|
|
23
23
|
this.mergeSlashes = false;
|
|
24
|
-
this.trailingSlash = trailingSlash
|
|
24
|
+
this.trailingSlash = trailingSlash ?? false;
|
|
25
25
|
this.strictTrailingSlash = typeof trailingSlash === 'undefined';
|
|
26
|
-
this.mergeSlashes = mergeSlashes
|
|
27
|
-
this.viewTransitionsEnabled = enableViewTransitions
|
|
26
|
+
this.mergeSlashes = mergeSlashes ?? false;
|
|
27
|
+
this.viewTransitionsEnabled = enableViewTransitions ?? false;
|
|
28
28
|
this.hooks = new Map([
|
|
29
29
|
['beforeResolve', new Set(beforeResolve)],
|
|
30
30
|
['beforeNavigate', new Set(beforeNavigate)],
|
|
@@ -48,23 +48,19 @@ class AbstractRouter {
|
|
|
48
48
|
this.started = true;
|
|
49
49
|
}
|
|
50
50
|
getCurrentRoute() {
|
|
51
|
-
var _a, _b, _c;
|
|
52
51
|
// when something will try to get currentRoute while navigating, it will get route which router currently navigating
|
|
53
52
|
// in case some handler supposed to load data of route or similar
|
|
54
|
-
return
|
|
53
|
+
return this.currentNavigation?.to ?? this.lastNavigation?.to;
|
|
55
54
|
}
|
|
56
55
|
getCurrentUrl() {
|
|
57
|
-
var _a, _b, _c;
|
|
58
56
|
// same as getCurrentRoute
|
|
59
|
-
return
|
|
57
|
+
return this.currentNavigation?.url ?? this.lastNavigation?.url;
|
|
60
58
|
}
|
|
61
59
|
getLastRoute() {
|
|
62
|
-
|
|
63
|
-
return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.to;
|
|
60
|
+
return this.lastNavigation?.to;
|
|
64
61
|
}
|
|
65
62
|
getLastUrl() {
|
|
66
|
-
|
|
67
|
-
return (_a = this.lastNavigation) === null || _a === void 0 ? void 0 : _a.url;
|
|
63
|
+
return this.lastNavigation?.url;
|
|
68
64
|
}
|
|
69
65
|
commitNavigation(navigation) {
|
|
70
66
|
logger.logger.debug({
|
|
@@ -83,8 +79,7 @@ class AbstractRouter {
|
|
|
83
79
|
return this.internalUpdateCurrentRoute(updateRouteOptions, {});
|
|
84
80
|
}
|
|
85
81
|
async internalUpdateCurrentRoute(updateRouteOptions, { history }) {
|
|
86
|
-
|
|
87
|
-
const prevNavigation = (_a = this.currentNavigation) !== null && _a !== void 0 ? _a : this.lastNavigation;
|
|
82
|
+
const prevNavigation = this.currentNavigation ?? this.lastNavigation;
|
|
88
83
|
if (!prevNavigation) {
|
|
89
84
|
throw new Error('updateCurrentRoute should only be called after navigate to some route');
|
|
90
85
|
}
|
|
@@ -118,16 +113,15 @@ class AbstractRouter {
|
|
|
118
113
|
return this.internalNavigate(utils.makeNavigateOptions(navigateOptions), {});
|
|
119
114
|
}
|
|
120
115
|
async internalNavigate(navigateOptions, { history, redirect }) {
|
|
121
|
-
var _a;
|
|
122
116
|
const { url, replace, params, navigateState, code, viewTransition } = navigateOptions;
|
|
123
117
|
const prevNavigation = redirect
|
|
124
118
|
? this.lastNavigation
|
|
125
|
-
:
|
|
119
|
+
: this.currentNavigation ?? this.lastNavigation;
|
|
126
120
|
if (!url && !prevNavigation) {
|
|
127
121
|
throw new Error('Navigate url should be specified and cannot be omitted for first navigation');
|
|
128
122
|
}
|
|
129
123
|
const resolvedUrl = this.resolveUrl(navigateOptions);
|
|
130
|
-
const { to: from, url: fromUrl } = prevNavigation
|
|
124
|
+
const { to: from, url: fromUrl } = prevNavigation ?? {};
|
|
131
125
|
const redirectFrom = redirect ? this.currentNavigation.to : undefined;
|
|
132
126
|
let navigation = {
|
|
133
127
|
type: 'navigate',
|
|
@@ -208,17 +202,15 @@ class AbstractRouter {
|
|
|
208
202
|
throw new Error('Not implemented');
|
|
209
203
|
}
|
|
210
204
|
addRoute(route) {
|
|
211
|
-
|
|
212
|
-
(_a = this.tree) === null || _a === void 0 ? void 0 : _a.addRoute(route);
|
|
205
|
+
this.tree?.addRoute(route);
|
|
213
206
|
}
|
|
214
207
|
async redirect(navigation, target) {
|
|
215
|
-
var _a;
|
|
216
208
|
logger.logger.debug({
|
|
217
209
|
event: 'redirect',
|
|
218
210
|
navigation,
|
|
219
211
|
target,
|
|
220
212
|
});
|
|
221
|
-
return
|
|
213
|
+
return this.onRedirect?.({
|
|
222
214
|
...navigation,
|
|
223
215
|
from: navigation.to,
|
|
224
216
|
fromUrl: navigation.url,
|
|
@@ -227,12 +219,11 @@ class AbstractRouter {
|
|
|
227
219
|
});
|
|
228
220
|
}
|
|
229
221
|
async notfound(navigation) {
|
|
230
|
-
var _a;
|
|
231
222
|
logger.logger.debug({
|
|
232
223
|
event: 'not-found',
|
|
233
224
|
navigation,
|
|
234
225
|
});
|
|
235
|
-
return
|
|
226
|
+
return this.onNotFound?.(navigation);
|
|
236
227
|
}
|
|
237
228
|
async block(navigation) {
|
|
238
229
|
logger.logger.debug({
|
|
@@ -257,10 +248,9 @@ class AbstractRouter {
|
|
|
257
248
|
return normalized;
|
|
258
249
|
}
|
|
259
250
|
resolveUrl({ url: url$1, query = {}, params, preserveQuery, hash }) {
|
|
260
|
-
var _a;
|
|
261
251
|
const currentRoute = this.getCurrentRoute();
|
|
262
252
|
const currentUrl = this.getCurrentUrl();
|
|
263
|
-
const resultUrl = url$1 ? url.rawResolveUrl(
|
|
253
|
+
const resultUrl = url$1 ? url.rawResolveUrl(currentUrl?.href ?? '', url$1) : url.rawParse(currentUrl.href);
|
|
264
254
|
let { pathname } = resultUrl;
|
|
265
255
|
if (params) {
|
|
266
256
|
if (url$1) {
|
|
@@ -280,15 +270,14 @@ class AbstractRouter {
|
|
|
280
270
|
...(preserveQuery ? this.getCurrentUrl().query : {}),
|
|
281
271
|
...query,
|
|
282
272
|
},
|
|
283
|
-
hash: hash
|
|
273
|
+
hash: hash ?? resultUrl.hash,
|
|
284
274
|
}));
|
|
285
275
|
}
|
|
286
276
|
resolveRoute({ url, params, navigateState }, { wildcard } = {}) {
|
|
287
|
-
|
|
288
|
-
let route = url ? (_a = this.tree) === null || _a === void 0 ? void 0 : _a.getRoute(url.pathname) : this.getCurrentRoute();
|
|
277
|
+
let route = url ? this.tree?.getRoute(url.pathname) : this.getCurrentRoute();
|
|
289
278
|
if (wildcard && !route && url) {
|
|
290
279
|
// if ordinary route not found look for a wildcard route
|
|
291
|
-
route =
|
|
280
|
+
route = this.tree?.getWildcard(url.pathname);
|
|
292
281
|
}
|
|
293
282
|
if (!route) {
|
|
294
283
|
return;
|
|
@@ -112,10 +112,9 @@ class Router extends ClientRouter {
|
|
|
112
112
|
async resolveIfDelayFound(task) {
|
|
113
113
|
let delayResolve;
|
|
114
114
|
const timer = setInterval(() => {
|
|
115
|
-
var _a;
|
|
116
115
|
if (this.delayedNavigation) {
|
|
117
116
|
if (this.delayedNavigation.type === 'navigate' ||
|
|
118
|
-
this.delayedNavigation.type ===
|
|
117
|
+
this.delayedNavigation.type === this.currentNavigation?.type) {
|
|
119
118
|
logger.info({
|
|
120
119
|
event: 'delay-navigation-found',
|
|
121
120
|
navigation: this.delayedNavigation,
|
|
@@ -160,11 +159,9 @@ class Router extends ClientRouter {
|
|
|
160
159
|
};
|
|
161
160
|
return flatten(navigation)
|
|
162
161
|
.then(() => {
|
|
163
|
-
|
|
164
|
-
(_a = this.delayedResolve) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
162
|
+
this.delayedResolve?.();
|
|
165
163
|
}, (err) => {
|
|
166
|
-
|
|
167
|
-
(_a = this.delayedReject) === null || _a === void 0 ? void 0 : _a.call(this, err);
|
|
164
|
+
this.delayedReject?.(err);
|
|
168
165
|
})
|
|
169
166
|
.finally(() => {
|
|
170
167
|
this.delayedPromise = null;
|
|
@@ -173,11 +170,10 @@ class Router extends ClientRouter {
|
|
|
173
170
|
});
|
|
174
171
|
}
|
|
175
172
|
shouldApplyViewTransition(navigation) {
|
|
176
|
-
var _a, _b;
|
|
177
173
|
const from = navigation.from.actualPath;
|
|
178
174
|
const to = navigation.to.redirect !== undefined
|
|
179
|
-
?
|
|
180
|
-
:
|
|
175
|
+
? this.resolve(navigation.to.redirect)?.actualPath
|
|
176
|
+
: navigation.to?.actualPath;
|
|
181
177
|
// If View Transition enabled to current navigation, save it
|
|
182
178
|
if (navigation.viewTransition) {
|
|
183
179
|
const toPaths = this.appliedViewTransitions.get(from);
|
|
@@ -11,7 +11,6 @@ class ClientRouter extends AbstractRouter {
|
|
|
11
11
|
this.fullRehydrationInProcess = null;
|
|
12
12
|
this.history = new ClientHistory();
|
|
13
13
|
this.history.listen(async ({ type, url, navigateState, replace, history }) => {
|
|
14
|
-
var _a;
|
|
15
14
|
const currentUrl = this.getCurrentUrl();
|
|
16
15
|
const { pathname, query } = this.resolveUrl({ url });
|
|
17
16
|
const isSameUrlNavigation = (currentUrl ? currentUrl.pathname : window.location.pathname) === pathname;
|
|
@@ -34,9 +33,9 @@ class ClientRouter extends AbstractRouter {
|
|
|
34
33
|
return;
|
|
35
34
|
}
|
|
36
35
|
if (type === 'updateCurrentRoute' || (!type && isSameUrlNavigation)) {
|
|
37
|
-
const route =
|
|
36
|
+
const route = this.tree?.getRoute(pathname);
|
|
38
37
|
await this.internalUpdateCurrentRoute({
|
|
39
|
-
params: route
|
|
38
|
+
params: route?.params,
|
|
40
39
|
query,
|
|
41
40
|
replace,
|
|
42
41
|
navigateState,
|
|
@@ -67,7 +66,7 @@ class ClientRouter extends AbstractRouter {
|
|
|
67
66
|
if (this.fullRehydrationInProcess) {
|
|
68
67
|
await this.runHooks('beforeResolve', this.currentNavigation);
|
|
69
68
|
const to = this.resolveRoute({ url }, { wildcard: true });
|
|
70
|
-
const redirect = to
|
|
69
|
+
const redirect = to?.redirect;
|
|
71
70
|
this.currentNavigation.to = to;
|
|
72
71
|
if (redirect) {
|
|
73
72
|
return this.redirect(this.currentNavigation, makeNavigateOptions(redirect));
|
|
@@ -103,10 +102,9 @@ class ClientRouter extends AbstractRouter {
|
|
|
103
102
|
return super.resolveRoute(...options);
|
|
104
103
|
}
|
|
105
104
|
async notfound(navigation) {
|
|
106
|
-
var _a, _b;
|
|
107
105
|
await super.notfound(navigation);
|
|
108
106
|
// in case we didn't find any matched route just force hard page navigation
|
|
109
|
-
const prevUrl =
|
|
107
|
+
const prevUrl = navigation.fromUrl?.href ?? window.location.href;
|
|
110
108
|
const nextUrl = navigation.url.href;
|
|
111
109
|
const isNoSpaNavigation = navigation.from && !navigation.to;
|
|
112
110
|
// prevent redirect cycle on the same page,
|
package/lib/router/client.es.js
CHANGED
|
@@ -11,7 +11,6 @@ class ClientRouter extends AbstractRouter {
|
|
|
11
11
|
this.fullRehydrationInProcess = null;
|
|
12
12
|
this.history = new ClientHistory();
|
|
13
13
|
this.history.listen(async ({ type, url, navigateState, replace, history }) => {
|
|
14
|
-
var _a;
|
|
15
14
|
const currentUrl = this.getCurrentUrl();
|
|
16
15
|
const { pathname, query } = this.resolveUrl({ url });
|
|
17
16
|
const isSameUrlNavigation = (currentUrl ? currentUrl.pathname : window.location.pathname) === pathname;
|
|
@@ -34,9 +33,9 @@ class ClientRouter extends AbstractRouter {
|
|
|
34
33
|
return;
|
|
35
34
|
}
|
|
36
35
|
if (type === 'updateCurrentRoute' || (!type && isSameUrlNavigation)) {
|
|
37
|
-
const route =
|
|
36
|
+
const route = this.tree?.getRoute(pathname);
|
|
38
37
|
await this.internalUpdateCurrentRoute({
|
|
39
|
-
params: route
|
|
38
|
+
params: route?.params,
|
|
40
39
|
query,
|
|
41
40
|
replace,
|
|
42
41
|
navigateState,
|
|
@@ -67,7 +66,7 @@ class ClientRouter extends AbstractRouter {
|
|
|
67
66
|
if (this.fullRehydrationInProcess) {
|
|
68
67
|
await this.runHooks('beforeResolve', this.currentNavigation);
|
|
69
68
|
const to = this.resolveRoute({ url }, { wildcard: true });
|
|
70
|
-
const redirect = to
|
|
69
|
+
const redirect = to?.redirect;
|
|
71
70
|
this.currentNavigation.to = to;
|
|
72
71
|
if (redirect) {
|
|
73
72
|
return this.redirect(this.currentNavigation, makeNavigateOptions(redirect));
|
|
@@ -103,10 +102,9 @@ class ClientRouter extends AbstractRouter {
|
|
|
103
102
|
return super.resolveRoute(...options);
|
|
104
103
|
}
|
|
105
104
|
async notfound(navigation) {
|
|
106
|
-
var _a, _b;
|
|
107
105
|
await super.notfound(navigation);
|
|
108
106
|
// in case we didn't find any matched route just force hard page navigation
|
|
109
|
-
const prevUrl =
|
|
107
|
+
const prevUrl = navigation.fromUrl?.href ?? window.location.href;
|
|
110
108
|
const nextUrl = navigation.url.href;
|
|
111
109
|
const isNoSpaNavigation = navigation.from && !navigation.to;
|
|
112
110
|
// prevent redirect cycle on the same page,
|
package/lib/router/client.js
CHANGED
|
@@ -15,7 +15,6 @@ class ClientRouter extends abstract.AbstractRouter {
|
|
|
15
15
|
this.fullRehydrationInProcess = null;
|
|
16
16
|
this.history = new client.ClientHistory();
|
|
17
17
|
this.history.listen(async ({ type, url, navigateState, replace, history }) => {
|
|
18
|
-
var _a;
|
|
19
18
|
const currentUrl = this.getCurrentUrl();
|
|
20
19
|
const { pathname, query } = this.resolveUrl({ url });
|
|
21
20
|
const isSameUrlNavigation = (currentUrl ? currentUrl.pathname : window.location.pathname) === pathname;
|
|
@@ -38,9 +37,9 @@ class ClientRouter extends abstract.AbstractRouter {
|
|
|
38
37
|
return;
|
|
39
38
|
}
|
|
40
39
|
if (type === 'updateCurrentRoute' || (!type && isSameUrlNavigation)) {
|
|
41
|
-
const route =
|
|
40
|
+
const route = this.tree?.getRoute(pathname);
|
|
42
41
|
await this.internalUpdateCurrentRoute({
|
|
43
|
-
params: route
|
|
42
|
+
params: route?.params,
|
|
44
43
|
query,
|
|
45
44
|
replace,
|
|
46
45
|
navigateState,
|
|
@@ -71,7 +70,7 @@ class ClientRouter extends abstract.AbstractRouter {
|
|
|
71
70
|
if (this.fullRehydrationInProcess) {
|
|
72
71
|
await this.runHooks('beforeResolve', this.currentNavigation);
|
|
73
72
|
const to = this.resolveRoute({ url: url$1 }, { wildcard: true });
|
|
74
|
-
const redirect = to
|
|
73
|
+
const redirect = to?.redirect;
|
|
75
74
|
this.currentNavigation.to = to;
|
|
76
75
|
if (redirect) {
|
|
77
76
|
return this.redirect(this.currentNavigation, utils.makeNavigateOptions(redirect));
|
|
@@ -107,10 +106,9 @@ class ClientRouter extends abstract.AbstractRouter {
|
|
|
107
106
|
return super.resolveRoute(...options);
|
|
108
107
|
}
|
|
109
108
|
async notfound(navigation) {
|
|
110
|
-
var _a, _b;
|
|
111
109
|
await super.notfound(navigation);
|
|
112
110
|
// in case we didn't find any matched route just force hard page navigation
|
|
113
|
-
const prevUrl =
|
|
111
|
+
const prevUrl = navigation.fromUrl?.href ?? window.location.href;
|
|
114
112
|
const nextUrl = navigation.url.href;
|
|
115
113
|
const isNoSpaNavigation = navigation.from && !navigation.to;
|
|
116
114
|
// prevent redirect cycle on the same page,
|
package/lib/router/server.es.js
CHANGED
|
@@ -6,11 +6,10 @@ import { logger } from '../logger.es.js';
|
|
|
6
6
|
|
|
7
7
|
class Router extends AbstractRouter {
|
|
8
8
|
constructor(options) {
|
|
9
|
-
var _a;
|
|
10
9
|
super(options);
|
|
11
10
|
this.blocked = false;
|
|
12
11
|
this.tree = new RouteTree(options.routes);
|
|
13
|
-
this.defaultRedirectCode =
|
|
12
|
+
this.defaultRedirectCode = options.defaultRedirectCode ?? 308;
|
|
14
13
|
this.history = new ServerHistory();
|
|
15
14
|
}
|
|
16
15
|
async dehydrate() {
|
package/lib/router/server.js
CHANGED
|
@@ -10,11 +10,10 @@ var logger = require('../logger.js');
|
|
|
10
10
|
|
|
11
11
|
class Router extends abstract.AbstractRouter {
|
|
12
12
|
constructor(options) {
|
|
13
|
-
var _a;
|
|
14
13
|
super(options);
|
|
15
14
|
this.blocked = false;
|
|
16
15
|
this.tree = new tree.RouteTree(options.routes);
|
|
17
|
-
this.defaultRedirectCode =
|
|
16
|
+
this.defaultRedirectCode = options.defaultRedirectCode ?? 308;
|
|
18
17
|
this.history = new server.ServerHistory();
|
|
19
18
|
}
|
|
20
19
|
async dehydrate() {
|
package/lib/tree/tree.browser.js
CHANGED
|
@@ -15,7 +15,7 @@ const createNavigationRoute = (route, pathname, params) => {
|
|
|
15
15
|
return {
|
|
16
16
|
...route,
|
|
17
17
|
actualPath: pathname,
|
|
18
|
-
params: params
|
|
18
|
+
params: params ?? {},
|
|
19
19
|
};
|
|
20
20
|
};
|
|
21
21
|
class RouteTree {
|
|
@@ -131,8 +131,8 @@ class RouteTree {
|
|
|
131
131
|
for (let i = parameters.length - 1; i >= 0; i--) {
|
|
132
132
|
const param = parameters[i];
|
|
133
133
|
const { paramName, tree, regexp } = param;
|
|
134
|
-
const match = regexp
|
|
135
|
-
const paramValue = regexp ? match
|
|
134
|
+
const match = regexp?.exec(part);
|
|
135
|
+
const paramValue = regexp ? match?.[1] : part;
|
|
136
136
|
if (paramValue) {
|
|
137
137
|
queue.push([tree, index + 1, { ...params, [paramName]: paramValue }]);
|
|
138
138
|
}
|
package/lib/tree/tree.es.js
CHANGED
|
@@ -15,7 +15,7 @@ const createNavigationRoute = (route, pathname, params) => {
|
|
|
15
15
|
return {
|
|
16
16
|
...route,
|
|
17
17
|
actualPath: pathname,
|
|
18
|
-
params: params
|
|
18
|
+
params: params ?? {},
|
|
19
19
|
};
|
|
20
20
|
};
|
|
21
21
|
class RouteTree {
|
|
@@ -131,8 +131,8 @@ class RouteTree {
|
|
|
131
131
|
for (let i = parameters.length - 1; i >= 0; i--) {
|
|
132
132
|
const param = parameters[i];
|
|
133
133
|
const { paramName, tree, regexp } = param;
|
|
134
|
-
const match = regexp
|
|
135
|
-
const paramValue = regexp ? match
|
|
134
|
+
const match = regexp?.exec(part);
|
|
135
|
+
const paramValue = regexp ? match?.[1] : part;
|
|
136
136
|
if (paramValue) {
|
|
137
137
|
queue.push([tree, index + 1, { ...params, [paramName]: paramValue }]);
|
|
138
138
|
}
|
package/lib/tree/tree.js
CHANGED
|
@@ -25,7 +25,7 @@ const createNavigationRoute = (route, pathname, params) => {
|
|
|
25
25
|
return {
|
|
26
26
|
...route,
|
|
27
27
|
actualPath: pathname,
|
|
28
|
-
params: params
|
|
28
|
+
params: params ?? {},
|
|
29
29
|
};
|
|
30
30
|
};
|
|
31
31
|
class RouteTree {
|
|
@@ -141,8 +141,8 @@ class RouteTree {
|
|
|
141
141
|
for (let i = parameters.length - 1; i >= 0; i--) {
|
|
142
142
|
const param = parameters[i];
|
|
143
143
|
const { paramName, tree, regexp } = param;
|
|
144
|
-
const match = regexp
|
|
145
|
-
const paramValue = regexp ? match
|
|
144
|
+
const match = regexp?.exec(part);
|
|
145
|
+
const paramValue = regexp ? match?.[1] : part;
|
|
146
146
|
if (paramValue) {
|
|
147
147
|
queue.push([tree, index + 1, { ...params, [paramName]: paramValue }]);
|
|
148
148
|
}
|
|
@@ -46,7 +46,6 @@ const parse = (part) => {
|
|
|
46
46
|
const makePath = (pathname, params) => {
|
|
47
47
|
const parts = getParts(pathname);
|
|
48
48
|
const result = map((part) => {
|
|
49
|
-
var _a;
|
|
50
49
|
if (isHistoryFallback(part) || isWildcard(part)) {
|
|
51
50
|
throw new Error(`Pathname should be only a string with dynamic parameters, not a special string, got ${pathname}`);
|
|
52
51
|
}
|
|
@@ -57,7 +56,7 @@ const makePath = (pathname, params) => {
|
|
|
57
56
|
throw new Error('parameters should satisfy pattern "prefix:paramName(regexp)\\?:postfix"');
|
|
58
57
|
}
|
|
59
58
|
const [, paramName, regexp, optional] = match;
|
|
60
|
-
const value =
|
|
59
|
+
const value = params[paramName]?.toString();
|
|
61
60
|
if (optional && !value) {
|
|
62
61
|
return '';
|
|
63
62
|
}
|
package/lib/tree/utils.es.js
CHANGED
|
@@ -46,7 +46,6 @@ const parse = (part) => {
|
|
|
46
46
|
const makePath = (pathname, params) => {
|
|
47
47
|
const parts = getParts(pathname);
|
|
48
48
|
const result = map((part) => {
|
|
49
|
-
var _a;
|
|
50
49
|
if (isHistoryFallback(part) || isWildcard(part)) {
|
|
51
50
|
throw new Error(`Pathname should be only a string with dynamic parameters, not a special string, got ${pathname}`);
|
|
52
51
|
}
|
|
@@ -57,7 +56,7 @@ const makePath = (pathname, params) => {
|
|
|
57
56
|
throw new Error('parameters should satisfy pattern "prefix:paramName(regexp)\\?:postfix"');
|
|
58
57
|
}
|
|
59
58
|
const [, paramName, regexp, optional] = match;
|
|
60
|
-
const value =
|
|
59
|
+
const value = params[paramName]?.toString();
|
|
61
60
|
if (optional && !value) {
|
|
62
61
|
return '';
|
|
63
62
|
}
|
package/lib/tree/utils.js
CHANGED
|
@@ -54,7 +54,6 @@ const parse = (part) => {
|
|
|
54
54
|
const makePath = (pathname, params) => {
|
|
55
55
|
const parts = getParts(pathname);
|
|
56
56
|
const result = map__default["default"]((part) => {
|
|
57
|
-
var _a;
|
|
58
57
|
if (isHistoryFallback(part) || isWildcard(part)) {
|
|
59
58
|
throw new Error(`Pathname should be only a string with dynamic parameters, not a special string, got ${pathname}`);
|
|
60
59
|
}
|
|
@@ -65,7 +64,7 @@ const makePath = (pathname, params) => {
|
|
|
65
64
|
throw new Error('parameters should satisfy pattern "prefix:paramName(regexp)\\?:postfix"');
|
|
66
65
|
}
|
|
67
66
|
const [, paramName, regexp, optional] = match;
|
|
68
|
-
const value =
|
|
67
|
+
const value = params[paramName]?.toString();
|
|
69
68
|
if (optional && !value) {
|
|
70
69
|
return '';
|
|
71
70
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinkoff/router",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.46",
|
|
4
4
|
"description": "router",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"use-sync-external-store": "^1.2.0"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@tramvai/core": "5.
|
|
30
|
+
"@tramvai/core": "5.14.9",
|
|
31
31
|
"react": ">=16.14.0",
|
|
32
32
|
"tslib": "^2.4.0"
|
|
33
33
|
},
|