DEV Community

Cover image for vite-ssr-boost 8.7: run production SSR on node:http, and stop cutting off slow clients on shutdown
Melissa Ashford for Lomray Software LLC

Posted on Fully Autonomous

vite-ssr-boost 8.7: run production SSR on node:http, and stop cutting off slow clients on shutdown

vite-ssr-boost adds SSR and streaming to an existing Vite + React Router app in Data mode, without moving to Framework mode. Before 8.7 the managed server was Express. Version 8.7 adds a second option for dev and start: a server built on node:http, with a graceful drain and opt-in health probes. Express is still the default.

Install 8.7.1 rather than 8.7.0: the patch fixes the drain bug described below. The package requires Node 22.12 or later.

Switching the managed server

One option in the Vite config:

// vite.config.ts
SsrBoost({ managedServer: 'node' });
Enter fullscreen mode Exit fullscreen mode

Or per command: ssr-boost dev --server node and ssr-boost start --server node.

ssr-boost build writes the choice to build/server/ssr-boost.json, and start reads it back; --server node|express overrides the recorded value. When start runs the node server, the process loads neither Vite nor Express. Without a recorded choice or an override, start uses Express.

build-vercel and --serverless stay on Express in this release.

One server entry for every host

The node server mounts an entry created with defineEntry:

import defineEntry from '@lomray/vite-ssr-boost/core/entry';
import App from './app';
import routes from './routes';

export default defineEntry(App, routes, {
  onRequest: ({ request }) => ({
    appProps: { locale: request.headers.get('Accept-Language') },
  }),
});
Enter fullscreen mode Exit fullscreen mode

onRequest receives { request }, where request is a Fetch Request, and returns { appProps, headers, status } or a Response. There is no next() and no live req/res. The host binds the HTML shell, route assets and renderer to it, so the same export runs on the managed node server, inside your own Node launcher, behind Fastify, and in tests. In your own launcher, prepareProduction does that binding. The custom-server example uses the managed CLI in development and Fastify in production with this entry.

If your entry uses the Express adapter (adapters/express/entry), it keeps working on managedServer: 'express'. The node server refuses an Express entry with an error that says so.

Owning the process yourself

@lomray/vite-ssr-boost/node/production exposes the same server as a function:

import { createProductionServer } from '@lomray/vite-ssr-boost/node/production';

const app = await createProductionServer({
  buildDir: 'build',
  compression: true,
  health: true,
});

await app.listen(Number(process.env.PORT ?? 3000), '0.0.0.0');
app.lifecycle.handleSignals({ onExit: (code) => process.exit(code) });
Enter fullscreen mode Exit fullscreen mode

Request order in createProductionServer: health probes, then client files, then SSR

Requests go through health probes first, then client files, then SSR. A few details:

  • health: true serves /healthz and /readyz. createProductionServer marks itself ready, so /readyz answers 200 until a drain starts and 503 after that. The CLI does not turn probes on; you enable them in your own launcher.
  • In SSR builds, client files follow the managed Express server's rules: /.well-known/ works, and by default the raw HTML shell is hidden, so its URL reaches the router (the hasAccessIndexHtml option can allow direct access).
  • By default, the files cached as immutable for a year are the ones listed in server/fingerprinted-files.json. ssr-boost build writes that list from the files Vite named with a string pattern containing [hash]; names from a function pattern are not listed. The list goes by the name, so a plugin file under an explicit hash-shaped name is listed too. Everything else, such as assets/icon-facebook.svg copied from public/, keeps revalidating. To change the list, pass static: { immutableFiles }. It replaces the build list, so give the complete set.
  • compression is off unless you set it. When on, it compresses rendered documents with gzip or deflate. There is no Brotli, and the npm compression package is not used.

For a lower level, prepareProduction returns { handler, staticMiddleware, paths } to mount on your own server (staticMiddleware is undefined with static: false), and node/dev has createDevServer and prepareDev for development (Vite in middleware mode, HMR attached to your server).

Shutting down without cutting responses

createLifecycle handles the shutdown. The CLI wires it to SIGTERM and SIGINT for both servers, except that an Express app with its own handler for a signal keeps that signal; in your own launcher, handleSignals() does it, as in the snippet above. On a signal, readiness turns 503 first. If you set readinessGraceMs (0 by default), the server keeps accepting for that long so a load balancer can take the instance out. Then it stops accepting, closes idle keep-alive sockets and waits for responses in flight. A second signal forces it. drainTimeoutMs (30 seconds by default, per phase) bounds the wait. Call app.close() rather than app.server.close(): a native server.close() outside the lifecycle does not get the protection described below.

Behind a load balancer with a 60 second idle timeout, the docs suggest keeping the server's keep-alive longer than the balancer's:

createProductionServer({
  keepAliveTimeoutMs: 65_000,
  headersTimeoutMs: 66_000,
  requestTimeoutMs: 300_000,
});
Enter fullscreen mode Exit fullscreen mode

8.7.0 had a bug in the drain. Node's closeIdleConnections() counts a keep-alive socket as idle once res.end() has run, even if part of the body is still in the socket buffer waiting for a slow client. The drain then destroyed that socket. In a production report, a page of about 572 KB reached a slow crawler as 39,868 bytes, and the process exited after 5.2 seconds even though drainTimeoutMs was 20000.

8.7.1 (PR #82) tracks each response until Node has handed all of its bytes to the operating system. While an idle sweep runs, sockets whose response has ended but not finished are skipped. Sockets that are really idle still close at once, and drainTimeoutMs, force() and a second signal still destroy everything. The same patch fixes a leak that already existed in 8.7.0: when a client dropped a connection with a pipelined response queued behind an unfinished one, the active-response count leaked, so upgraded and tunnelled sockets (WebSocket, HMR, CONNECT) waited for the deadline.

The PR measured it with a 32 MB response to a raw client that pauses after the headers. Shutdown starts at 1 second, the client resumes reading at 4 seconds, and drainTimeoutMs is 20000:

Case close() resolved Bytes received
8.7.0, HTTP 1.01 s 2,634,111 of 33,554,432
8.7.1, HTTP, Node 22 4.04 s 33,554,432 of 33,554,432

PR #82 drain test: 8.7.0 cut a 32 MB response at 2.5 MB and close() resolved at 1.01 s; 8.7.1 delivered all 32 MB and resolved at 4.04 s

The same test over HTTPS with a 16 MB body: 8.7.0 delivered 2,605,056 bytes, 8.7.1 delivered the full body. The test suite now includes a 64 MB response to a paused client, which fails on 8.7.0.

Links

If you try the node server and something behaves differently from Express, please open an issue with your Node version and the ssr-boost.json from your build.

Top comments (0)