Skip to content

Commit

Permalink
Add some comments to the multi-app example
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Nov 2, 2021
1 parent e5f6122 commit 2ee81d7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion examples/multi-app/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Multi Page Example
# Multi App Example

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.

## Preview

Expand Down
4 changes: 4 additions & 0 deletions examples/multi-app/home/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function Layout() {
<Link to="/about">About</Link>
</li>
<li>
{/* Use a normal <a> when linking to the "Inbox" app so the browser
does a full document reload, which is what we want when exiting
this app and entering another so we execute its entry point in
inbox/main.jsx. */}
<a href="/inbox">Inbox</a>
</li>
</ul>
Expand Down
1 change: 1 addition & 0 deletions examples/multi-app/home/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import HomeApp from "./App";

ReactDOM.render(
<React.StrictMode>
{/* No basename for this router. This app renders at the root / URL. */}
<BrowserRouter>
<HomeApp />
</BrowserRouter>
Expand Down
8 changes: 8 additions & 0 deletions examples/multi-app/inbox/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { NoMatch } from "./no-match";
export default function InboxApp() {
return (
<Routes>
{/* Routes in this app don't need to worry about which URL prefix they are
mounted at. They can just assume they are mounted at /. Then, if they
are moved under a different basename later on, all routes and links will
continue to work. */}
<Route path="/" element={<Layout />}>
<Route index element={<Inbox />} />
<Route path=":id" element={<Message />} />
Expand All @@ -22,6 +26,10 @@ function Layout() {
<nav>
<ul>
<li>
{/* Using a normal link here will cause the browser to reload the
document when following this link, which is exactly what we want
when transitioning to the "Home" app so we execute its entry
point (see home/main.jsx). */}
<a href="/">Home</a>
</li>
<li>
Expand Down
2 changes: 2 additions & 0 deletions examples/multi-app/inbox/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import InboxApp from "./App";

ReactDOM.render(
<React.StrictMode>
{/* "Mount" this app under the /inbox URL pathname. All routes and links
are relative to this name. */}
<BrowserRouter basename="inbox">
<InboxApp />
</BrowserRouter>
Expand Down
1 change: 1 addition & 0 deletions examples/multi-app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async function createServer() {
app.use("*", async (req, res) => {
let url = req.originalUrl;

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

Expand Down
1 change: 1 addition & 0 deletions examples/multi-app/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default defineConfig({
],
build: {
rollupOptions: {
// Build two separate bundles, one for each app.
input: {
main: path.resolve(__dirname, "index.html"),
inbox: path.resolve(__dirname, "inbox/index.html")
Expand Down

0 comments on commit 2ee81d7

Please sign in to comment.