@tinkoff/router 0.2.8 → 0.2.10

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,12 +1,14 @@
1
1
  import { parse } from '@tinkoff/url';
2
2
  import { AbstractRouter } from './abstract.browser.js';
3
- import { isSameHost } from '../utils.browser.js';
3
+ import { makeNavigateOptions, isSameHost } from '../utils.browser.js';
4
4
  import { logger } from '../logger.browser.js';
5
5
  import { ClientHistory } from '../history/client.browser.js';
6
6
 
7
7
  class ClientRouter extends AbstractRouter {
8
8
  constructor(options) {
9
9
  super(options);
10
+ // this flag for cases when we don't have initial router state from server - CSR fallback initialization
11
+ this.fullRehydrationInProcess = null;
10
12
  this.history = new ClientHistory();
11
13
  this.history.listen(async ({ type, url, navigateState, replace, history }) => {
12
14
  var _a;
@@ -32,8 +34,7 @@ class ClientRouter extends AbstractRouter {
32
34
  });
33
35
  }
34
36
  async rehydrate(navigation) {
35
- // this flag for cases when we don't have initial router state from server - CSR fallback for example
36
- const fullRehydration = !navigation.to;
37
+ this.fullRehydrationInProcess = !navigation.to;
37
38
  logger.debug({
38
39
  event: 'rehydrate',
39
40
  navigation,
@@ -45,21 +46,25 @@ class ClientRouter extends AbstractRouter {
45
46
  url,
46
47
  };
47
48
  this.lastNavigation = this.currentNavigation;
48
- if (fullRehydration) {
49
+ if (this.fullRehydrationInProcess) {
49
50
  await this.runHooks('beforeResolve', this.currentNavigation);
50
51
  const to = this.resolveRoute({ url }, { wildcard: true });
52
+ const redirect = to === null || to === void 0 ? void 0 : to.redirect;
51
53
  this.currentNavigation.to = to;
54
+ if (redirect) {
55
+ return this.redirect(this.currentNavigation, makeNavigateOptions(redirect));
56
+ }
52
57
  }
53
58
  // rerun guard check in case it differs from server side
54
59
  await this.runGuards(this.currentNavigation);
55
60
  // and init any history listeners
56
61
  this.history.init(this.currentNavigation);
57
- if (fullRehydration) {
62
+ if (this.fullRehydrationInProcess) {
58
63
  this.runSyncHooks('change', this.currentNavigation);
59
64
  }
60
65
  this.currentNavigation = null;
61
66
  // add dehydrated route to tree to prevent its loading
62
- if (!fullRehydration) {
67
+ if (!this.fullRehydrationInProcess) {
63
68
  this.addRoute({
64
69
  name: navigation.to.name,
65
70
  path: navigation.to.path,
@@ -69,6 +74,7 @@ class ClientRouter extends AbstractRouter {
69
74
  alias: url.pathname,
70
75
  });
71
76
  }
77
+ this.fullRehydrationInProcess = null;
72
78
  }
73
79
  resolveRoute(...options) {
74
80
  const { url } = options[0];
@@ -96,6 +102,10 @@ class ClientRouter extends AbstractRouter {
96
102
  window.location.assign(nextUrl);
97
103
  }
98
104
  }
105
+ else if (this.onBlock) {
106
+ // last resort case for CSR fallback
107
+ return this.onBlock(navigation);
108
+ }
99
109
  // prevent routing from any continues navigation returning promise which will be not resolved
100
110
  return new Promise(() => { });
101
111
  }
@@ -104,6 +114,13 @@ class ClientRouter extends AbstractRouter {
104
114
  }
105
115
  async redirect(navigation, target) {
106
116
  await super.redirect(navigation, target);
117
+ // on CSR fallback initialization, if we found a redirect,
118
+ // we need to make hard reload for prevent current page rendering
119
+ if (this.fullRehydrationInProcess) {
120
+ window.location.replace(target.url);
121
+ // prevent routing from any continues navigation returning promise which will be not resolved
122
+ return new Promise(() => { });
123
+ }
107
124
  return this.internalNavigate({
108
125
  ...target,
109
126
  replace: target.replace || navigation.replace,
@@ -2,6 +2,7 @@ import type { Options } from './abstract';
2
2
  import { AbstractRouter } from './abstract';
3
3
  import type { Navigation, NavigateOptions } from '../types';
4
4
  export declare abstract class ClientRouter extends AbstractRouter {
5
+ protected fullRehydrationInProcess: boolean;
5
6
  constructor(options: Options);
6
7
  rehydrate(navigation: Navigation): Promise<void>;
7
8
  protected resolveRoute(...options: Parameters<AbstractRouter['resolveRoute']>): import("../types").NavigationRoute;
@@ -1,12 +1,14 @@
1
1
  import { parse } from '@tinkoff/url';
2
2
  import { AbstractRouter } from './abstract.es.js';
3
- import { isSameHost } from '../utils.es.js';
3
+ import { makeNavigateOptions, isSameHost } from '../utils.es.js';
4
4
  import { logger } from '../logger.es.js';
5
5
  import { ClientHistory } from '../history/client.es.js';
6
6
 
7
7
  class ClientRouter extends AbstractRouter {
8
8
  constructor(options) {
9
9
  super(options);
10
+ // this flag for cases when we don't have initial router state from server - CSR fallback initialization
11
+ this.fullRehydrationInProcess = null;
10
12
  this.history = new ClientHistory();
11
13
  this.history.listen(async ({ type, url, navigateState, replace, history }) => {
12
14
  var _a;
@@ -32,8 +34,7 @@ class ClientRouter extends AbstractRouter {
32
34
  });
33
35
  }
34
36
  async rehydrate(navigation) {
35
- // this flag for cases when we don't have initial router state from server - CSR fallback for example
36
- const fullRehydration = !navigation.to;
37
+ this.fullRehydrationInProcess = !navigation.to;
37
38
  logger.debug({
38
39
  event: 'rehydrate',
39
40
  navigation,
@@ -45,21 +46,25 @@ class ClientRouter extends AbstractRouter {
45
46
  url,
46
47
  };
47
48
  this.lastNavigation = this.currentNavigation;
48
- if (fullRehydration) {
49
+ if (this.fullRehydrationInProcess) {
49
50
  await this.runHooks('beforeResolve', this.currentNavigation);
50
51
  const to = this.resolveRoute({ url }, { wildcard: true });
52
+ const redirect = to === null || to === void 0 ? void 0 : to.redirect;
51
53
  this.currentNavigation.to = to;
54
+ if (redirect) {
55
+ return this.redirect(this.currentNavigation, makeNavigateOptions(redirect));
56
+ }
52
57
  }
53
58
  // rerun guard check in case it differs from server side
54
59
  await this.runGuards(this.currentNavigation);
55
60
  // and init any history listeners
56
61
  this.history.init(this.currentNavigation);
57
- if (fullRehydration) {
62
+ if (this.fullRehydrationInProcess) {
58
63
  this.runSyncHooks('change', this.currentNavigation);
59
64
  }
60
65
  this.currentNavigation = null;
61
66
  // add dehydrated route to tree to prevent its loading
62
- if (!fullRehydration) {
67
+ if (!this.fullRehydrationInProcess) {
63
68
  this.addRoute({
64
69
  name: navigation.to.name,
65
70
  path: navigation.to.path,
@@ -69,6 +74,7 @@ class ClientRouter extends AbstractRouter {
69
74
  alias: url.pathname,
70
75
  });
71
76
  }
77
+ this.fullRehydrationInProcess = null;
72
78
  }
73
79
  resolveRoute(...options) {
74
80
  const { url } = options[0];
@@ -96,6 +102,10 @@ class ClientRouter extends AbstractRouter {
96
102
  window.location.assign(nextUrl);
97
103
  }
98
104
  }
105
+ else if (this.onBlock) {
106
+ // last resort case for CSR fallback
107
+ return this.onBlock(navigation);
108
+ }
99
109
  // prevent routing from any continues navigation returning promise which will be not resolved
100
110
  return new Promise(() => { });
101
111
  }
@@ -104,6 +114,13 @@ class ClientRouter extends AbstractRouter {
104
114
  }
105
115
  async redirect(navigation, target) {
106
116
  await super.redirect(navigation, target);
117
+ // on CSR fallback initialization, if we found a redirect,
118
+ // we need to make hard reload for prevent current page rendering
119
+ if (this.fullRehydrationInProcess) {
120
+ window.location.replace(target.url);
121
+ // prevent routing from any continues navigation returning promise which will be not resolved
122
+ return new Promise(() => { });
123
+ }
107
124
  return this.internalNavigate({
108
125
  ...target,
109
126
  replace: target.replace || navigation.replace,
@@ -11,6 +11,8 @@ var client = require('../history/client.js');
11
11
  class ClientRouter extends abstract.AbstractRouter {
12
12
  constructor(options) {
13
13
  super(options);
14
+ // this flag for cases when we don't have initial router state from server - CSR fallback initialization
15
+ this.fullRehydrationInProcess = null;
14
16
  this.history = new client.ClientHistory();
15
17
  this.history.listen(async ({ type, url, navigateState, replace, history }) => {
16
18
  var _a;
@@ -36,8 +38,7 @@ class ClientRouter extends abstract.AbstractRouter {
36
38
  });
37
39
  }
38
40
  async rehydrate(navigation) {
39
- // this flag for cases when we don't have initial router state from server - CSR fallback for example
40
- const fullRehydration = !navigation.to;
41
+ this.fullRehydrationInProcess = !navigation.to;
41
42
  logger.logger.debug({
42
43
  event: 'rehydrate',
43
44
  navigation,
@@ -49,21 +50,25 @@ class ClientRouter extends abstract.AbstractRouter {
49
50
  url: url$1,
50
51
  };
51
52
  this.lastNavigation = this.currentNavigation;
52
- if (fullRehydration) {
53
+ if (this.fullRehydrationInProcess) {
53
54
  await this.runHooks('beforeResolve', this.currentNavigation);
54
55
  const to = this.resolveRoute({ url: url$1 }, { wildcard: true });
56
+ const redirect = to === null || to === void 0 ? void 0 : to.redirect;
55
57
  this.currentNavigation.to = to;
58
+ if (redirect) {
59
+ return this.redirect(this.currentNavigation, utils.makeNavigateOptions(redirect));
60
+ }
56
61
  }
57
62
  // rerun guard check in case it differs from server side
58
63
  await this.runGuards(this.currentNavigation);
59
64
  // and init any history listeners
60
65
  this.history.init(this.currentNavigation);
61
- if (fullRehydration) {
66
+ if (this.fullRehydrationInProcess) {
62
67
  this.runSyncHooks('change', this.currentNavigation);
63
68
  }
64
69
  this.currentNavigation = null;
65
70
  // add dehydrated route to tree to prevent its loading
66
- if (!fullRehydration) {
71
+ if (!this.fullRehydrationInProcess) {
67
72
  this.addRoute({
68
73
  name: navigation.to.name,
69
74
  path: navigation.to.path,
@@ -73,6 +78,7 @@ class ClientRouter extends abstract.AbstractRouter {
73
78
  alias: url$1.pathname,
74
79
  });
75
80
  }
81
+ this.fullRehydrationInProcess = null;
76
82
  }
77
83
  resolveRoute(...options) {
78
84
  const { url } = options[0];
@@ -100,6 +106,10 @@ class ClientRouter extends abstract.AbstractRouter {
100
106
  window.location.assign(nextUrl);
101
107
  }
102
108
  }
109
+ else if (this.onBlock) {
110
+ // last resort case for CSR fallback
111
+ return this.onBlock(navigation);
112
+ }
103
113
  // prevent routing from any continues navigation returning promise which will be not resolved
104
114
  return new Promise(() => { });
105
115
  }
@@ -108,6 +118,13 @@ class ClientRouter extends abstract.AbstractRouter {
108
118
  }
109
119
  async redirect(navigation, target) {
110
120
  await super.redirect(navigation, target);
121
+ // on CSR fallback initialization, if we found a redirect,
122
+ // we need to make hard reload for prevent current page rendering
123
+ if (this.fullRehydrationInProcess) {
124
+ window.location.replace(target.url);
125
+ // prevent routing from any continues navigation returning promise which will be not resolved
126
+ return new Promise(() => { });
127
+ }
111
128
  return this.internalNavigate({
112
129
  ...target,
113
130
  replace: target.replace || navigation.replace,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkoff/router",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "description": "router",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",