feat(deploy): derive the API origin from the page, not from a pinned env var
The browser hard-required API_ORIGIN, so every move of the server — tailnet today, the 192.168.1.0 office LAN later, a temporary demo domain in between — meant editing the deploy env and redeploying. Worse, an http:// API origin on a page served over TLS is blocked outright as mixed active content, which is what broke the demo on https://jorgecuadros.freakma.com. The browser now derives the origin from window.location the way a PHP app would: same host on port 3001 over plain HTTP, or the same-origin /api path under https (the reverse proxy strips the prefix). API_ORIGIN survives as an optional override for a deployment that genuinely splits the two hosts, and SSR still reads process.env because a derived origin is browser-only. WEB_ORIGIN becomes a comma-separated list to match: one deployment is now reached under several origins, and a credentialed fetch from an unlisted one gets no CORS headers and fails. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+13
-1
@@ -60,7 +60,19 @@ async function bootstrap() {
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
app.enableCors({ credentials: true, origin: process.env.WEB_ORIGIN ?? "http://localhost:3000" });
|
||||
// The same deployment is reached under several origins — the office LAN IP,
|
||||
// the tailnet name, the demo domain — and the browser derives the API origin
|
||||
// from whichever one served the page (apps/web/src/lib/api.ts). So WEB_ORIGIN
|
||||
// is a comma-separated LIST, not a single value. A request whose Origin is
|
||||
// not listed gets no CORS headers and the credentialed fetch fails, so add an
|
||||
// entry when a new way of reaching the app is introduced. Same-origin setups
|
||||
// (web and API behind one proxy) never hit CORS at all.
|
||||
const webOrigins = (process.env.WEB_ORIGIN ?? "http://localhost:3000")
|
||||
.split(",")
|
||||
.map((o) => o.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
app.enableCors({ credentials: true, origin: webOrigins });
|
||||
|
||||
const port = process.env.PORT ? Number(process.env.PORT) : 3001;
|
||||
await app.listen(port);
|
||||
|
||||
Reference in New Issue
Block a user