File tree Expand file tree Collapse file tree 7 files changed +20
-1
lines changed Expand file tree Collapse file tree 7 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 1
- # Multi Page Example
1
+ # Multi App Example
2
+
3
+ This example demonstrates how to build a site with multiple React Router apps by mounting each at a URL pathname prefix using the ` <Router basename> ` prop. This essentially decouples the apps from each other and allows them to be portable and even deployed separately.
2
4
3
5
## Preview
4
6
Original file line number Diff line number Diff line change @@ -34,6 +34,10 @@ function Layout() {
34
34
< Link to = "/about" > About</ Link >
35
35
</ li >
36
36
< li >
37
+ { /* Use a normal <a> when linking to the "Inbox" app so the browser
38
+ does a full document reload, which is what we want when exiting
39
+ this app and entering another so we execute its entry point in
40
+ inbox/main.jsx. */ }
37
41
< a href = "/inbox" > Inbox</ a >
38
42
</ li >
39
43
</ ul >
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import HomeApp from "./App";
6
6
7
7
ReactDOM . render (
8
8
< React . StrictMode >
9
+ { /* No basename for this router. This app renders at the root / URL. */ }
9
10
< BrowserRouter >
10
11
< HomeApp />
11
12
</ BrowserRouter >
Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ import { NoMatch } from "./no-match";
6
6
export default function InboxApp ( ) {
7
7
return (
8
8
< Routes >
9
+ { /* Routes in this app don't need to worry about which URL prefix they are
10
+ mounted at. They can just assume they are mounted at /. Then, if they
11
+ are moved under a different basename later on, all routes and links will
12
+ continue to work. */ }
9
13
< Route path = "/" element = { < Layout /> } >
10
14
< Route index element = { < Inbox /> } />
11
15
< Route path = ":id" element = { < Message /> } />
@@ -22,6 +26,10 @@ function Layout() {
22
26
< nav >
23
27
< ul >
24
28
< li >
29
+ { /* Using a normal link here will cause the browser to reload the
30
+ document when following this link, which is exactly what we want
31
+ when transitioning to the "Home" app so we execute its entry
32
+ point (see home/main.jsx). */ }
25
33
< a href = "/" > Home</ a >
26
34
</ li >
27
35
< li >
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ import InboxApp from "./App";
6
6
7
7
ReactDOM . render (
8
8
< React . StrictMode >
9
+ { /* "Mount" this app under the /inbox URL pathname. All routes and links
10
+ are relative to this name. */ }
9
11
< BrowserRouter basename = "inbox" >
10
12
< InboxApp />
11
13
</ BrowserRouter >
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ async function createServer() {
26
26
app . use ( "*" , async ( req , res ) => {
27
27
let url = req . originalUrl ;
28
28
29
+ // Use a separate HTML file for the "Inbox" app.
29
30
let appDirectory = url . startsWith ( "/inbox" ) ? "inbox" : "" ;
30
31
let htmlFileToLoad ;
31
32
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ export default defineConfig({
17
17
] ,
18
18
build : {
19
19
rollupOptions : {
20
+ // Build two separate bundles, one for each app.
20
21
input : {
21
22
main : path . resolve ( __dirname , "index.html" ) ,
22
23
inbox : path . resolve ( __dirname , "inbox/index.html" )
You can’t perform that action at this time.
0 commit comments