@tarojs/router 3.5.7 → 3.5.9

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/dist/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createBrowserHistory, createHashHistory, Action, parsePath } from 'history';
2
- import { Current, eventCenter, createPageConfig, hooks, stringify, requestAnimationFrame } from '@tarojs/runtime';
2
+ import { Current, incrementId, eventCenter, createPageConfig, hooks, stringify, requestAnimationFrame } from '@tarojs/runtime';
3
3
  import MobileDetect from 'mobile-detect';
4
4
  import queryString from 'query-string';
5
5
  import { initTabBarApis } from '@tarojs/taro';
@@ -202,6 +202,8 @@ class Stacks {
202
202
  constructor() {
203
203
  this.stacks = [];
204
204
  this.backDelta = 0;
205
+ this.tabs = {};
206
+ this.methodName = '';
205
207
  }
206
208
  set delta(delta) {
207
209
  if (delta > 0) {
@@ -217,6 +219,12 @@ class Stacks {
217
219
  get delta() {
218
220
  return this.backDelta;
219
221
  }
222
+ set method(methodName) {
223
+ this.methodName = methodName;
224
+ }
225
+ get method() {
226
+ return this.methodName;
227
+ }
220
228
  get length() {
221
229
  return this.stacks.length;
222
230
  }
@@ -254,6 +262,20 @@ class Stacks {
254
262
  push(page) {
255
263
  return this.stacks.push(page);
256
264
  }
265
+ getTabs() {
266
+ return this.tabs;
267
+ }
268
+ pushTab(path) {
269
+ this.tabs[path] = this.last;
270
+ this.pop();
271
+ }
272
+ popTab(path) {
273
+ this.push(this.tabs[path]);
274
+ delete this.tabs[path];
275
+ }
276
+ removeTab(path) {
277
+ delete this.tabs[path];
278
+ }
257
279
  }
258
280
  const stacks = new Stacks();
259
281
 
@@ -284,6 +306,7 @@ function processNavigateUrl(option) {
284
306
  function navigate(option, method) {
285
307
  return __awaiter(this, void 0, void 0, function* () {
286
308
  return new Promise((resolve, reject) => {
309
+ stacks.method = method;
287
310
  const { success, complete, fail } = option;
288
311
  const unListen = history.listen(() => {
289
312
  const res = { errMsg: `${method}:ok` };
@@ -460,7 +483,7 @@ class MultiPageHandler {
460
483
  this.config = config;
461
484
  this.mount();
462
485
  }
463
- get appId() { return 'app'; }
486
+ get appId() { return this.config.appId || 'app'; }
464
487
  get router() { return this.config.router || {}; }
465
488
  get routerMode() { return this.router.mode || 'hash'; }
466
489
  get customRoutes() { return this.router.customRoutes || {}; }
@@ -493,11 +516,13 @@ class MultiPageHandler {
493
516
  return Object.assign(Object.assign({}, query), options);
494
517
  }
495
518
  mount() {
496
- var _a;
497
519
  setHistoryMode(this.routerMode, this.router.basename);
498
- (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
499
- const app = document.createElement('div');
500
- app.id = this.appId;
520
+ const appId = this.appId;
521
+ let app = document.getElementById(appId);
522
+ if (!app) {
523
+ app = document.createElement('div');
524
+ app.id = appId;
525
+ }
501
526
  app.classList.add('taro_router');
502
527
  if (this.tabBarList.length > 1) {
503
528
  const container = document.createElement('div');
@@ -560,6 +585,8 @@ class MultiPageHandler {
560
585
  }
561
586
  }
562
587
 
588
+ const createStampId$1 = incrementId();
589
+ const launchStampId$1 = createStampId$1();
563
590
  // TODO 支持多路由 (APP 生命周期仅触发一次)
564
591
  /** Note: 关于多页面应用
565
592
  * - 需要配置路由映射(根目录跳转、404 页面……)
@@ -575,7 +602,7 @@ function createMultiRouter(app, config, framework) {
575
602
  const handler = new MultiPageHandler(config);
576
603
  const launchParam = {
577
604
  path: config.pageName,
578
- query: handler.getQuery(),
605
+ query: handler.getQuery(launchStampId$1),
579
606
  scene: 0,
580
607
  shareTicket: '',
581
608
  referrerInfo: {}
@@ -662,7 +689,7 @@ class PageHandler {
662
689
  this.homePage = this.getHomePage();
663
690
  this.mount();
664
691
  }
665
- get appId() { return 'app'; }
692
+ get appId() { return this.config.appId || 'app'; }
666
693
  get router() { return this.config.router || {}; }
667
694
  get routerMode() { return this.router.mode || 'hash'; }
668
695
  get customRoutes() { return this.router.customRoutes || {}; }
@@ -698,9 +725,9 @@ class PageHandler {
698
725
  return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
699
726
  });
700
727
  }
701
- get isTabBar() {
728
+ isTabBar(pathname) {
702
729
  var _a;
703
- const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
730
+ const routePath = addLeadingSlash(stripBasename(pathname, this.basename)).split('?')[0];
704
731
  const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
705
732
  if (typeof target === 'string') {
706
733
  return target === routePath;
@@ -736,21 +763,23 @@ class PageHandler {
736
763
  }
737
764
  return search.substr(1);
738
765
  }
739
- getQuery(stamp = 0, search = '', options = {}) {
766
+ getQuery(stamp = '', search = '', options = {}) {
740
767
  search = search ? `${search}&${this.search}` : this.search;
741
768
  const query = search
742
769
  ? queryString.parse(search, { decode: false })
743
770
  : {};
744
- query.stamp = stamp.toString();
771
+ query.stamp = stamp;
745
772
  return Object.assign(Object.assign({}, query), options);
746
773
  }
747
774
  mount() {
748
- var _a;
749
775
  setHistoryMode(this.routerMode, this.router.basename);
750
- (_a = document.getElementById('app')) === null || _a === void 0 ? void 0 : _a.remove();
751
776
  this.animation && loadAnimateStyle(this.animationDuration);
752
- const app = document.createElement('div');
753
- app.id = this.appId;
777
+ const appId = this.appId;
778
+ let app = document.getElementById(appId);
779
+ if (!app) {
780
+ app = document.createElement('div');
781
+ app.id = appId;
782
+ }
754
783
  app.classList.add('taro_router');
755
784
  if (this.tabBarList.length > 1) {
756
785
  const container = document.createElement('div');
@@ -782,18 +811,18 @@ class PageHandler {
782
811
  onLoad && (pageEl['__page'] = page);
783
812
  }
784
813
  }
785
- load(page, pageConfig = {}, stacksIndex = 0) {
814
+ load(page, pageConfig = {}, stampId, pageNo = 0) {
786
815
  var _a, _b;
787
816
  if (!page)
788
817
  return;
789
818
  // NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
790
819
  stacks.push(page);
791
- const param = this.getQuery(stacks.length, '', page.options);
820
+ const param = this.getQuery(stampId, '', page.options);
792
821
  let pageEl = this.getPageContainer(page);
793
822
  if (pageEl) {
794
823
  setDisplay(pageEl);
795
- this.isTabBar && pageEl.classList.add('taro_tabbar_page');
796
- this.addAnimation(pageEl, stacksIndex === 0);
824
+ this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
825
+ this.addAnimation(pageEl, pageNo === 0);
797
826
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
798
827
  this.bindPageEvents(page, pageEl, pageConfig);
799
828
  }
@@ -801,8 +830,8 @@ class PageHandler {
801
830
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
802
831
  var _a;
803
832
  pageEl = this.getPageContainer(page);
804
- this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
805
- this.addAnimation(pageEl, stacksIndex === 0);
833
+ this.isTabBar(this.pathname) && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
834
+ this.addAnimation(pageEl, pageNo === 0);
806
835
  this.onReady(page, true);
807
836
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
808
837
  this.bindPageEvents(page, pageEl, pageConfig);
@@ -840,15 +869,15 @@ class PageHandler {
840
869
  if (delta >= 1)
841
870
  this.unload(stacks.last, delta);
842
871
  }
843
- show(page, pageConfig = {}, stacksIndex = 0) {
872
+ show(page, pageConfig = {}, pageNo = 0) {
844
873
  var _a, _b;
845
874
  if (!page)
846
875
  return;
847
- const param = this.getQuery(stacks.length, '', page.options);
876
+ const param = this.getQuery(page['$taroParams']['stamp'], '', page.options);
848
877
  let pageEl = this.getPageContainer(page);
849
878
  if (pageEl) {
850
879
  setDisplay(pageEl);
851
- this.addAnimation(pageEl, stacksIndex === 0);
880
+ this.addAnimation(pageEl, pageNo === 0);
852
881
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
853
882
  this.bindPageEvents(page, pageEl, pageConfig);
854
883
  }
@@ -856,7 +885,7 @@ class PageHandler {
856
885
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
857
886
  var _a;
858
887
  pageEl = this.getPageContainer(page);
859
- this.addAnimation(pageEl, stacksIndex === 0);
888
+ this.addAnimation(pageEl, pageNo === 0);
860
889
  this.onReady(page, false);
861
890
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
862
891
  this.bindPageEvents(page, pageEl, pageConfig);
@@ -926,6 +955,8 @@ class PageHandler {
926
955
  }
927
956
  }
928
957
 
958
+ const createStampId = incrementId();
959
+ let launchStampId = createStampId();
929
960
  function createRouter(app, config, framework) {
930
961
  var _a, _b;
931
962
  RouterConfig.config = config;
@@ -943,7 +974,7 @@ function createRouter(app, config, framework) {
943
974
  const router = new UniversalRouter(routes, { baseUrl: basename || '' });
944
975
  const launchParam = {
945
976
  path: handler.homePage,
946
- query: handler.getQuery(stacks.length),
977
+ query: handler.getQuery(launchStampId),
947
978
  scene: 0,
948
979
  shareTicket: '',
949
980
  referrerInfo: {}
@@ -952,7 +983,7 @@ function createRouter(app, config, framework) {
952
983
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
953
984
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
954
985
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
955
- var _c, _d, _e, _f, _g;
986
+ var _c, _d, _e, _f, _g, _h;
956
987
  handler.pathname = decodeURI(location.pathname);
957
988
  eventCenter.trigger('__taroRouterChange', {
958
989
  toLocation: {
@@ -991,8 +1022,45 @@ function createRouter(app, config, framework) {
991
1022
  }
992
1023
  const currentPage = Current.page;
993
1024
  const pathname = handler.pathname;
1025
+ const methodName = (_g = stacks.method) !== null && _g !== void 0 ? _g : '';
1026
+ const cacheTabs = stacks.getTabs();
994
1027
  let shouldLoad = false;
995
- if (action === 'POP') {
1028
+ stacks.method = '';
1029
+ if (methodName === 'reLaunch') {
1030
+ handler.unload(currentPage, stacks.length);
1031
+ // NOTE: 同时卸载缓存在tabs里面的页面实例
1032
+ for (const key in cacheTabs) {
1033
+ if (cacheTabs[key]) {
1034
+ handler.unload(cacheTabs[key]);
1035
+ stacks.removeTab(key);
1036
+ }
1037
+ }
1038
+ shouldLoad = true;
1039
+ }
1040
+ else if (handler.isTabBar(handler.pathname)) {
1041
+ if (handler.isSamePage(currentPage))
1042
+ return;
1043
+ if (handler.isTabBar(currentPage.path)) {
1044
+ handler.hide(currentPage);
1045
+ stacks.pushTab(currentPage.path.split('?')[0]);
1046
+ }
1047
+ else if (stacks.length > 0) {
1048
+ const firstIns = stacks.getItem(0);
1049
+ if (handler.isTabBar(firstIns.path)) {
1050
+ handler.unload(currentPage, stacks.length - 1);
1051
+ stacks.pushTab(firstIns.path.split('?')[0]);
1052
+ }
1053
+ else {
1054
+ handler.unload(currentPage, stacks.length);
1055
+ }
1056
+ }
1057
+ if (cacheTabs[handler.pathname]) {
1058
+ stacks.popTab(handler.pathname);
1059
+ return handler.show(stacks.getItem(0), pageConfig, 0);
1060
+ }
1061
+ shouldLoad = true;
1062
+ }
1063
+ else if (action === 'POP') {
996
1064
  // NOTE: 浏览器事件退后多次时,该事件只会被触发一次
997
1065
  const prevIndex = stacks.getPrevIndex(pathname);
998
1066
  const delta = stacks.getDelta(pathname);
@@ -1007,37 +1075,34 @@ function createRouter(app, config, framework) {
1007
1075
  }
1008
1076
  }
1009
1077
  }
1010
- else {
1011
- if (handler.isTabBar) {
1012
- if (handler.isSamePage(currentPage))
1013
- return;
1014
- const prevIndex = stacks.getPrevIndex(pathname, 0);
1015
- handler.hide(currentPage);
1016
- if (prevIndex > -1) {
1017
- // NOTE: tabbar 页且之前出现过,直接复用
1018
- return handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
1019
- }
1020
- }
1021
- else if (action === 'REPLACE') {
1022
- const delta = stacks.getDelta(pathname);
1023
- // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1024
- handler.unload(currentPage, delta);
1025
- }
1026
- else if (action === 'PUSH') {
1027
- handler.hide(currentPage);
1028
- }
1078
+ else if (action === 'REPLACE') {
1079
+ const delta = stacks.getDelta(pathname);
1080
+ // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1081
+ handler.unload(currentPage, delta);
1082
+ shouldLoad = true;
1083
+ }
1084
+ else if (action === 'PUSH') {
1085
+ handler.hide(currentPage);
1029
1086
  shouldLoad = true;
1030
1087
  }
1031
1088
  if (shouldLoad || stacks.length < 1) {
1032
- const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
1089
+ const el = (_h = element.default) !== null && _h !== void 0 ? _h : element;
1033
1090
  const loadConfig = Object.assign({}, pageConfig);
1034
1091
  const stacksIndex = stacks.length;
1035
1092
  delete loadConfig['path'];
1036
1093
  delete loadConfig['load'];
1037
- const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
1094
+ let pageStampId = '';
1095
+ if (launchStampId) {
1096
+ pageStampId = launchStampId;
1097
+ launchStampId = '';
1098
+ }
1099
+ else {
1100
+ pageStampId = createStampId();
1101
+ }
1102
+ const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + stringify(handler.getQuery(pageStampId)), {}, loadConfig);
1038
1103
  if (params)
1039
1104
  page.options = params;
1040
- return handler.load(page, pageConfig, stacksIndex);
1105
+ return handler.load(page, pageConfig, pageStampId, stacksIndex);
1041
1106
  }
1042
1107
  });
1043
1108
  const routePath = addLeadingSlash(stripBasename(history.location.pathname, handler.basename));