@micro-zoe/micro-app 1.0.0-rc.6 → 1.0.0-rc.8

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/lib/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- const version = '1.0.0-rc.6';
1
+ const version = '1.0.0-rc.8';
2
2
  // do not use isUndefined
3
3
  const isBrowser = typeof window !== 'undefined';
4
4
  // do not use isUndefined
@@ -4065,32 +4065,34 @@ function createMicroHistory(appName, microLocation) {
4065
4065
  (_c = (_a = appInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : (_b = _a.sandBox).updateIframeBase) === null || _c === void 0 ? void 0 : _c.call(_b);
4066
4066
  };
4067
4067
  }
4068
- const pushState = getMicroHistoryMethod('pushState');
4069
- const replaceState = getMicroHistoryMethod('replaceState');
4068
+ const originalHistory = {
4069
+ pushState: getMicroHistoryMethod('pushState'),
4070
+ replaceState: getMicroHistoryMethod('replaceState'),
4071
+ };
4070
4072
  if (isIframeSandbox(appName)) {
4071
- return {
4072
- pushState,
4073
- replaceState,
4073
+ return assign({
4074
4074
  go(delta) {
4075
4075
  return rawHistory.go(delta);
4076
4076
  }
4077
- };
4077
+ }, originalHistory);
4078
4078
  }
