Skip to content

Commit 1bc1586

Browse files
committed
Support configurable base path deployment
1 parent 87f7504 commit 1bc1586

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ npm install
5757
npm run build
5858
```
5959

60+
#### Serving from a sub-path
61+
62+
If you need the app to live under a path prefix (for example `/drawdb`), set `VITE_BASE_PATH` before building or previewing:
63+
64+
```bash
65+
VITE_BASE_PATH=/drawdb npm run build
66+
```
67+
68+
All generated asset links and client-side routes will then resolve relative to `/drawdb`. Make sure your web server rewrites unknown paths back to the built `index.html` so the SPA router can handle them.
69+
6070
### Docker Build
6171

6272
```bash

src/App.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import SettingsContextProvider from "./context/SettingsContext";
88
import NotFound from "./pages/NotFound";
99

1010
export default function App() {
11+
const basename = import.meta.env.BASE_URL.replace(/\/$/, "");
12+
1113
return (
1214
<SettingsContextProvider>
13-
<BrowserRouter>
15+
<BrowserRouter basename={basename || undefined}>
1416
<RestoreScroll />
1517
<Routes>
1618
<Route path="/" element={<LandingPage />} />

vite.config.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { defineConfig } from 'vite'
2-
import react from '@vitejs/plugin-react'
1+
import { defineConfig, loadEnv } from "vite";
2+
import react from "@vitejs/plugin-react";
33

4-
// https://vitejs.dev/config/
5-
export default defineConfig({
6-
plugins: [react()],
7-
})
4+
export default defineConfig(({ mode }) => {
5+
const env = loadEnv(mode, process.cwd(), "");
6+
const rawBase = env.VITE_BASE_PATH || "/";
7+
const base = rawBase.endsWith("/") ? rawBase : `${rawBase}/`;
8+
9+
return {
10+
base,
11+
plugins: [react()],
12+
};
13+
});

0 commit comments

Comments
 (0)