@shuvi/router 1.0.39 → 1.0.42
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/esm/history/base.d.ts +4 -4
- package/esm/history/base.js +1 -1
- package/esm/router.js +18 -19
- package/esm/types/history.d.ts +1 -1
- package/esm/utils/history.d.ts +2 -2
- package/lib/history/base.d.ts +4 -4
- package/lib/history/base.js +1 -1
- package/lib/router.js +18 -19
- package/lib/types/history.d.ts +1 -1
- package/lib/utils/history.d.ts +2 -2
- package/package.json +2 -2
package/esm/history/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { State, HistoryState, Blocker, Location, ResolvedPath, PathRecord, Action } from '../types';
|
|
1
|
+
import { State, HistoryState, Blocker, Location, ResolvedPath, PathRecord, Path, Action } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* A POP indicates a change to an arbitrary index in the history stack, such
|
|
4
4
|
* as a back or forward navigation. It does not describe the direction of the
|
|
@@ -21,7 +21,7 @@ export declare const ACTION_REPLACE: Action;
|
|
|
21
21
|
export interface TransitionOptions {
|
|
22
22
|
state?: State;
|
|
23
23
|
action?: Action;
|
|
24
|
-
redirectedFrom?:
|
|
24
|
+
redirectedFrom?: Path;
|
|
25
25
|
/**
|
|
26
26
|
* skipGuards means this route transition will be straightforwardly executed without any before guard
|
|
27
27
|
*/
|
|
@@ -35,13 +35,13 @@ export interface TransitionOptions {
|
|
|
35
35
|
}
|
|
36
36
|
export interface PushOptions {
|
|
37
37
|
state?: object | null | undefined;
|
|
38
|
-
redirectedFrom?:
|
|
38
|
+
redirectedFrom?: Path;
|
|
39
39
|
skipGuards?: boolean;
|
|
40
40
|
}
|
|
41
41
|
export default abstract class BaseHistory {
|
|
42
42
|
action: Action;
|
|
43
43
|
location: Location;
|
|
44
|
-
doTransition: (to: PathRecord, onComplete: Function, onAbort?: Function, skipGuards?: boolean) => void;
|
|
44
|
+
doTransition: (to: PathRecord, onComplete: Function, onAbort?: Function, skipGuards?: boolean, isReplace?: boolean, redirectedFrom?: Path) => void;
|
|
45
45
|
protected _index: number;
|
|
46
46
|
protected _blockers: import("../utils").Events<Blocker<State>>;
|
|
47
47
|
protected abstract getIndexAndLocation(): [number, Location];
|
package/esm/history/base.js
CHANGED
|
@@ -71,7 +71,7 @@ export default class BaseHistory {
|
|
|
71
71
|
url: this.resolve(nextLocation).href
|
|
72
72
|
});
|
|
73
73
|
this._updateState(action);
|
|
74
|
-
}, onAbort, skipGuards);
|
|
74
|
+
}, onAbort, skipGuards, action === ACTION_REPLACE, redirectedFrom);
|
|
75
75
|
}
|
|
76
76
|
_updateState(nextAction) {
|
|
77
77
|
// update state
|
package/esm/router.js
CHANGED
|
@@ -121,14 +121,16 @@ class Router {
|
|
|
121
121
|
5. Emit change event(trigger react update)
|
|
122
122
|
6. Call router.afterEach
|
|
123
123
|
*/
|
|
124
|
-
_doTransition(to, onComplete, onAbort, skipGuards) {
|
|
124
|
+
_doTransition(to, onComplete, onAbort, skipGuards, isReplace, redirectedFrom) {
|
|
125
125
|
const nextRoute = this._getNextRoute(to);
|
|
126
126
|
const current = this._current;
|
|
127
127
|
const nextMatches = nextRoute.matches;
|
|
128
128
|
const routeRedirect = getRedirectFromRoutes(nextMatches);
|
|
129
|
+
const isInitialNavigation = current === START;
|
|
129
130
|
if (routeRedirect) {
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
const transitionMethod = isReplace || isInitialNavigation ? 'replace' : 'push';
|
|
132
|
+
return this._history[transitionMethod](routeRedirect, {
|
|
133
|
+
redirectedFrom: redirectedFrom || nextRoute
|
|
132
134
|
});
|
|
133
135
|
}
|
|
134
136
|
const routeContext = new Map();
|
|
@@ -144,7 +146,7 @@ class Router {
|
|
|
144
146
|
this._cancleHandler = null;
|
|
145
147
|
onAbort && onAbort();
|
|
146
148
|
// fire ready cbs once
|
|
147
|
-
if (!this._ready) {
|
|
149
|
+
if (!this._ready && this._current !== START) {
|
|
148
150
|
this._ready = true;
|
|
149
151
|
this._readyDefer.resolve();
|
|
150
152
|
}
|
|
@@ -169,24 +171,21 @@ class Router {
|
|
|
169
171
|
else if (typeof to === 'string' ||
|
|
170
172
|
(typeof to === 'object' && typeof to.path === 'string')) {
|
|
171
173
|
abort();
|
|
174
|
+
const useReplace = isReplace ||
|
|
175
|
+
(typeof to === 'object' && to.replace) ||
|
|
176
|
+
isInitialNavigation;
|
|
177
|
+
const transitionMethod = useReplace ? 'replace' : 'push';
|
|
172
178
|
if (typeof to === 'object') {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
this._history.push(to.path, {
|
|
182
|
-
redirectedFrom: current,
|
|
183
|
-
skipGuards: to.skipGuards,
|
|
184
|
-
state: to.state
|
|
185
|
-
});
|
|
186
|
-
}
|
|
179
|
+
this._history[transitionMethod](to.path, {
|
|
180
|
+
redirectedFrom: redirectedFrom || nextRoute,
|
|
181
|
+
skipGuards: to.skipGuards,
|
|
182
|
+
state: to.state
|
|
183
|
+
});
|
|
187
184
|
}
|
|
188
185
|
else {
|
|
189
|
-
this._history
|
|
186
|
+
this._history[transitionMethod](to, {
|
|
187
|
+
redirectedFrom: redirectedFrom || nextRoute
|
|
188
|
+
});
|
|
190
189
|
}
|
|
191
190
|
}
|
|
192
191
|
else {
|
package/esm/types/history.d.ts
CHANGED
package/esm/utils/history.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { HistoryState, Location, PathRecord, State, Key, Blocker } from '../types';
|
|
1
|
+
import { HistoryState, Location, PathRecord, State, Key, Blocker, Path } from '../types';
|
|
2
2
|
import { Events } from './misc';
|
|
3
3
|
export declare function createLocation(to: PathRecord, { state, key, redirectedFrom }?: {
|
|
4
4
|
state?: State;
|
|
5
5
|
key?: Key;
|
|
6
|
-
redirectedFrom?:
|
|
6
|
+
redirectedFrom?: Path;
|
|
7
7
|
}): Location<any>;
|
|
8
8
|
export declare function pushState(state: HistoryState, url?: string, { replace }?: {
|
|
9
9
|
replace?: boolean;
|
package/lib/history/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { State, HistoryState, Blocker, Location, ResolvedPath, PathRecord, Action } from '../types';
|
|
1
|
+
import { State, HistoryState, Blocker, Location, ResolvedPath, PathRecord, Path, Action } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* A POP indicates a change to an arbitrary index in the history stack, such
|
|
4
4
|
* as a back or forward navigation. It does not describe the direction of the
|
|
@@ -21,7 +21,7 @@ export declare const ACTION_REPLACE: Action;
|
|
|
21
21
|
export interface TransitionOptions {
|
|
22
22
|
state?: State;
|
|
23
23
|
action?: Action;
|
|
24
|
-
redirectedFrom?:
|
|
24
|
+
redirectedFrom?: Path;
|
|
25
25
|
/**
|
|
26
26
|
* skipGuards means this route transition will be straightforwardly executed without any before guard
|
|
27
27
|
*/
|
|
@@ -35,13 +35,13 @@ export interface TransitionOptions {
|
|
|
35
35
|
}
|
|
36
36
|
export interface PushOptions {
|
|
37
37
|
state?: object | null | undefined;
|
|
38
|
-
redirectedFrom?:
|
|
38
|
+
redirectedFrom?: Path;
|
|
39
39
|
skipGuards?: boolean;
|
|
40
40
|
}
|
|
41
41
|
export default abstract class BaseHistory {
|
|
42
42
|
action: Action;
|
|
43
43
|
location: Location;
|
|
44
|
-
doTransition: (to: PathRecord, onComplete: Function, onAbort?: Function, skipGuards?: boolean) => void;
|
|
44
|
+
doTransition: (to: PathRecord, onComplete: Function, onAbort?: Function, skipGuards?: boolean, isReplace?: boolean, redirectedFrom?: Path) => void;
|
|
45
45
|
protected _index: number;
|
|
46
46
|
protected _blockers: import("../utils").Events<Blocker<State>>;
|
|
47
47
|
protected abstract getIndexAndLocation(): [number, Location];
|
package/lib/history/base.js
CHANGED
package/lib/router.js
CHANGED
|
@@ -124,14 +124,16 @@ class Router {
|
|
|
124
124
|
5. Emit change event(trigger react update)
|
|
125
125
|
6. Call router.afterEach
|
|
126
126
|
*/
|
|
127
|
-
_doTransition(to, onComplete, onAbort, skipGuards) {
|
|
127
|
+
_doTransition(to, onComplete, onAbort, skipGuards, isReplace, redirectedFrom) {
|
|
128
128
|
const nextRoute = this._getNextRoute(to);
|
|
129
129
|
const current = this._current;
|
|
130
130
|
const nextMatches = nextRoute.matches;
|
|
131
131
|
const routeRedirect = (0, getRedirectFromRoutes_1.getRedirectFromRoutes)(nextMatches);
|
|
132
|
+
const isInitialNavigation = current === START;
|
|
132
133
|
if (routeRedirect) {
|
|
133
|
-
|
|
134
|
-
|
|
134
|
+
const transitionMethod = isReplace || isInitialNavigation ? 'replace' : 'push';
|
|
135
|
+
return this._history[transitionMethod](routeRedirect, {
|
|
136
|
+
redirectedFrom: redirectedFrom || nextRoute
|
|
135
137
|
});
|
|
136
138
|
}
|
|
137
139
|
const routeContext = new Map();
|
|
@@ -147,7 +149,7 @@ class Router {
|
|
|
147
149
|
this._cancleHandler = null;
|
|
148
150
|
onAbort && onAbort();
|
|
149
151
|
// fire ready cbs once
|
|
150
|
-
if (!this._ready) {
|
|
152
|
+
if (!this._ready && this._current !== START) {
|
|
151
153
|
this._ready = true;
|
|
152
154
|
this._readyDefer.resolve();
|
|
153
155
|
}
|
|
@@ -172,24 +174,21 @@ class Router {
|
|
|
172
174
|
else if (typeof to === 'string' ||
|
|
173
175
|
(typeof to === 'object' && typeof to.path === 'string')) {
|
|
174
176
|
abort();
|
|
177
|
+
const useReplace = isReplace ||
|
|
178
|
+
(typeof to === 'object' && to.replace) ||
|
|
179
|
+
isInitialNavigation;
|
|
180
|
+
const transitionMethod = useReplace ? 'replace' : 'push';
|
|
175
181
|
if (typeof to === 'object') {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
this._history.push(to.path, {
|
|
185
|
-
redirectedFrom: current,
|
|
186
|
-
skipGuards: to.skipGuards,
|
|
187
|
-
state: to.state
|
|
188
|
-
});
|
|
189
|
-
}
|
|
182
|
+
this._history[transitionMethod](to.path, {
|
|
183
|
+
redirectedFrom: redirectedFrom || nextRoute,
|
|
184
|
+
skipGuards: to.skipGuards,
|
|
185
|
+
state: to.state
|
|
186
|
+
});
|
|
190
187
|
}
|
|
191
188
|
else {
|
|
192
|
-
this._history
|
|
189
|
+
this._history[transitionMethod](to, {
|
|
190
|
+
redirectedFrom: redirectedFrom || nextRoute
|
|
191
|
+
});
|
|
193
192
|
}
|
|
194
193
|
}
|
|
195
194
|
else {
|
package/lib/types/history.d.ts
CHANGED
package/lib/utils/history.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { HistoryState, Location, PathRecord, State, Key, Blocker } from '../types';
|
|
1
|
+
import { HistoryState, Location, PathRecord, State, Key, Blocker, Path } from '../types';
|
|
2
2
|
import { Events } from './misc';
|
|
3
3
|
export declare function createLocation(to: PathRecord, { state, key, redirectedFrom }?: {
|
|
4
4
|
state?: State;
|
|
5
5
|
key?: Key;
|
|
6
|
-
redirectedFrom?:
|
|
6
|
+
redirectedFrom?: Path;
|
|
7
7
|
}): Location<any>;
|
|
8
8
|
export declare function pushState(state: HistoryState, url?: string, { replace }?: {
|
|
9
9
|
replace?: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shuvi/router",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.42",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/shuvijs/shuvi.git",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"node": ">= 16.0.0"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@shuvi/utils": "1.0.
|
|
31
|
+
"@shuvi/utils": "1.0.42",
|
|
32
32
|
"query-string": "6.13.8"
|
|
33
33
|
}
|
|
34
34
|
}
|