@tarojs/router 3.5.8 → 3.5.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.
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` };
@@ -562,6 +585,8 @@ class MultiPageHandler {
562
585
  }
563
586
  }
564
587
 
588
+ const createStampId$1 = incrementId();
589
+ const launchStampId$1 = createStampId$1();
565
590
  // TODO 支持多路由 (APP 生命周期仅触发一次)
566
591
  /** Note: 关于多页面应用
567
592
  * - 需要配置路由映射(根目录跳转、404 页面……)
@@ -577,7 +602,7 @@ function createMultiRouter(app, config, framework) {
577
602
  const handler = new MultiPageHandler(config);
578
603
  const launchParam = {
579
604
  path: config.pageName,
580
- query: handler.getQuery(),
605
+ query: handler.getQuery(launchStampId$1),
581
606
  scene: 0,
582
607
  shareTicket: '',
583
608
  referrerInfo: {}
@@ -700,9 +725,9 @@ class PageHandler {
700
725
  return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
701
726
  });
702
727
  }
703
- get isTabBar() {
728
+ isTabBar(pathname) {
704
729
  var _a;
705
- const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
730
+ const routePath = addLeadingSlash(stripBasename(pathname, this.basename)).split('?')[0];
706
731
  const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
707
732
  if (typeof target === 'string') {
708
733
  return target === routePath;
@@ -738,12 +763,12 @@ class PageHandler {
738
763
  }
739
764
  return search.substr(1);
740
765
  }
741
- getQuery(stamp = 0, search = '', options = {}) {
766
+ getQuery(stamp = '', search = '', options = {}) {
742
767
  search = search ? `${search}&${this.search}` : this.search;
743
768
  const query = search
744
769
  ? queryString.parse(search, { decode: false })
745
770
  : {};
746
- query.stamp = stamp.toString();
771
+ query.stamp = stamp;
747
772
  return Object.assign(Object.assign({}, query), options);
748
773
  }
749
774
  mount() {
@@ -786,18 +811,18 @@ class PageHandler {
786
811
  onLoad && (pageEl['__page'] = page);
787
812
  }
788
813
  }
789
- load(page, pageConfig = {}, stacksIndex = 0) {
814
+ load(page, pageConfig = {}, stampId, pageNo = 0) {
790
815
  var _a, _b;
791
816
  if (!page)
792
817
  return;
793
818
  // NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
794
819
  stacks.push(page);
795
- const param = this.getQuery(stacks.length, '', page.options);
820
+ const param = this.getQuery(stampId, '', page.options);
796
821
  let pageEl = this.getPageContainer(page);
797
822
  if (pageEl) {
798
823
  setDisplay(pageEl);
799
- this.isTabBar && pageEl.classList.add('taro_tabbar_page');
800
- this.addAnimation(pageEl, stacksIndex === 0);
824
+ this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
825
+ this.addAnimation(pageEl, pageNo === 0);
801
826
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
802
827
  this.bindPageEvents(page, pageEl, pageConfig);
803
828
  }
@@ -805,8 +830,8 @@ class PageHandler {
805
830
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
806
831
  var _a;
807
832
  pageEl = this.getPageContainer(page);
808
- this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
809
- 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);
810
835
  this.onReady(page, true);
811
836
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
812
837
  this.bindPageEvents(page, pageEl, pageConfig);
@@ -844,15 +869,15 @@ class PageHandler {
844
869
  if (delta >= 1)
845
870
  this.unload(stacks.last, delta);
846
871
  }
847
- show(page, pageConfig = {}, stacksIndex = 0) {
872
+ show(page, pageConfig = {}, pageNo = 0) {
848
873
  var _a, _b;
849
874
  if (!page)
850
875
  return;
851
- const param = this.getQuery(stacks.length, '', page.options);
876
+ const param = this.getQuery(page['$taroParams']['stamp'], '', page.options);
852
877
  let pageEl = this.getPageContainer(page);
853
878
  if (pageEl) {
854
879
  setDisplay(pageEl);
855
- this.addAnimation(pageEl, stacksIndex === 0);
880
+ this.addAnimation(pageEl, pageNo === 0);
856
881
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
857
882
  this.bindPageEvents(page, pageEl, pageConfig);
858
883
  }
@@ -860,7 +885,7 @@ class PageHandler {
860
885
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
861
886
  var _a;
862
887
  pageEl = this.getPageContainer(page);
863
- this.addAnimation(pageEl, stacksIndex === 0);
888
+ this.addAnimation(pageEl, pageNo === 0);
864
889
  this.onReady(page, false);
865
890
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
866
891
  this.bindPageEvents(page, pageEl, pageConfig);
@@ -930,6 +955,8 @@ class PageHandler {
930
955
  }
931
956
  }
932
957
 
958
+ const createStampId = incrementId();
959
+ let launchStampId = createStampId();
933
960
  function createRouter(app, config, framework) {
934
961
  var _a, _b;
935
962
  RouterConfig.config = config;
@@ -947,7 +974,7 @@ function createRouter(app, config, framework) {
947
974
  const router = new UniversalRouter(routes, { baseUrl: basename || '' });
948
975
  const launchParam = {
949
976
  path: handler.homePage,
950
- query: handler.getQuery(stacks.length),
977
+ query: handler.getQuery(launchStampId),
951
978
  scene: 0,
952
979
  shareTicket: '',
953
980
  referrerInfo: {}
@@ -956,7 +983,7 @@ function createRouter(app, config, framework) {
956
983
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
957
984
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
958
985
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
959
- var _c, _d, _e, _f, _g;
986
+ var _c, _d, _e, _f, _g, _h;
960
987
  handler.pathname = decodeURI(location.pathname);
961
988
  eventCenter.trigger('__taroRouterChange', {
962
989
  toLocation: {
@@ -995,8 +1022,45 @@ function createRouter(app, config, framework) {
995
1022
  }
996
1023
  const currentPage = Current.page;
997
1024
  const pathname = handler.pathname;
1025
+ const methodName = (_g = stacks.method) !== null && _g !== void 0 ? _g : '';
1026
+ const cacheTabs = stacks.getTabs();
998
1027
  let shouldLoad = false;
999
- 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') {
1000
1064
  // NOTE: 浏览器事件退后多次时,该事件只会被触发一次
1001
1065
  const prevIndex = stacks.getPrevIndex(pathname);
1002
1066
  const delta = stacks.getDelta(pathname);
@@ -1011,37 +1075,34 @@ function createRouter(app, config, framework) {
1011
1075
  }
1012
1076
  }
1013
1077
  }
1014
- else {
1015
- if (handler.isTabBar) {
1016
- if (handler.isSamePage(currentPage))
1017
- return;
1018
- const prevIndex = stacks.getPrevIndex(pathname, 0);
1019
- handler.hide(currentPage);
1020
- if (prevIndex > -1) {
1021
- // NOTE: tabbar 页且之前出现过,直接复用
1022
- return handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
1023
- }
1024
- }
1025
- else if (action === 'REPLACE') {
1026
- const delta = stacks.getDelta(pathname);
1027
- // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1028
- handler.unload(currentPage, delta);
1029
- }
1030
- else if (action === 'PUSH') {
1031
- handler.hide(currentPage);
1032
- }
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);
1033
1086
  shouldLoad = true;
1034
1087
  }
1035
1088
  if (shouldLoad || stacks.length < 1) {
1036
- const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
1089
+ const el = (_h = element.default) !== null && _h !== void 0 ? _h : element;
1037
1090
  const loadConfig = Object.assign({}, pageConfig);
1038
1091
  const stacksIndex = stacks.length;
1039
1092
  delete loadConfig['path'];
1040
1093
  delete loadConfig['load'];
1041
- 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);
1042
1103
  if (params)
1043
1104
  page.options = params;
1044
- return handler.load(page, pageConfig, stacksIndex);
1105
+ return handler.load(page, pageConfig, pageStampId, stacksIndex);
1045
1106
  }
1046
1107
  });
1047
1108
  const routePath = addLeadingSlash(stripBasename(history.location.pathname, handler.basename));