Skip to content

Commit 2ee81d7

Browse files
committed
Add some comments to the multi-app example
1 parent e5f6122 commit 2ee81d7

File tree

7 files changed

+20
-1
lines changed

7 files changed

+20
-1
lines changed

examples/multi-app/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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.
24

35
## Preview
46

examples/multi-app/home/App.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function Layout() {
3434
<Link to="/about">About</Link>
3535
</li>
3636
<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. */}
3741
<a href="/inbox">Inbox</a>
3842
</li>
3943
</ul>

examples/multi-app/home/main.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import HomeApp from "./App";
66

77
ReactDOM.render(
88
<React.StrictMode>
9+
{/* No basename for this router. This app renders at the root / URL. */}
910
<BrowserRouter>
1011
<HomeApp />
1112
</BrowserRouter>

examples/multi-app/inbox/App.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { NoMatch } from "./no-match";
66
export default function InboxApp() {
77
return (
88
<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. */}
913
<Route path="/" element={<Layout />}>
1014
<Route index element={<Inbox />} />
1115
<Route path=":id" element={<Message />} />
@@ -22,6 +26,10 @@ function Layout() {
2226
<nav>
2327
<ul>
2428
<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). */}
2533
<a href="/">Home</a>
2634
</li>
2735
<li>

examples/multi-app/inbox/main.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import InboxApp from "./App";
66

77
ReactDOM.render(
88
<React.StrictMode>
9+
{/* "Mount" this app under the /inbox URL pathname. All routes and links
10+
are relative to this name. */}
911
<BrowserRouter basename="inbox">
1012
<InboxApp />
1113
</BrowserRouter>

examples/multi-app/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async function createServer() {
2626
app.use("*", async (req, res) => {
2727
let url = req.originalUrl;
2828

29+
// Use a separate HTML file for the "Inbox" app.
2930
let appDirectory = url.startsWith("/inbox") ? "inbox" : "";
3031
let htmlFileToLoad;
3132

examples/multi-app/vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default defineConfig({
1717
],
1818
build: {
1919
rollupOptions: {
20+
// Build two separate bundles, one for each app.
2021
input: {
2122
main: path.resolve(__dirname, "index.html"),
2223
inbox: path.resolve(__dirname, "inbox/index.html")

0 commit comments

Comments
 (0)