Skip to content

Commit 26abed5

Browse files
authored
fix: garfish plugin popstate not trigger (#7668)
1 parent 32d27c9 commit 26abed5

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

.changeset/common-bats-agree.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/plugin-garfish': patch
3+
---
4+
5+
fix: garfish plugin popstate not trigger
6+
7+
fix: 修复 garfish 插件 popstate 事件未触发问题

packages/runtime/plugin-garfish/src/runtime/utils/apps.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { RuntimeReactContext } from '@meta/runtime';
22
import Garfish, { type interfaces } from 'garfish';
33
// The loading logic of the current component refers to react-loadable https://github.com/jamiebuilds/react-loadable
44
import type React from 'react';
5-
import { useContext, useEffect, useRef, useState } from 'react';
5+
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
66
import { generateSubAppContainerKey, logger } from '../../util';
77
import { Loadable, type MicroProps } from '../loadable';
88
// import Loadable from 'react-loadable';
@@ -43,8 +43,6 @@ function getAppInstance(
4343
appInfo: ModulesInfo[number],
4444
manifest?: Manifest,
4545
) {
46-
let locationHref = '';
47-
4846
// Create a callback registry center
4947
// This object is within the closure of getAppInstance and will only be created once for the same sub-app.
5048
// It will be shared by all MicroApp component instances to store the state setter of the currently active component
@@ -56,6 +54,8 @@ function getAppInstance(
5654

5755
function MicroApp(props: MicroProps) {
5856
const appRef = useRef<interfaces.App | null>(null);
57+
const locationHrefRef = useRef('');
58+
5959
const domId = generateSubAppContainerKey(appInfo);
6060
const [{ component: SubModuleComponent }, setSubModuleComponent] =
6161
useState<{
@@ -135,16 +135,27 @@ or directly pass the "basename":
135135
basename = props.basename;
136136
}
137137

138+
const locationPathname = useMemo(
139+
function () {
140+
return location ? location.pathname : '';
141+
},
142+
[location ? location.pathname : ''],
143+
);
144+
138145
// useLocation is NECESSARY in syncPopStateEvent
139146
useEffect(() => {
140-
if (location && locationHref !== location.pathname && !Garfish.running) {
141-
locationHref = location.pathname;
147+
if (
148+
location &&
149+
locationHrefRef.current !== location.pathname &&
150+
!Garfish.running
151+
) {
152+
locationHrefRef.current = location.pathname;
142153
const popStateEvent = new PopStateEvent('popstate');
143154
(popStateEvent as any).garfish = true;
144155
dispatchEvent(popStateEvent);
145156
logger(`MicroApp Garfish.loadApp popstate`);
146157
}
147-
}, [location]);
158+
}, [locationPathname]);
148159

149160
useEffect(() => {
150161
// [MODIFIED] Register the current instance's state setter when the component mounts

0 commit comments

Comments
 (0)