Serve the production build in Docker so the PWA service worker ships

The Docker app container ran the CRA dev server (`npm run start`), which
never emits the Workbox service worker, so the PWA had no offline cache when
deployed via Docker (native already serves the build via wrolpi-app.service).

- docker/app/Dockerfile: build the bundle (`npm run build`) and serve it with
  `serve build -c /app/serve.json` (mirrors native; serve.json keeps the SPA
  fallback while serving service-worker.js and epub.html directly).
- docker-compose.yml: drop the dev-server `command`/mounts so the base stack
  serves the production build (what end users run).
- docker-compose.dev.yml: new opt-in override restoring the dev server + hot
  reload for local development:
  `docker compose -f docker-compose.yml -f docker-compose.dev.yml up`
- docker/README.md: document the dev vs production run commands.

Verified: served via Caddy, /service-worker.js now returns real JS
(application/javascript) instead of the SPA index.html fallback; the SW
registers, caches inventory, and serves it offline.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-06-22 15:56:40 -06:00
parent 20d2bb8717
commit f87921e2a7
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 47 additions and 14 deletions

22
docker-compose.dev.yml Normal file
View file

@ -0,0 +1,22 @@
# Local development override for the React app.
#
# The base docker-compose.yml serves the production build (so the PWA service worker ships). This override runs
# the Create React App dev server instead, restoring hot reload. The dev server does not emit a service worker —
# that is expected; offline/PWA support is a production concern and is verified against a production build.
#
# Usage:
# docker compose -f docker-compose.yml -f docker-compose.dev.yml up
#
# (Production / end users just run `docker compose up`, which serves the built PWA.)
services:
app:
command: 'npm run start'
stdin_open: true
volumes:
- './app/public:/app/public'
- './app/src:/app/src'
- './app/package.json:/app/package.json'
- '.env:/app/.env'
environment:
# Force React to use the Caddy port for its dev-server websocket.
- WDS_SOCKET_PORT=0

View file

@ -92,20 +92,13 @@ services:
build:
context: .
dockerfile: docker/app/Dockerfile
volumes:
- './app/public:/app/public'
- './app/src:/app/src'
- './app/package.json:/app/package.json'
- '.env:/app/.env'
command: 'npm run start'
stdin_open: true
# The image builds the production bundle and serves it (see docker/app/Dockerfile), so the PWA service worker
# ships. Rebuild the image (`docker compose build app`) to pick up code changes. For local development with
# hot reload, layer on docker-compose.dev.yml (runs the CRA dev server instead).
healthcheck:
test: [ 'CMD-SHELL', 'curl http://127.0.0.1:3000' ]
interval: 1m
user: '${UID-1000}:${GID-1000}'
environment:
# Force React to use Caddy port to connect to it's websocket.
- WDS_SOCKET_PORT=0
web: # Caddy reverse proxy - handles HTTPS for all services.
depends_on:

View file

@ -13,11 +13,25 @@ docker volume create --name=openstreetmap-data
docker volume create --name=openstreetmap-rendered-tiles
docker-compose up -d db
docker-compose run --rm api db upgrade
docker-compose up
# Development (React dev server, hot reload):
docker compose -f docker-compose.yml -f docker-compose.dev.yml up
```
Browse to https://localhost:8443
### Production vs. development
The base `docker-compose.yml` serves the **production** React build, which is what ships the installable PWA
and its offline service worker — this is what end users run:
```bash
docker compose up # serves the built PWA (no hot reload; rebuild the app image to update)
```
For day-to-day frontend work, layer on `docker-compose.dev.yml` (as in the Quick Start above) to run the CRA
dev server with hot reload instead. The dev server does not emit a service worker, which is expected — verify
PWA/offline behavior against the production build.
## Services
| Service | Port(s) | Description |

View file

@ -9,7 +9,7 @@ USER root
RUN npm install -g serve
COPY app/public /app/public
COPY app/src /app/src
COPY app/package.json app/package-lock.json app/tsconfig.json /app/
COPY app/package.json app/package-lock.json app/tsconfig.json app/serve.json /app/
# "node" user will run the app after build.
RUN chown -R node:node /app
@ -17,5 +17,9 @@ RUN chown -R node:node /app
USER node
RUN npm install
# Run the production build
CMD ["npm", "run", "start"]
# Build the production bundle (emits the PWA service worker; the dev server does not).
RUN npm run build
# Serve the production build, matching the native wrolpi-app.service. serve.json keeps the SPA fallback while
# serving real files (service-worker.js, epub.html) directly.
CMD ["serve", "build", "-l", "3000", "-c", "/app/serve.json"]