Skip to content

perf: compress dynamic SSR and API responses#2407

Closed
gary149 wants to merge 1 commit into
mainfrom
perf/compress-dynamic-responses
Closed

perf: compress dynamic SSR and API responses#2407
gary149 wants to merge 1 commit into
mainfrom
perf/compress-dynamic-responses

Conversation

@gary149

@gary149 gary149 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

What

Adds a thin production server (server.mjs) that wraps the adapter-node handler with compression, so dynamic responses (SSR HTML, /api/v2/* JSON) are served with brotli/gzip. Static assets were already pre-compressed at build time via sirv, but everything rendered at runtime went out raw.

Measured against hf.co/chat in production: the HTML document is 318-355 KB with no content-encoding on every page view, and every conversation-switch JSON payload (20-70 KB) is also uncompressed.

Why streaming is excluded

The chat token stream (POST /conversation/[id]) responds with Content-Type: application/jsonl, and /api/v2/conversations/updates uses text/event-stream. A compression middleware buffers output, which would silently destroy time-to-first-token. Both content types are explicitly skipped, so token streaming behavior is unchanged.

Deployment

  • entrypoint.sh now starts node /app/server.mjs instead of build/index.js (PORT/HOST/SHUTDOWN_TIMEOUT env vars behave the same, body-size limiting stays inside the adapter handler)
  • Dockerfile copies server.mjs into the image
  • npm run dev / vite preview are unaffected

Verification

  • Behavioral test against the wrapper: HTML/JSON compressed (br), application/jsonl and text/event-stream pass through unencoded and unbuffered (first chunk arrives at ~100 ms of a ~500 ms stream, 5 distinct chunks received)
  • Built the app and booted through server.mjs: GET /chat/ went from 198,427 to 23,528 bytes transferred (-88%), Content-Encoding: br, Vary: Accept-Encoding set
  • npm run check, npm run lint, npm test (392 tests) all pass

adapter-node serves pre-compressed static assets via sirv but streams SSR
HTML and API JSON uncompressed. Wrap the handler in a small server that
negotiates brotli/gzip for dynamic responses.

Streaming endpoints (application/jsonl token stream, text/event-stream SSE)
are excluded from compression so tokens keep flowing chunk-by-chunk.

Measured on the built app: GET / went from 198,427 to 23,528 bytes (-88%).

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dce291c7c4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread server.mjs
});

const shutdown = () => {
server.close(() => process.exit(0));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid exiting before shutdown hooks finish

When SIGTERM/SIGINT arrives while there are no active HTTP requests, server.close can invoke this callback immediately, and process.exit(0) terminates the process before the app's existing async shutdown hooks registered in src/lib/server/exitHandler.ts can finish (for example closing MongoDB in src/lib/server/database.ts and the metrics server in src/lib/server/metrics.ts). The adapter-node server this replaces did not call process.exit in its close callback, so normal container shutdowns now risk skipping cleanup; let the event loop drain or coordinate with the existing exit handler instead.

Useful? React with 👍 / 👎.

@gary149

gary149 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by #2409, which lands the same compression approach with a mime filter for both streaming content types (application/jsonl and text/event-stream), a Vary merge that survives SvelteKit's own header writes, brotli level pinned to 4, unix-socket support, and graceful shutdown. Verified end to end against a production build.

@gary149 gary149 closed this Jul 2, 2026
@gary149 gary149 deleted the perf/compress-dynamic-responses branch July 2, 2026 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant