@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.
@@ -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?: PathRecord;
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?: PathRecord;
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];
@@ -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
- return this._history.replace(routeRedirect, {
131
- redirectedFrom: routeRedirect
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
- if (to.replace) {
174
- this._history.replace(to.path, {
175
- redirectedFrom: current,
176
- skipGuards: to.skipGuards,
177
- state: to.state
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.push(to, { redirectedFrom: current });
186
+ this._history[transitionMethod](to, {
187
+ redirectedFrom: redirectedFrom || nextRoute
188
+ });
190
189
  }
191
190
  }
192
191
  else {
@@ -95,7 +95,7 @@ export interface PartialPath {
95
95
  *
96
96
  */
97
97
  export interface Location<S extends State = State> extends Path {
98
- redirectedFrom?: PathRecord;
98
+ redirectedFrom?: Path;
99
99
  /**
100
100
  * An object of arbitrary data associated with this location.
101
101
  *
@@ -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?: PathRecord;
6
+ redirectedFrom?: Path;
7
7
  }): Location<any>;
8
8
  export declare function pushState(state: HistoryState, url?: string, { replace }?: {
9
9
  replace?: boolean;
@@ -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?: PathRecord;
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?: PathRecord;
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];
@@ -74,7 +74,7 @@ class BaseHistory {
74
74
  url: this.resolve(nextLocation).href
75
75
  });
76
76
  this._updateState(action);
77
- }, onAbort, skipGuards);
77
+ }, onAbort, skipGuards, action === exports.ACTION_REPLACE, redirectedFrom);
78
78
  }
79
79
  _updateState(nextAction) {
80
80
  // update state
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
- return this._history.replace(routeRedirect, {
134
- redirectedFrom: routeRedirect
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
- if (to.replace) {
177
- this._history.replace(to.path, {
178
- redirectedFrom: current,
179
- skipGuards: to.skipGuards,
180
- state: to.state
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.push(to, { redirectedFrom: current });
189
+ this._history[transitionMethod](to, {
190
+ redirectedFrom: redirectedFrom || nextRoute
191
+ });
193
192
  }
194
193
  }
195
194
  else {
@@ -95,7 +95,7 @@ export interface PartialPath {
95
95
  *
96
96
  */
97
97
  export interface Location<S extends State = State> extends Path {
98
- redirectedFrom?: PathRecord;
98
+ redirectedFrom?: Path;
99
99
  /**
100
100
  * An object of arbitrary data associated with this location.
101
101
  *
@@ -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?: PathRecord;
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.39",
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.39",
31
+ "@shuvi/utils": "1.0.42",
32
32
  "query-string": "6.13.8"
33
33
  }
34
34
  }