Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重复进入子应用时,子应用获取到的路由错误 #220

Closed
viviprprpr opened this issue Jan 8, 2020 · 8 comments
Closed

重复进入子应用时,子应用获取到的路由错误 #220

viviprprpr opened this issue Jan 8, 2020 · 8 comments
Assignees

Comments

@viviprprpr
Copy link

What happens?

重复进入子应用时,子应用获取到的路由错误

最小可复现仓库

https://github.com/viviprprpr/personal-test-umi-plugin-qiankun.git
万分感谢🙏

复现步骤,错误日志以及相关配置

点击进入子应用,浏览器点击返回,连续操作三次必定报警告,然后再点击进入子应用,浏览器路由变了,但是子应用获取到的还是上一个。

相关环境信息

  • qiankun 版本
    1.3.4
  • 浏览器版本
    chrome 79.0.3945.88
  • 操作系统
    mac
@Deturium
Copy link
Contributor

Deturium commented Jan 9, 2020

你的问题描述不是很清楚,下次请给一下错误日志和示意
我试了一下,在主子应用间反复横跳的话有几率触发这个:

index.js:1 Warning: You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "/" to begin with "/knowledgespace".

随后再尝试跳往子应用的话会出现 404 的错误页

你说的是这个问题吗?

解决方法

src/app.js 的主应用设置中,开启 jsSandBox: true

这样子应用的错误就不会影响主应用了

问题原因

umi 的 history 路由是依赖于 history 库的 createBrowserHistory 的,创建 history 实例的时候有传入一个 basename,而在子应用中,你的 basename 是 /knowledgespace,因此当你尝试从子应用中后退的时(前往 /)会触发错误

你可以通过 window.routerBasewindow.g_history 来查看 umi 的 history 实例的情况来验证这一点

@viviprprpr
Copy link
Author

viviprprpr commented Jan 10, 2020

@Deturium 您好,是这个问题。

但是因为我需要兼容ie11,根据文档说明需要jsSandBox: false,所以有没有其他办法能够解决。

而且经过测试,当jsSandBox: true时,确实不会404了,但是有时候需要点两次浏览器返回才能正常返回

@Deturium
Copy link
Contributor

不用 jsSandbox 的话,就得手动避免全局变量的冲突了,你这里就是要处理好 history

一个可行的思路是,手动使用 history/createBrowserHistory 来创建 history 对象,让各个子应用都使用同一个 history 来做路由,由此保证他们的路由都能正常工作

比如这样:https://alili.tech/archive/5ff0b366/#%E5%8F%AA%E6%9C%89%E4%B8%80%E4%B8%AAhistory

比较麻烦,得做不少定制化工作

@viviprprpr
Copy link
Author

那这个后续会有优化或者jsSandbox兼容IE的计划么

现在可以暂时主应用使用browser,子应用使用hash,但是感觉不太好

@Deturium
Copy link
Contributor

有计划,见 #182

@viviprprpr
Copy link
Author

好的,非常感谢🙏

@laixstx
Copy link

laixstx commented Mar 4, 2020

你的问题描述不是很清楚,下次请给一下错误日志和示意
我试了一下,在主子应用间反复横跳的话有几率触发这个:

index.js:1 Warning: You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "/" to begin with "/knowledgespace".

随后再尝试跳往子应用的话会出现 404 的错误页

你说的是这个问题吗?

解决方法

src/app.js 的主应用设置中,开启 jsSandBox: true

这样子应用的错误就不会影响主应用了

问题原因

umi 的 history 路由是依赖于 history 库的 createBrowserHistory 的,创建 history 实例的时候有传入一个 basename,而在子应用中,你的 basename 是 /knowledgespace,因此当你尝试从子应用中后退的时(前往 /)会触发错误

你可以通过 window.routerBasewindow.g_history 来查看 umi 的 history 实例的情况来验证这一点

@Deturium
如果 history: 'hash',设置了 jsSandBox: true,但是从子应用切换到主应用,并在之后每次切换应用路由时,都会有类似报错:

Warning: You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "/" to begin with "/app1".

image

umi-plugin-qiankun 的自带 examples,将 master、app1、app2、app3 的 history 改成 hash, 就有这个问题:https://github.com/umijs/umi-plugin-qiankun

@topwood
Copy link

topwood commented Apr 27, 2020

same +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants