Skip to content

Commit a9a0bb6

Browse files
committed
fix(unmount): call unmount callback when switching between MFEs
1 parent 31ec29b commit a9a0bb6

File tree

13 files changed

+41
-32
lines changed

13 files changed

+41
-32
lines changed

app1/src/bootstrap.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const mount = ({
1717
const router = createRouter({ strategy: routingStrategy, initialPathname });
1818
const root = createRoot(mountPoint);
1919
root.render(<RouterProvider router={router} />);
20+
21+
return () => queueMicrotask(() => root.unmount());
2022
};
2123

2224
export { mount };

app1/src/components/NavigationManager.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { ReactElement, useEffect } from "react";
2-
import { useLocation, useNavigate } from "react-router-dom";
2+
import { matchRoutes, useLocation, useNavigate } from "react-router-dom";
3+
import { routes } from "../routing/routes";
34

45
interface NavigationManagerProps {
56
children: ReactElement;
@@ -12,7 +13,7 @@ export function NavigationManager({ children }: NavigationManagerProps) {
1213
useEffect(() => {
1314
function shellNavigationHandler(event: Event) {
1415
const pathname = (event as CustomEvent<string>).detail;
15-
if (location.pathname === pathname) {
16+
if (location.pathname === pathname || !matchRoutes(routes, { pathname })) {
1617
return;
1718
}
1819
navigate(pathname);

app2/src/bootstrap.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const mount = ({
1717
const router = createRouter({ strategy: routingStrategy, initialPathname });
1818
const root = createRoot(mountPoint);
1919
root.render(<RouterProvider router={router} />);
20+
21+
return () => queueMicrotask(() => root.unmount());
2022
};
2123

2224
export { mount };

app2/src/components/NavigationManager.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { ReactElement, useEffect } from 'react';
2-
import { useLocation, useNavigate } from 'react-router-dom';
2+
import { matchRoutes, useLocation, useNavigate } from 'react-router-dom';
3+
import { routes } from '../routing/routes';
34

45
interface NavigationManagerProps {
56
children: ReactElement;
@@ -12,7 +13,7 @@ export function NavigationManager({ children }: NavigationManagerProps) {
1213
useEffect(() => {
1314
function shellNavigationHandler(event: Event) {
1415
const pathname = (event as CustomEvent<string>).detail;
15-
if (location.pathname === pathname) {
16+
if (location.pathname === pathname || !matchRoutes(routes, { pathname })) {
1617
return;
1718
}
1819
navigate(pathname);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22
import { Link } from 'react-router-dom';
33

4-
export function Page1() {
4+
export function PageA() {
55
return (
66
<React.Fragment>
77
<div>Page 1 from App2</div>
8-
<Link to="/page-2">Go to Page 2</Link>
8+
<Link to="/page-b">Go to Page B</Link>
99
</React.Fragment>
1010
);
1111
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22
import { Link } from 'react-router-dom';
33

4-
export function Page2() {
4+
export function PageB() {
55
return (
66
<React.Fragment>
7-
<div>Page 2 from App2</div>
8-
<Link to="/page-1">Go to Page 1</Link>
7+
<div>Page B from App2</div>
8+
<Link to="/page-a">Go to Page A</Link>
99
</React.Fragment>
1010
);
1111
}

app2/src/routing/routes.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22
import { Outlet } from "react-router-dom";
33
import { NavigationManager } from "../components/NavigationManager";
4-
import { Page1 } from "../pages/Page1";
5-
import { Page2 } from "../pages/Page2";
4+
import { PageA } from "../pages/PageA";
5+
import { PageB } from "../pages/PageB";
66

77
export const routes = [
88
{
@@ -15,15 +15,15 @@ export const routes = [
1515
children: [
1616
{
1717
index: true,
18-
element: <Page1 />,
18+
element: <PageA />,
1919
},
2020
{
21-
path: "page-1",
22-
element: <Page1 />,
21+
path: "page-a",
22+
element: <PageA />,
2323
},
2424
{
25-
path: "page-2",
26-
element: <Page2 />,
25+
path: "page-b",
26+
element: <PageB />,
2727
},
2828
],
2929
},

shell/src/App.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import React, { Suspense } from "react";
1+
import React from "react";
22
import "./index.css";
33
import { Router } from "./routing/Router";
44

55
export const App = () => (
6-
<Suspense fallback={<div>Loading...</div>}>
7-
<Router />
8-
</Suspense>
6+
<Router />
97
);

shell/src/components/App1.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ export default () => {
4545
);
4646

4747
const isFirstRunRef = useRef(true);
48+
const unmountRef = useRef(() => {});
4849
// Mount app1 MFE
4950
useEffect(
5051
() => {
5152
if (!isFirstRunRef.current) {
5253
return;
5354
}
54-
mount({
55+
unmountRef.current = mount({
5556
mountPoint: wrapperRef.current!,
5657
initialPathname: location.pathname.replace(
5758
app1Basename,
@@ -63,5 +64,7 @@ export default () => {
6364
[location],
6465
);
6566

67+
useEffect(() => unmountRef.current, []);
68+
6669
return <div ref={wrapperRef} id="app1-mfe" />;
6770
};

shell/src/components/App2.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ export default () => {
4545
);
4646

4747
const isFirstRunRef = useRef(true);
48+
const unmountRef = useRef(() => {});
4849
// Mount app1 MFE
4950
useEffect(
5051
() => {
5152
if (!isFirstRunRef.current) {
5253
return;
5354
}
54-
mount({
55+
unmountRef.current = mount({
5556
mountPoint: wrapperRef.current!,
5657
initialPathname: location.pathname.replace(
5758
app2Basename,
@@ -63,5 +64,8 @@ export default () => {
6364
[location],
6465
);
6566

67+
useEffect(() => unmountRef.current, []);
68+
69+
6670
return <div ref={wrapperRef} id="app2-mfe" />;
6771
};

0 commit comments

Comments
 (0)