From c37349587cf23d00227d37549282ccd74d9406f6 Mon Sep 17 00:00:00 2001 From: Joshua Oosthuizen Date: Fri, 20 Nov 2026 23:41:43 +0200 Subject: [PATCH] V1.32.1: the API can live under the site's own address - Live updates connect to the path the API sits under (such as /api behind the site's own nginx), not only to an API on a host of its own: Socket.io reads a URL's path as a namespace, so the path now goes into its `path` - README: how to serve the API under the site's address (.env values and the nginx location block) - All packages at version 1.32.1 Co-Authored-By: Claude Opus 5.5 --- README.md | 27 +++++++++++++++++++++++++ backend/package.json | 2 +- frontend/package.json | 2 +- frontend/src/hooks/useCampaignSocket.ts | 8 +++++++- package-lock.json | 8 ++++---- package.json | 2 +- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1db5594..9bef407 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,9 @@ including a character sheet or an encounter) and **Account**. items) and by level in the class that gives them. - **Armor holds Dexterity back** as in the rules: all of it counts toward AC in light armor, at most +2 in medium, none in heavy. +- **The API can live under the site's own address**, such as `/api` + behind the same nginx, as well as on a host of its own (see "Serving the + API under the site's address"). - **Battles that keep to the fight.** While a battle is under way the scene description shows only between rounds, cards' details start closed (the spotlight, or on a phone the card on turn, has them), and the DM's setup, @@ -234,6 +237,30 @@ old data model and pages that use anything new fail to load (for example "Could not load this encounter"). Change your password from the account page ("Account" in the navigation). +### Serving the API under the site's address + +The API can sit on its own host or under a path of the site, such as +`https://example.com/api`. For the second, set `VITE_API_URL=/api` in +`frontend/.env` before building (Vite bakes it in), set `CORS_ORIGIN` in +`backend/.env` to the site's address, and pass the path on in the site's +nginx config: + +```nginx +location /api/ { + proxy_pass http://127.0.0.1:4000/; # the trailing slash strips /api + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_read_timeout 3600s; +} +``` + +The backend keeps seeing `/auth`, `/campaigns` and `/socket.io/` as before; +the live updates connect to `/api/socket.io`. + ## Tests ```bash diff --git a/backend/package.json b/backend/package.json index 7421f04..3f688f3 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "backend", - "version": "1.32.0", + "version": "1.32.1", "private": true, "type": "commonjs", "main": "src/index.js", diff --git a/frontend/package.json b/frontend/package.json index 3977e15..edcd94a 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,7 +1,7 @@ { "name": "frontend", "private": true, - "version": "1.32.0", + "version": "1.32.1", "type": "module", "scripts": { "dev": "vite", diff --git a/frontend/src/hooks/useCampaignSocket.ts b/frontend/src/hooks/useCampaignSocket.ts index ca03491..c3780d1 100644 --- a/frontend/src/hooks/useCampaignSocket.ts +++ b/frontend/src/hooks/useCampaignSocket.ts @@ -13,7 +13,13 @@ function getSocket(token: string): Socket { // Starts on plain requests and moves up to a websocket when one gets // through. Asking for the websocket first leaves no way back: behind a // proxy that does not pass the upgrade, nothing live arrives at all. - sharedSocket = io(API_URL, { auth: { token } }); + // + // Socket.io reads the path of a URL as a namespace, not as where the + // server lives. When the API sits under a path (such as /api behind the + // site's own nginx), that path has to go into `path` instead. + const url = new URL(API_URL, window.location.origin); + const base = url.pathname.replace(/\/$/, ''); + sharedSocket = io(url.origin, { path: `${base}/socket.io`, auth: { token } }); } return sharedSocket; } diff --git a/package-lock.json b/package-lock.json index 3991102..b89dfbf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,19 @@ { "name": "dnd-campaign-manager", - "version": "1.32.0", + "version": "1.32.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dnd-campaign-manager", - "version": "1.32.0", + "version": "1.32.1", "workspaces": [ "backend", "frontend" ] }, "backend": { - "version": "1.32.0", + "version": "1.32.1", "dependencies": { "@prisma/adapter-pg": "^7.10.0", "@prisma/client": "^7.10.0", @@ -33,7 +33,7 @@ } }, "frontend": { - "version": "1.32.0", + "version": "1.32.1", "dependencies": { "@tanstack/react-query": "^5.103.2", "axios": "^1.20.0", diff --git a/package.json b/package.json index f57c8ab..bf346ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dnd-campaign-manager", - "version": "1.32.0", + "version": "1.32.1", "private": true, "description": "D&D 5e campaign manager — monorepo root", "workspaces": [