Skip to content

PrerenderServer ​

An HTTP server that renders pages on request.

ts
import { PrerenderServer } from "headless-prerender";

Constructor ​

ts
new PrerenderServer(options?: ServerOptions)

Creates the server without starting it. ServerOptions extends RendererOptions with server-specific fields:

OptionTypeDefaultDescription
portnumber3000TCP port to listen on
hoststring"0.0.0.0"Interface to bind
cacheTtlMsnumber0In-memory cache TTL; 0 disables caching
tokenstringnoneRequired token, via ?token= or x-prerender-token
ts
const server = new PrerenderServer({
  port: 3000,
  host: "127.0.0.1",
  cacheTtlMs: 300_000,
  token: process.env.PRERENDER_TOKEN,
  allowedOrigins: ["https://www.example.com"],
});

listen() ​

ts
listen(): Promise<number>

Starts listening and resolves with the bound port (useful when port is 0).

ts
const port = await server.listen();
console.log(`Listening on ${port}`);

close() ​

ts
close(): Promise<void>

Stops accepting connections and closes the browser. Resolves when both are done.

ts
process.on("SIGTERM", () => server.close().then(() => process.exit(0)));

HTTP API ​

GET /render ​

Query paramRequiredDescription
urlyesAbsolute http(s) URL to render
tokenonly if a token is configuredThe access token

Response: the rendered HTML document.

Response headerDescription
x-prerender-cachehit or miss
x-prerender-duration-msRender time in ms (on a miss)
StatusWhen
200 and the page's own statusSuccess (the upstream status is passed through)
400Missing url parameter
401Token configured and missing/incorrect
404Any path other than /render or /health
500Render error (invalid URL, disallowed origin, navigation failure)

GET /health ​

Returns 200 with body {"ok":true}. No token required.

Example ​

ts
import { PrerenderServer } from "headless-prerender";

const server = new PrerenderServer({
  cacheTtlMs: 60_000,
  token: process.env.PRERENDER_TOKEN,
  allowedOrigins: ["https://www.example.com"],
});

const port = await server.listen();
console.log(`headless-prerender on :${port}`);

for (const sig of ["SIGINT", "SIGTERM"] as const) {
  process.on(sig, () => server.close().then(() => process.exit(0)));
}

Released under the MIT License.