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

[Bug]: 父应用使用 Link 跳转到子应用,子应用内进行路由跳转后,再次点击 Link 不会触发子应用的路由更新 #5758

Open
kiyass opened this issue May 21, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@kiyass
Copy link

kiyass commented May 21, 2024

版本信息

System:
    OS: macOS 11.7.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 422.70 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Browsers:
    Chrome: 124.0.6367.207
    Safari: 14.1.3
  npmPackages:
    @modern-js/app-tools: 2.49.3 => 2.49.3 
    @modern-js/eslint-config: 2.49.3 => 2.49.3 
    @modern-js/plugin-garfish: 2.49.3 => 2.49.3 
    @modern-js/runtime: 2.49.3 => 2.49.3 
    @modern-js/tsconfig: 2.49.3 => 2.49.3

问题详情

父应用使用 Link 跳转到子应用如 /app1/path,子应用内进行路由跳转如 /app1/home 后,再次点击父应用的 Link( /app1/path )不会触发子应用的路由更新。

image

可能存在问题的源码位置:
https://github.com/web-infra-dev/modern.js/blob/main/packages/runtime/plugin-garfish/src/runtime/utils/apps.tsx#L126
    // useLocation is NECESSARY in syncPopStateEvent
    useEffect(() => {
      if (location && locationHref !== location.pathname && !Garfish.running) {
        locationHref = location.pathname;
        const popStateEvent = new PopStateEvent('popstate');
        (popStateEvent as any).garfish = true;
        dispatchEvent(popStateEvent);
        logger(`MicroApp Garfish.loadApp popstate`);
      }
    }, [location]);

不太了解具体的原理,看到在 MicroApp 中添加如上 hook,如果它监听的是父应用的 location 变化,似乎会带来一个问题,在问题描述的第三步中对于父应用而言,location href 并没有发生变化,所以导致了 PopStateEvent 事件没有被触发。

还有一点是,在 singe-spa 中对于 popstate 事件为 ie11 的兼容处理,因为 ie11 不支持以 new PopStateEvent 的形式定义事件。
对应的源码位置:
https://github.com/single-spa/single-spa/blob/main/src/navigation/navigation-events.js#L108

复现链接

复现步骤

@kiyass kiyass added the bug Something isn't working label May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants