fix (web): proxy /api requests to backend in production hooks#51
Merged
Conversation
The SvelteKit server hook was calling resolve(event) for /api/* paths, which caused SvelteKit to look for a matching route under src/routes/api/. No such route exists, so every API call returned 404 in production. In dev mode the Vite proxy handles this, but in production the Node adapter (node build/index.js) must forward /api requests to the FastAPI backend itself. The hook now fetches from PRIVATE_API_URL and relays Set-Cookie headers so auth tokens reach the browser. Closes login returning [404] POST /api/v1/auth/login.
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.
Summary
Signing in from the production deployment returned
[404] POST /api/v1/auth/login. The login form sendsPOST /api/v1/auth/loginto the web container (SvelteKit Node server on port 3000), but the server hook calledresolve(event)— which has SvelteKit search for a route undersrc/routes/api/. No such route exists, so every API call got a 404.In dev mode, Vite's proxy config (
vite.config.js) handles this transparently. That proxy does not run in production (node build/index.js).Fix
web/src/hooks.server.ts— the/api/*branch now fetches from the FastAPI backend (PRIVATE_API_URL, defaulthttp://api:8000) instead of callingresolve(event). Set-Cookie headers from the API are forwarded so auth tokens(access_token, refresh_token) reach the browser.
Request flow
Before:
browser → web:3000 → SvelteKit route lookup → 404After:
browser → web:3000 → fetch(api:8000) → API response → browser