4079
4079
  return new Proxy(rawHistory, {
4080
4080
  get(target, key) {
4081
- if (key === 'state') {
4082
- return getMicroState(appName);
4083
- }
4084
- else if (key === 'pushState') {
4085
- return pushState;
4081
+ if (key === 'pushState' || key === 'replaceState') {
4082
+ return originalHistory[key];
4086
4083
  }
4087
- else if (key === 'replaceState') {
4088
- return replaceState;
4084
+ else if (key === 'state') {
4085
+ return getMicroState(appName);
4089
4086
  }
4090
4087
  return bindFunctionToRawTarget(Reflect.get(target, key), target, 'HISTORY');
4091
4088
  },
4092
4089
  set(target, key, value) {
4093
- Reflect.set(target, key, value);
4090
+ if (key === 'pushState' || key === 'replaceState') {
4091
+ originalHistory[key] = value;
4092
+ }
4093
+ else {
4094
+ Reflect.set(target, key, value);
4095
+ }
4094
4096
  /**
4095
4097
  * If the set() method returns false, and the assignment happened in strict-mode code, a TypeError will be thrown.
4096
4098
  * e.g. history.state = {}
@@ -4186,7 +4188,6 @@ function reWriteHistoryMethod(method) {
4186
4188
  excludeHiddenApp: true,
4187
4189
  excludePreRender: true,
4188
4190
  }).forEach(appName => {
4189
- // TODO: 大部分情况下,history.pushState 都是先执行,micro-app后卸载,所以会产生一种情况:跳转到新地址后,search模式会在url上添加参数,卸载后再将参数删除,所以会导致浏览器地址闪烁,是否需要去掉这个功能
4190
4191
  if ((isRouterModeSearch(appName) || isRouterModeState(appName)) && !getMicroPathFromURL(appName)) {
4191
4192
  const app = appInstanceMap.get(appName);
4192
4193
  attachRouteToBrowserURL(appName, setMicroPathToURL(appName, app.sandBox.proxyWindow.location), setMicroState(appName, getMicroState(appName), app.sandBox.proxyWindow.location));
@@ -4752,12 +4753,12 @@ function updateMicroLocation(appName, targetFullPath, microLocation, type) {
4752
4753
  microLocation.self.href = targetHref;
4753
4754
  }
4754
4755
  /**
4755
- * native模式从state中取值,而浏览器地址的修改无法控制,很可能出现浏览器地址和__MICRO_APP_STATE__不一致的情况
4756
- * 尤其是在初始化和前进后退时,由于vue-router4会主动修改url地址,倒是上述情况经常出现
4757
- * 为了结局这个问题,在子应用初始化和响应popstate事件后,判断__MICRO_APP_STATE__和浏览器地址是否一致,如果不一致,则将浏览器地址更新为__MICRO_APP_STATE__的地址
4758
- * 说明:
4759
- * 1、如果__MICRO_APP_STATE__和url不一样,那么更新url的操作是对的,否则会产生url和渲染页面不一致的问题,开发者会更加困惑
4760
- * 2、当native模式有多个子应用同时存在,其中一个修改url地址,另外一个并不会改变__MICRO_APP_STATE__,刷新就产生问题,不一致,第二是根据谁更新url?
4756
+ * The native mode also base of history.state, and the modification of the browser url cannot be controlled. It is very likely that the browser url and __MICRO_APP_STATE__ are different.
4757
+ * Especially during init of child or forward and backward of browser, because vue-router@4 will actively modify the browser URL, the above situation often occurs
4758
+ * To solve this problem, after child app is initialized and responds to the popstateEvent, it is determined whether __MICRO_APP_STATE__ and the browser url are different. If they are different, the browser url will updated to the address of __MICRO_APP_STATE__
4759
+ * NOTE:
4760
+ * 1. If __MICRO_APP_STATE__ is different from the URL, then the operation of updating the URL is correct, otherwise there will be a problem of inconsistency between the URL and the rendered page
4761
+ * 2. When there are multiple child app in native mode, if one of them changes the URL address, the other one will not change __MICRO_APP_STATE__, and refresh browser will cause problems
4761
4762
  */
4762
4763
  const rawLocation = globalEnv.rawWindow.location;
4763
4764
  if (isRouterModeCustom(appName) &&
@@ -5848,14 +5849,26 @@ function patchDocumentPrototype(appName, microAppWindow) {
5848
5849
  }
5849
5850
  // query element👇
5850
5851
  function querySelector(selectors) {
5851
- var _a, _b, _c;
5852
+ var _a;
5852
5853
  const _this = getBindTarget(this);
5853
5854
  if (!selectors ||
5854
5855
  isUniqueElement(selectors) ||
5855
5856
  rawDocument !== _this) {
5856
5857
  return rawMicroQuerySelector.call(_this, selectors);
5857
5858
  }
5858
- return (_c = (_b = (_a = appInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.querySelector(selectors)) !== null && _b !== void 0 ? _b : rawMicroQuerySelector.call(microDocument, selectors)) !== null && _c !== void 0 ? _c : null;
5859
+ /**
5860
+ * The child app cannot query the base element inside iframe
5861
+ * Same for querySelectorAll
5862
+ *
5863
+ * Scenes:
5864
+ * 1. vue-router@4.x --> createWebHistory(base?: string)
5865
+ * const baseEl = document.querySelector('base')
5866
+ * base = (baseEl && baseEl.getAttribute('href')) || '/'
5867
+ *
5868
+ * Issue: https://github.com/micro-zoe/micro-app/issues/1335
5869
+ */
5870
+ const result = (_a = appInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.querySelector(selectors);
5871
+ return result || selectors === 'base' ? result : rawMicroQuerySelector.call(microDocument, selectors);
5859
5872
  }
5860
5873
  function querySelectorAll(selectors) {
5861
5874
  var _a, _b;
@@ -5866,7 +5879,7 @@ function patchDocumentPrototype(appName, microAppWindow) {
5866
5879
  return rawMicroQuerySelectorAll.call(_this, selectors);
5867
5880
  }
5868
5881
  const result = (_b = (_a = appInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.querySelectorAll(selectors)) !== null && _b !== void 0 ? _b : [];
5869
- return result.length ? result : rawMicroQuerySelectorAll.call(microDocument, selectors);
5882
+ return result.length || selectors === 'base' ? result : rawMicroQuerySelectorAll.call(microDocument, selectors);
5870
5883
  }
5871
5884
  microRootDocument.prototype.querySelector = querySelector;
5872
5885
  microRootDocument.prototype.querySelectorAll = querySelectorAll;
@@ -5899,8 +5912,9 @@ function patchDocumentPrototype(appName, microAppWindow) {
5899
5912
  if (isUniqueElement(key) ||
5900
5913
  isInvalidQuerySelectorKey(key)) {
5901
5914
  return rawMicroGetElementsByTagName.call(_this, key);
5915
+ // just script, not base
5902
5916
  }
5903
- else if (/^script|base$/i.test(key)) {
5917
+ else if (/^script$/i.test(key)) {
5904
5918
  return rawMicroGetElementsByTagName.call(microDocument, key);
5905
5919
  }
5906
5920
  try {
@@ -6366,7 +6380,7 @@ class IframeSandbox {
6366
6380
  this.escapeProperties = [];
6367
6381
  // Properties escape to rawWindow, cleared when unmount
6368
6382
  this.escapeKeys = new Set();
6369
- // 初始化和每次跳转时都要更新basehref
6383
+ // Update the base.href when initial and each redirect
6370
6384
  this.updateIframeBase = () => {
6371
6385
  var _a;
6372
6386
  // origin must be child app origin
@@ -8270,8 +8284,7 @@ function defineElement(tagName) {
8270
8284
  if (this.legalAttribute(attr, newVal) &&
8271
8285
  this[attr === ObservedAttrName.NAME ? 'appName' : 'appUrl'] !== newVal) {
8272
8286
  if (attr === ObservedAttrName.URL && (!this.appUrl ||
8273
- !this.connectStateMap.get(this.connectedCount) // TODO: 这里的逻辑可否再优化一下
8274
- )) {
8287
+ !this.connectStateMap.get(this.connectedCount))) {
8275
8288
  newVal = formatAppURL(newVal, this.appName);
8276
8289
  if (!newVal) {
8277
8290
  return logError(`Invalid attribute url ${newVal}`, this.appName);
@@ -8280,13 +8293,11 @@ function defineElement(tagName) {
8280
8293
  this.handleInitialNameAndUrl();
8281
8294
  }
8282
8295
  else if (attr === ObservedAttrName.NAME && (!this.appName ||
8283
- !this.connectStateMap.get(this.connectedCount) // TODO: 这里的逻辑可否再优化一下
8284
- )) {
8296
+ !this.connectStateMap.get(this.connectedCount))) {
8285
8297
  const formatNewName = formatAppName(newVal);
8286
8298
  if (!formatNewName) {
8287
8299
  return logError(`Invalid attribute name ${newVal}`, this.appName);
8288
8300
  }
8289
- // TODO: 当micro-app还未插入文档中就修改name,逻辑可否再优化一下
8290
8301
  if (this.cacheData) {
8291
8302
  microApp.setData(formatNewName, this.cacheData);
8292
8303
  this.cacheData = null;