@tarojs/router 4.0.0-alpha.19 → 4.0.0-alpha.20

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/LICENSE CHANGED
@@ -165,3 +165,10 @@ Apache-2.0 (intersection-observer):
165
165
  The following files embed [intersection-observer](https://github.com/GoogleChromeLabs/intersection-observer) Apache-2.0:
166
166
  `/packages/taro-api/src/polyfill/intersection-observer.ts`
167
167
  See `/LICENSE.txt` for details of the license.
168
+
169
+ ==================
170
+
171
+ MIT (babel-plugin-jsx-dom-expressions):
172
+ The following files embed [babel-plugin-jsx-dom-expressions](https://github.com/ryansolid/dom-expressions/blob/main/packages/babel-plugin-jsx-dom-expressions) MIT:
173
+ `/packages/babel-plugin-transform-solid-jsx/src/*`
174
+ See `/LICENSE` for details of the license.
package/dist/api.js CHANGED
@@ -54,6 +54,7 @@ function navigate(option, method) {
54
54
  const pathPieces = processNavigateUrl(option);
55
55
  const state = { timestamp: Date.now() };
56
56
  if (method === 'navigateTo') {
57
+ // Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
57
58
  history.push(pathPieces, state);
58
59
  }
59
60
  else if (method === 'redirectTo' || method === 'switchTab') {
package/dist/index.cjs.js CHANGED
@@ -574,6 +574,7 @@ function navigate(option, method) {
574
574
  const pathPieces = processNavigateUrl(option);
575
575
  const state = { timestamp: Date.now() };
576
576
  if (method === 'navigateTo') {
577
+ // Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
577
578
  exports.history.push(pathPieces, state);
578
579
  }
579
580
  else if (method === 'redirectTo' || method === 'switchTab') {
@@ -1493,6 +1494,10 @@ function createRouter(history$1, app, config, framework) {
1493
1494
  }
1494
1495
  RouterConfig.config = config;
1495
1496
  const handler = new PageHandler(config, history$1);
1497
+ // Note: 弱网情况下,快速切换 tab,会造成同个页面实例被多次挂在到页面上,原因是资源请求是异步的,短时间内发起多个请求,
1498
+ // 会在资源加载完成后,同时走到挂载的逻辑,造成 pageStampId 更新不及时,两个 page 的Id 相同,后面很多操作是通过 getElementById 来进行的
1499
+ // 所以需要加一个锁来应对这个情况
1500
+ const pageLock = {};
1496
1501
  routesAlias.set(handler.router.customRoutes);
1497
1502
  const basename = handler.router.basename;
1498
1503
  const routes = handler.routes.map(route => {
@@ -1516,24 +1521,30 @@ function createRouter(history$1, app, config, framework) {
1516
1521
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
1517
1522
  const render = (_c) => tslib.__awaiter(this, [_c], void 0, function* ({ location, action }) {
1518
1523
  var _d, _e, _f, _g, _h, _j, _k, _l;
1519
- handler.pathname = decodeURI(location.pathname);
1524
+ // Note: 由于下面有异步加载操作 先不要在这里去设置 handler.pathname
1525
+ const currentPathname = decodeURI(location.pathname);
1520
1526
  if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
1521
1527
  window.scrollTo(0, 0);
1522
1528
  runtime.eventCenter.trigger('__taroRouterChange', {
1523
1529
  toLocation: {
1524
- path: handler.pathname
1530
+ path: currentPathname
1525
1531
  }
1526
1532
  });
1527
- let element, params;
1533
+ let element, context, params;
1534
+ const routerPath = handler.router.forcePath || currentPathname;
1535
+ pageLock[routerPath] = typeof pageLock[routerPath] === 'number' ? pageLock[routerPath] + 1 : 1;
1536
+ const currentLock = pageLock[routerPath];
1537
+ let postLock;
1528
1538
  try {
1529
- const result = yield router.resolve(handler.router.forcePath || handler.pathname);
1530
- [element, , params] = yield Promise.all(result);
1539
+ const result = yield router.resolve(routerPath);
1540
+ [element, context, params] = yield Promise.all(result);
1541
+ postLock = pageLock[routerPath];
1531
1542
  }
1532
1543
  catch (error) {
1533
1544
  if (error.status === 404) {
1534
1545
  const notFoundEvent = {
1535
1546
  isEntryPage: stacks.length === 0,
1536
- path: handler.pathname,
1547
+ path: currentPathname,
1537
1548
  query: handler.getQuery(createStampId()),
1538
1549
  };
1539
1550
  (_e = app.onPageNotFound) === null || _e === void 0 ? void 0 : _e.call(app, notFoundEvent);
@@ -1547,9 +1558,12 @@ function createRouter(history$1, app, config, framework) {
1547
1558
  throw error;
1548
1559
  }
1549
1560
  }
1550
- if (!element)
1561
+ if (!element || currentLock !== postLock)
1551
1562
  return;
1552
- const pageConfig = handler.pageConfig;
1563
+ // Note: 异步结束后,在设置 handler.pathname
1564
+ // context.pathname 在 universal-router 被处理过了,是发起资源请求的时候传入的 pathname,即 await router.resolve(routerPath) 这个 routerPath
1565
+ handler.pathname = context.pathname;
1566
+ const { pathname, pageConfig } = handler;
1553
1567
  let enablePullDownRefresh = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.enablePullDownRefresh) || false;
1554
1568
  let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
1555
1569
  let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
@@ -1570,7 +1584,6 @@ function createRouter(history$1, app, config, framework) {
1570
1584
  }
1571
1585
  runtime.eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
1572
1586
  const currentPage = runtime.Current.page;
1573
- const pathname = handler.pathname;
1574
1587
  const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
1575
1588
  const cacheTabs = stacks.getTabs();
1576
1589
  let shouldLoad = false;
@@ -1586,7 +1599,7 @@ function createRouter(history$1, app, config, framework) {
1586
1599
  }
1587
1600
  shouldLoad = true;
1588
1601
  }
1589
- else if (currentPage && handler.isTabBar(handler.pathname)) {
1602
+ else if (currentPage && handler.isTabBar(pathname)) {
1590
1603
  if (handler.isSamePage(currentPage))
1591
1604
  return;
1592
1605
  if (handler.isTabBar(currentPage.path)) {
@@ -1604,8 +1617,8 @@ function createRouter(history$1, app, config, framework) {
1604
1617
  handler.unload(currentPage, stacks.length, true);
1605
1618
  }
1606
1619
  }
1607
- if (cacheTabs[handler.pathname]) {
1608
- stacks.popTab(handler.pathname);
1620
+ if (cacheTabs[pathname]) {
1621
+ stacks.popTab(pathname);
1609
1622
  return handler.show(stacks.getItem(0), pageConfig, 0);
1610
1623
  }
1611
1624
  shouldLoad = true;
package/dist/index.esm.js CHANGED
@@ -573,6 +573,7 @@ function navigate(option, method) {
573
573
  const pathPieces = processNavigateUrl(option);
574
574
  const state = { timestamp: Date.now() };
575
575
  if (method === 'navigateTo') {
576
+ // Note: 由于 spa 会针对弱网情况下,短时间内多次跳转同一个页面跳转加了锁,后续如果有用户反馈返回无效,那可能是这个问题
576
577
  history.push(pathPieces, state);
577
578
  }
578
579
  else if (method === 'redirectTo' || method === 'switchTab') {
@@ -1492,6 +1493,10 @@ function createRouter(history, app, config, framework) {
1492
1493
  }
1493
1494
  RouterConfig.config = config;
1494
1495
  const handler = new PageHandler(config, history);
1496
+ // Note: 弱网情况下,快速切换 tab,会造成同个页面实例被多次挂在到页面上,原因是资源请求是异步的,短时间内发起多个请求,
1497
+ // 会在资源加载完成后,同时走到挂载的逻辑,造成 pageStampId 更新不及时,两个 page 的Id 相同,后面很多操作是通过 getElementById 来进行的
1498
+ // 所以需要加一个锁来应对这个情况
1499
+ const pageLock = {};
1495
1500
  routesAlias.set(handler.router.customRoutes);
1496
1501
  const basename = handler.router.basename;
1497
1502
  const routes = handler.routes.map(route => {
@@ -1515,24 +1520,30 @@ function createRouter(history, app, config, framework) {
1515
1520
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
1516
1521
  const render = (_c) => __awaiter(this, [_c], void 0, function* ({ location, action }) {
1517
1522
  var _d, _e, _f, _g, _h, _j, _k, _l;
1518
- handler.pathname = decodeURI(location.pathname);
1523
+ // Note: 由于下面有异步加载操作 先不要在这里去设置 handler.pathname
1524
+ const currentPathname = decodeURI(location.pathname);
1519
1525
  if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
1520
1526
  window.scrollTo(0, 0);
1521
1527
  eventCenter.trigger('__taroRouterChange', {
1522
1528
  toLocation: {
1523
- path: handler.pathname
1529
+ path: currentPathname
1524
1530
  }
1525
1531
  });
1526
- let element, params;
1532
+ let element, context, params;
1533
+ const routerPath = handler.router.forcePath || currentPathname;
1534
+ pageLock[routerPath] = typeof pageLock[routerPath] === 'number' ? pageLock[routerPath] + 1 : 1;
1535
+ const currentLock = pageLock[routerPath];
1536
+ let postLock;
1527
1537
  try {
1528
- const result = yield router.resolve(handler.router.forcePath || handler.pathname);
1529
- [element, , params] = yield Promise.all(result);
1538
+ const result = yield router.resolve(routerPath);
1539
+ [element, context, params] = yield Promise.all(result);
1540
+ postLock = pageLock[routerPath];
1530
1541
  }
1531
1542
  catch (error) {
1532
1543
  if (error.status === 404) {
1533
1544
  const notFoundEvent = {
1534
1545
  isEntryPage: stacks.length === 0,
1535
- path: handler.pathname,
1546
+ path: currentPathname,
1536
1547
  query: handler.getQuery(createStampId()),
1537
1548
  };
1538
1549
  (_e = app.onPageNotFound) === null || _e === void 0 ? void 0 : _e.call(app, notFoundEvent);
@@ -1546,9 +1557,12 @@ function createRouter(history, app, config, framework) {
1546
1557
  throw error;
1547
1558
  }
1548
1559
  }
1549
- if (!element)
1560
+ if (!element || currentLock !== postLock)
1550
1561
  return;
1551
- const pageConfig = handler.pageConfig;
1562
+ // Note: 异步结束后,在设置 handler.pathname
1563
+ // context.pathname 在 universal-router 被处理过了,是发起资源请求的时候传入的 pathname,即 await router.resolve(routerPath) 这个 routerPath
1564
+ handler.pathname = context.pathname;
1565
+ const { pathname, pageConfig } = handler;
1552
1566
  let enablePullDownRefresh = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.enablePullDownRefresh) || false;
1553
1567
  let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
1554
1568
  let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
@@ -1569,7 +1583,6 @@ function createRouter(history, app, config, framework) {
1569
1583
  }
1570
1584
  eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
1571
1585
  const currentPage = Current.page;
1572
- const pathname = handler.pathname;
1573
1586
  const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
1574
1587
  const cacheTabs = stacks.getTabs();
1575
1588
  let shouldLoad = false;
@@ -1585,7 +1598,7 @@ function createRouter(history, app, config, framework) {
1585
1598
  }
1586
1599
  shouldLoad = true;
1587
1600
  }
1588
- else if (currentPage && handler.isTabBar(handler.pathname)) {
1601
+ else if (currentPage && handler.isTabBar(pathname)) {
1589
1602
  if (handler.isSamePage(currentPage))
1590
1603
  return;
1591
1604
  if (handler.isTabBar(currentPage.path)) {
@@ -1603,8 +1616,8 @@ function createRouter(history, app, config, framework) {
1603
1616
  handler.unload(currentPage, stacks.length, true);
1604
1617
  }
1605
1618
  }
1606
- if (cacheTabs[handler.pathname]) {
1607
- stacks.popTab(handler.pathname);
1619
+ if (cacheTabs[pathname]) {
1620
+ stacks.popTab(pathname);
1608
1621
  return handler.show(stacks.getItem(0), pageConfig, 0);
1609
1622
  }
1610
1623
  shouldLoad = true;
@@ -17,6 +17,10 @@ function createRouter(history, app, config, framework) {
17
17
  }
18
18
  RouterConfig.config = config;
19
19
  const handler = new PageHandler(config, history);
20
+ // Note: 弱网情况下,快速切换 tab,会造成同个页面实例被多次挂在到页面上,原因是资源请求是异步的,短时间内发起多个请求,
21
+ // 会在资源加载完成后,同时走到挂载的逻辑,造成 pageStampId 更新不及时,两个 page 的Id 相同,后面很多操作是通过 getElementById 来进行的
22
+ // 所以需要加一个锁来应对这个情况
23
+ const pageLock = {};
20
24
  routesAlias.set(handler.router.customRoutes);
21
25
  const basename = handler.router.basename;
22
26
  const routes = handler.routes.map(route => {
@@ -40,24 +44,30 @@ function createRouter(history, app, config, framework) {
40
44
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
41
45
  const render = (_c) => __awaiter(this, [_c], void 0, function* ({ location, action }) {
42
46
  var _d, _e, _f, _g, _h, _j, _k, _l;
43
- handler.pathname = decodeURI(location.pathname);
47
+ // Note: 由于下面有异步加载操作 先不要在这里去设置 handler.pathname
48
+ const currentPathname = decodeURI(location.pathname);
44
49
  if ((_d = window.__taroAppConfig) === null || _d === void 0 ? void 0 : _d.usingWindowScroll)
45
50
  window.scrollTo(0, 0);
46
51
  eventCenter.trigger('__taroRouterChange', {
47
52
  toLocation: {
48
- path: handler.pathname
53
+ path: currentPathname
49
54
  }
50
55
  });
51
- let element, params;
56
+ let element, context, params;
57
+ const routerPath = handler.router.forcePath || currentPathname;
58
+ pageLock[routerPath] = typeof pageLock[routerPath] === 'number' ? pageLock[routerPath] + 1 : 1;
59
+ const currentLock = pageLock[routerPath];
60
+ let postLock;
52
61
  try {
53
- const result = yield router.resolve(handler.router.forcePath || handler.pathname);
54
- [element, , params] = yield Promise.all(result);
62
+ const result = yield router.resolve(routerPath);
63
+ [element, context, params] = yield Promise.all(result);
64
+ postLock = pageLock[routerPath];
55
65
  }
56
66
  catch (error) {
57
67
  if (error.status === 404) {
58
68
  const notFoundEvent = {
59
69
  isEntryPage: stacks.length === 0,
60
- path: handler.pathname,
70
+ path: currentPathname,
61
71
  query: handler.getQuery(createStampId()),
62
72
  };
63
73
  (_e = app.onPageNotFound) === null || _e === void 0 ? void 0 : _e.call(app, notFoundEvent);
@@ -71,9 +81,12 @@ function createRouter(history, app, config, framework) {
71
81
  throw error;
72
82
  }
73
83
  }
74
- if (!element)
84
+ if (!element || currentLock !== postLock)
75
85
  return;
76
- const pageConfig = handler.pageConfig;
86
+ // Note: 异步结束后,在设置 handler.pathname
87
+ // context.pathname 在 universal-router 被处理过了,是发起资源请求的时候传入的 pathname,即 await router.resolve(routerPath) 这个 routerPath
88
+ handler.pathname = context.pathname;
89
+ const { pathname, pageConfig } = handler;
77
90
  let enablePullDownRefresh = ((_f = config === null || config === void 0 ? void 0 : config.window) === null || _f === void 0 ? void 0 : _f.enablePullDownRefresh) || false;
78
91
  let navigationStyle = ((_g = config === null || config === void 0 ? void 0 : config.window) === null || _g === void 0 ? void 0 : _g.navigationStyle) || 'default';
79
92
  let navigationBarTextStyle = ((_h = config === null || config === void 0 ? void 0 : config.window) === null || _h === void 0 ? void 0 : _h.navigationBarTextStyle) || 'white';
@@ -94,7 +107,6 @@ function createRouter(history, app, config, framework) {
94
107
  }
95
108
  eventCenter.trigger('__taroSetNavigationStyle', navigationStyle, navigationBarTextStyle, navigationBarBackgroundColor);
96
109
  const currentPage = Current.page;
97
- const pathname = handler.pathname;
98
110
  const methodName = (_k = stacks.method) !== null && _k !== void 0 ? _k : '';
99
111
  const cacheTabs = stacks.getTabs();
100
112
  let shouldLoad = false;
@@ -110,7 +122,7 @@ function createRouter(history, app, config, framework) {
110
122
  }
111
123
  shouldLoad = true;
112
124
  }
113
- else if (currentPage && handler.isTabBar(handler.pathname)) {
125
+ else if (currentPage && handler.isTabBar(pathname)) {
114
126
  if (handler.isSamePage(currentPage))
115
127
  return;
116
128
  if (handler.isTabBar(currentPage.path)) {
@@ -128,8 +140,8 @@ function createRouter(history, app, config, framework) {
128
140
  handler.unload(currentPage, stacks.length, true);
129
141
  }
130
142
  }
131
- if (cacheTabs[handler.pathname]) {
132
- stacks.popTab(handler.pathname);
143
+ if (cacheTabs[pathname]) {
144
+ stacks.popTab(pathname);
133
145
  return handler.show(stacks.getItem(0), pageConfig, 0);
134
146
  }
135
147
  shouldLoad = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tarojs/router",
3
- "version": "4.0.0-alpha.19",
3
+ "version": "4.0.0-alpha.20",
4
4
  "description": "Taro-router",
5
5
  "author": "O2Team",
6
6
  "license": "MIT",
@@ -34,15 +34,15 @@
34
34
  "universal-router": "^9.2.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@tarojs/components": "4.0.0-alpha.19",
38
- "@tarojs/runtime": "4.0.0-alpha.19",
39
- "@tarojs/taro": "4.0.0-alpha.19",
40
- "@tarojs/shared": "4.0.0-alpha.19"
37
+ "@tarojs/components": "4.0.0-alpha.20",
38
+ "@tarojs/runtime": "4.0.0-alpha.20",
39
+ "@tarojs/taro": "4.0.0-alpha.20",
40
+ "@tarojs/shared": "4.0.0-alpha.20"
41
41
  },
42
42
  "peerDependencies": {
43
- "@tarojs/runtime": "4.0.0-alpha.19",
44
- "@tarojs/taro": "4.0.0-alpha.19",
45
- "@tarojs/shared": "4.0.0-alpha.19"
43
+ "@tarojs/runtime": "4.0.0-alpha.20",
44
+ "@tarojs/taro": "4.0.0-alpha.20",
45
+ "@tarojs/shared": "4.0.0-alpha.20"
46
46
  },
47
47
  "scripts": {
48
48
  "prod": "pnpm run build",