Skip to content

renderPage

renderPage renders one page from a Request and gives you the HTML, with no server listening. It is a named export of the server entry on bun, bun-compile, node and the presets that run node's entry (see Server entry exports). Use it to send a rendered page by email, to produce a PDF from HTML, to write your own static export or to test a page from a script.

renderPage(request, { context?, address? }) renders the page of a GET of the request's URL and resolves { status, headers, html, payload, redirect? }. It runs the pages() stage of the pipeline alone, with what runs around any pipeline (the hooks, the security headers, the ipRestriction and basicAuth rules); the middleware list, handler routes and the route rules that routeRules() applies do not run, while route middleware and loaders do. context is merged into event.context before the request hook, and address is the client address the ipRestriction rules and getRequestIP read; without it an ipRestriction route answers 403. html is the whole document, read to the end; payload is the payload object the page was rendered with, not its serialized text. A loader's redirect() (or any redirect answer) gives redirect, the Location, with html empty and no payload. A path no page matches gives the 404 page, and a page error its status and the error page. The page is always rendered for this call: a cache, swr or isr rule is not read, and the answer carries no shared-cache Cache-Control (s-maxage) or CDN cache header, since it may hold what context gave it.

ts
interface RenderedPage {
  status: number
  headers: Headers
  html: string
  payload: unknown
  redirect?: string
}

interface ServerEntry {
  renderPage(request: Request, options?: { context?: Record<string, unknown>; address?: string }): Promise<RenderedPage>
}

const entry = "./.output/server/index.mjs"

export async function render(path: string, tenant: string): Promise<string> {
  const { renderPage } = (await import(entry)) as ServerEntry
  const page = await renderPage(new Request(new URL(path, "http://localhost")), { context: { tenant } })
  return page.redirect ?? page.html
}

The context object reaches the loaders and route middleware as event.context, so a script can render a page for a tenant or a user without a session. address is for the rules that read the client address; pass the address a real request would have, or leave it out for pages under no ipRestriction. A loader that reads cookies or headers gets what the Request you built carries.

The API reference has the signature.