File tree Expand file tree Collapse file tree 13 files changed +41
-32
lines changed Expand file tree Collapse file tree 13 files changed +41
-32
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,8 @@ const mount = ({
17
17
const router = createRouter ( { strategy : routingStrategy , initialPathname } ) ;
18
18
const root = createRoot ( mountPoint ) ;
19
19
root . render ( < RouterProvider router = { router } /> ) ;
20
+
21
+ return ( ) => queueMicrotask ( ( ) => root . unmount ( ) ) ;
20
22
} ;
21
23
22
24
export { mount } ;
Original file line number Diff line number Diff line change 1
1
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" ;
3
4
4
5
interface NavigationManagerProps {
5
6
children : ReactElement ;
@@ -12,7 +13,7 @@ export function NavigationManager({ children }: NavigationManagerProps) {
12
13
useEffect ( ( ) => {
13
14
function shellNavigationHandler ( event : Event ) {
14
15
const pathname = ( event as CustomEvent < string > ) . detail ;
15
- if ( location . pathname === pathname ) {
16
+ if ( location . pathname === pathname || ! matchRoutes ( routes , { pathname } ) ) {
16
17
return ;
17
18
}
18
19
navigate ( pathname ) ;
Original file line number Diff line number Diff line change @@ -17,6 +17,8 @@ const mount = ({
17
17
const router = createRouter ( { strategy : routingStrategy , initialPathname } ) ;
18
18
const root = createRoot ( mountPoint ) ;
19
19
root . render ( < RouterProvider router = { router } /> ) ;
20
+
21
+ return ( ) => queueMicrotask ( ( ) => root . unmount ( ) ) ;
20
22
} ;
21
23
22
24
export { mount } ;
Original file line number Diff line number Diff line change 1
1
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' ;
3
4
4
5
interface NavigationManagerProps {
5
6
children : ReactElement ;
@@ -12,7 +13,7 @@ export function NavigationManager({ children }: NavigationManagerProps) {
12
13
useEffect ( ( ) => {
13
14
function shellNavigationHandler ( event : Event ) {
14
15
const pathname = ( event as CustomEvent < string > ) . detail ;
15
- if ( location . pathname === pathname ) {
16
+ if ( location . pathname === pathname || ! matchRoutes ( routes , { pathname } ) ) {
16
17
return ;
17
18
}
18
19
navigate ( pathname ) ;
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import { Link } from 'react-router-dom' ;
3
3
4
- export function Page1 ( ) {
4
+ export function PageA ( ) {
5
5
return (
6
6
< React . Fragment >
7
7
< 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 >
9
9
</ React . Fragment >
10
10
) ;
11
11
}
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import { Link } from 'react-router-dom' ;
3
3
4
- export function Page2 ( ) {
4
+ export function PageB ( ) {
5
5
return (
6
6
< 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 >
9
9
</ React . Fragment >
10
10
) ;
11
11
}
Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
2
import { Outlet } from "react-router-dom" ;
3
3
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 " ;
6
6
7
7
export const routes = [
8
8
{
@@ -15,15 +15,15 @@ export const routes = [
15
15
children : [
16
16
{
17
17
index : true ,
18
- element : < Page1 /> ,
18
+ element : < PageA /> ,
19
19
} ,
20
20
{
21
- path : "page-1 " ,
22
- element : < Page1 /> ,
21
+ path : "page-a " ,
22
+ element : < PageA /> ,
23
23
} ,
24
24
{
25
- path : "page-2 " ,
26
- element : < Page2 /> ,
25
+ path : "page-b " ,
26
+ element : < PageB /> ,
27
27
} ,
28
28
] ,
29
29
} ,
Original file line number Diff line number Diff line change 1
- import React , { Suspense } from "react" ;
1
+ import React from "react" ;
2
2
import "./index.css" ;
3
3
import { Router } from "./routing/Router" ;
4
4
5
5
export const App = ( ) => (
6
- < Suspense fallback = { < div > Loading...</ div > } >
7
- < Router />
8
- </ Suspense >
6
+ < Router />
9
7
) ;
Original file line number Diff line number Diff line change @@ -45,13 +45,14 @@ export default () => {
45
45
) ;
46
46
47
47
const isFirstRunRef = useRef ( true ) ;
48
+ const unmountRef = useRef ( ( ) => { } ) ;
48
49
// Mount app1 MFE
49
50
useEffect (
50
51
( ) => {
51
52
if ( ! isFirstRunRef . current ) {
52
53
return ;
53
54
}
54
- mount ( {
55
+ unmountRef . current = mount ( {
55
56
mountPoint : wrapperRef . current ! ,
56
57
initialPathname : location . pathname . replace (
57
58
app1Basename ,
@@ -63,5 +64,7 @@ export default () => {
63
64
[ location ] ,
64
65
) ;
65
66
67
+ useEffect ( ( ) => unmountRef . current , [ ] ) ;
68
+
66
69
return < div ref = { wrapperRef } id = "app1-mfe" /> ;
67
70
} ;
Original file line number Diff line number Diff line change @@ -45,13 +45,14 @@ export default () => {
45
45
) ;
46
46
47
47
const isFirstRunRef = useRef ( true ) ;
48
+ const unmountRef = useRef ( ( ) => { } ) ;
48
49
// Mount app1 MFE
49
50
useEffect (
50
51
( ) => {
51
52
if ( ! isFirstRunRef . current ) {
52
53
return ;
53
54
}
54
- mount ( {
55
+ unmountRef . current = mount ( {
55
56
mountPoint : wrapperRef . current ! ,
56
57
initialPathname : location . pathname . replace (
57
58
app2Basename ,
@@ -63,5 +64,8 @@ export default () => {
63
64
[ location ] ,
64
65
) ;
65
66
67
+ useEffect ( ( ) => unmountRef . current , [ ] ) ;
68
+
69
+
66
70
return < div ref = { wrapperRef } id = "app2-mfe" /> ;
67
71
} ;
You can’t perform that action at this time.
0 commit comments