frontend: support serving the app under a sub-path - #20
Open
albertotb wants to merge 1 commit into
Open
Conversation
A root-only build: no Vite `base`, no router `basename`, and an API base hardcoded to /api. Served from anything other than the domain root the bundle requests /assets/... instead of <prefix>/assets/..., so the page renders blank — the reason the CI preview artifact needed a history-rewrite shim to show anything. Thread one variable through instead. VITE_BASE_PATH sets Vite's `base`, which becomes import.meta.env.BASE_URL, which the router's basename and the default API base both read; the dev proxy keys off the same value. Unset it and everything resolves to '/' exactly as before. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011texLkDBELWbXsBf6San3M
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The blank-page bug I hit building the CI preview artifact, fixed properly.
The bug
The build is root-only: no
baseinvite.config.ts, a bare<BrowserRouter>, and an API base hardcoded to/api. Served from anything but the domain root,index.htmlasks for/assets/index-*.js, which isn't there — blank page, two 404s in the console. That's why the preview artifact needed ahistory.replaceStateshim to render at all.The fix
One variable, threaded through:
VITE_BASE_PATH→ Vite'sbase→import.meta.env.BASE_URL→ the router'sbasenameand the default API base. The dev proxy keys off the same value, sonpm run devunder a prefix proxies<prefix>/apicorrectly too. Unset, everything resolves to/and nothing changes.VITE_BASE_PATH=/myapp/ npm run build # or: docker build --build-arg VITE_BASE_PATH=/myapp/ .A trailing slash is added if you leave it off, matching how Vite normalises
base.Verified in a real browser
Both builds served from the same nginx, headless Chromium,
/aboutloaded directly:/old/(today's behaviour)VITE_BASE_PATH=/myapp/build under/myapp/AboutReact Template HOME ABOUT About This page exists…404 (Not Found)/assets/index-*.js→ 404/myapp/assets/index-*.js→ 200Also checked: clicking ABOUT from
/myapp/client-side navigates to/myapp/aboutand renders, and the home page's version chip requests/myapp/api/rather than/api/.Root build output is byte-identical in its asset URLs (
src="/assets/index-DvrTU-L4.js"before and after), so the default path is untouched.npm run lint,format:check,testandbuildall pass.Notes
vite.config.tsbecomes the function form so it can callloadEnv— that's what letsVITE_BASE_PATHcome from an.envfile, the shell, or a Docker build arg, the same three ways asVITE_API_URL. The proxy block itself is unchanged apart from the base-aware key.`${BASE_URL}api`. At the root that is/api, exactly as before;VITE_API_URLstill overrides it entirely.frontend/README.md. No nginx change here:docker/nginx.confserves the root case, which is the template's default.Independent of #16 and #19 — no overlapping files.
🤖 Generated with Claude Code
https://claude.ai/code/session_011texLkDBELWbXsBf6San3M
Generated by Claude Code