diff --git a/Dockerfile b/Dockerfile index ed35b52f..a4f57061 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,8 @@ COPY package*.json ./ RUN npm ci COPY . . ENV NODE_OPTIONS="--max-old-space-size=4096" +ARG VITE_BASE_PATH=/ +ENV VITE_BASE_PATH=${VITE_BASE_PATH} RUN npm run build # Stage 2: Setup the Nginx Server to serve the app diff --git a/README.md b/README.md index 2b6976c1..f776347b 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,16 @@ npm install npm run build ``` +#### Serving from a sub-path + +If you need the app to live under a path prefix (for example `/drawdb`), set `VITE_BASE_PATH` before building or previewing: + +```bash +VITE_BASE_PATH=/drawdb npm run build +``` + +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. + ### Docker Build ```bash @@ -64,4 +74,11 @@ docker build -t drawdb . docker run -p 3000:80 drawdb ``` +To serve the app from a sub-path in Docker, pass the base path during build: + +```bash +docker build --build-arg VITE_BASE_PATH=/drawdb -t drawdb . +docker run -p 3000:80 drawdb +``` + If you want to enable sharing, set up the [server](https://github.com/drawdb-io/drawdb-server) and environment variables according to `.env.sample`. This is optional unless you need to share files.. diff --git a/src/App.jsx b/src/App.jsx index fcb09811..df1782ec 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -8,9 +8,11 @@ import SettingsContextProvider from "./context/SettingsContext"; import NotFound from "./pages/NotFound"; export default function App() { + const basename = import.meta.env.BASE_URL.replace(/\/$/, ""); + return ( - + } /> diff --git a/vite.config.js b/vite.config.js index 5a33944a..e3a10ac2 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,7 +1,14 @@ -import { defineConfig } from 'vite' -import react from '@vitejs/plugin-react' +/* eslint-env node */ +import { defineConfig, loadEnv } from "vite"; +import react from "@vitejs/plugin-react"; -// https://vitejs.dev/config/ -export default defineConfig({ - plugins: [react()], -}) +export default defineConfig(({ mode }) => { + const env = loadEnv(mode, process.cwd(), ""); + const rawBase = env.VITE_BASE_PATH || "/"; + const base = rawBase.endsWith("/") ? rawBase : `${rawBase}/`; + + return { + base, + plugins: [react()], + }; +});