Skip to content

Commit e5a258a

Browse files
authored
Dev: redirect bare subpath /app → /app/ when RUN_SUBPATH is set (Stirling-Tools#6934)
## Problem With `RUN_SUBPATH=app`, the app is served under base `/app/`. Vite serves `index.html` at `/app/` and redirects `/` → `/app/`, but a bare **`/app`** (no trailing slash) returns **404** — so you had to type `localhost:5173/app/` to load the app. `/app` should work too. ## Fix A small dev + preview middleware that **301-redirects `/app` → `/app/`** (query string preserved), so either form loads the app. Only active when `RUN_SUBPATH` is set; no-op otherwise. Also routed the vite `base` through the same slash-stripped `runSubpath` value the middleware uses, so a stray `RUN_SUBPATH=/app/` can't produce a doubled `//app//` base. ## Verified (dev server + prod build, `RUN_SUBPATH=app`) | Request | Before | After | |---|---|---| | `GET /app` | 404 | **301 → `/app/`** | | `GET /app?foo=1` | 404 | **301 → `/app/?foo=1`** (query kept) | | `GET /app/` | 200 | 200 (unchanged) | | `GET /` | 302 → `/app/` | 302 → `/app/` (unchanged) | Production build under the subpath still emits `<base href="/app/">` and `/app/assets/...`. Lint + format green.
1 parent c64369e commit e5a258a

1 file changed

Lines changed: 39 additions & 3 deletions

File tree

frontend/editor/vite.config.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { constants, brotliCompress, gzip } from "node:zlib";
66
import { fileURLToPath } from "node:url";
77
import { promisify } from "node:util";
88
import { defineConfig, loadEnv } from "vite";
9-
import type { PluginOption } from "vite";
9+
import type { Connect, PluginOption } from "vite";
1010
import tsconfigPaths from "vite-tsconfig-paths";
1111
import { viteStaticCopy } from "vite-plugin-static-copy";
1212

@@ -129,6 +129,38 @@ function prerenderOgPlugin(): PluginOption {
129129
};
130130
}
131131

132+
/**
133+
* When the app is served under a subpath (RUN_SUBPATH → base like "/app/"), Vite
134+
* serves index.html at "/app/" and redirects "/" → the base, but a bare "/app"
135+
* (no trailing slash) 404s. This middleware redirects "/app" → "/app/" so either
136+
* form loads the app in dev and `vite preview`. Query strings are preserved.
137+
*/
138+
function subpathBareRedirectPlugin(subpath: string): PluginOption {
139+
const bare = `/${subpath}`;
140+
const withSlash = `${bare}/`;
141+
const redirect: Connect.NextHandleFunction = (req, res, next) => {
142+
const url = req.url ?? "";
143+
const q = url.indexOf("?");
144+
const pathname = q === -1 ? url : url.slice(0, q);
145+
if (pathname === bare) {
146+
res.statusCode = 301;
147+
res.setHeader("Location", withSlash + (q === -1 ? "" : url.slice(q)));
148+
res.end();
149+
return;
150+
}
151+
next();
152+
};
153+
return {
154+
name: "subpath-bare-redirect",
155+
configureServer(server) {
156+
server.middlewares.use(redirect);
157+
},
158+
configurePreviewServer(server) {
159+
server.middlewares.use(redirect);
160+
},
161+
};
162+
}
163+
132164
// NOTE: cloud/ is a SHARED layer, not a runnable build flavor — it's compiled
133165
// into the saas and desktop builds. It has no entry here and no vite tsconfig;
134166
// it is only typechecked standalone via editor/src/cloud/tsconfig.json
@@ -178,6 +210,9 @@ export default defineConfig(async ({ mode }) => {
178210

179211
const tsconfigProject = TSCONFIG_MAP[effectiveMode];
180212

213+
// Subpath the app is served under (base becomes "/<runSubpath>/"). Empty = root.
214+
const runSubpath = (env.RUN_SUBPATH || "").replace(/^\/+|\/+$/g, "");
215+
181216
// Backend proxy target: default localhost:8080. Override via BACKEND_URL env var
182217
// so the top-level dev launcher can wire a dynamically-assigned backend port.
183218
const backendUrl = process.env.BACKEND_URL || "http://localhost:8080";
@@ -217,6 +252,7 @@ export default defineConfig(async ({ mode }) => {
217252
return {
218253
plugins: [
219254
react(),
255+
...(runSubpath ? [subpathBareRedirectPlugin(runSubpath)] : []),
220256
tsconfigPaths({
221257
projects: [tsconfigProject],
222258
}),
@@ -330,8 +366,8 @@ export default defineConfig(async ({ mode }) => {
330366
// an absolute base so deep-route asset paths resolve to /assets/...
331367
// Trailing slash required: it becomes `<base href>`, and browsers resolve
332368
// relative URLs (manifest.json, favicon) against the base's *directory*.
333-
base: env.RUN_SUBPATH
334-
? `/${env.RUN_SUBPATH}/`
369+
base: runSubpath
370+
? `/${runSubpath}/`
335371
: process.env.VITE_BUILD_FOR_PREVIEW === "1"
336372
? "/"
337373
: "./",

0 commit comments

Comments
 (0)