Guide

Deploying

This page serves examples/todo in production with Nitro 3. The Node build below was run against that example. The page renders under a strict CSP, a click loads its two chunks, and pages from older builds recover as described. The Vercel and Cloudflare presets were built, and not deployed.

#What the build writes

vite build with idlip() and nitro() builds the client environment, the ssr environment and Nitro's server:

Path Holds
.output/public/assets/ The loader, one file per chunk (todo.handler.toggle.<hash>.js), and build.js.
.output/server/index.mjs The Node server. It serves .output/public and calls the fetch of the entry for every other path.
.output/server/_ssr/ssr.mjs The server entry, with the chunk URLs and the loader URL of the client build written in.
dist/.idlip/ The chunk map and the list of kept builds. Not served.

Every file in assets/ except build.js has a content hash in its name, so Nitro serves it with cache-control: public, max-age=31536000, immutable. The server reads no manifest: the URL of the loader is in the server entry, which the page writes as its script tag.

#Node

The todo entry answers each request with the page. It holds no policy code:

ts
// src/entry-server.ts
import { renderToResponse } from 'idlip/server';
import TodoApp from './todo.tsx';

const server = {
  async fetch(): Promise<Response> {
    return renderToResponse(TodoApp, { initial: [] }, { csp: true });
  },
};

export default server;

Build and run:

bash
pnpm exec vite build
PORT=3000 node .output/server/index.mjs

The config names the entry and adds Nitro:

ts
// vite.config.ts
import { idlip } from 'idlip/vite';
import { nitro } from 'nitro/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    idlip({ entry: '/src/entry-server.ts' }),
    // The loader asks for build.js to learn which build the server runs, so it must not be cached.
    nitro({ routeRules: { '/assets/build.js': { headers: { 'cache-control': 'no-store' } } } }),
  ],
});

A missing asset answers 404, which the loader treats as a failed chunk. A CDN may cache the page itself. The sections below cover the policy and a cached page that outlives its build.

#Vercel

Build for Vercel with the preset:

bash
NITRO_PRESET=vercel pnpm exec vite build

Or set it in the config: nitro({ preset: 'vercel' }). Nitro writes .vercel/output with the static files, the function __server.func and a config.json that carries the cache headers, build.js with no-store included. There is no vercel.json and no function file to write. The build log ends with npx nitro deploy --prebuilt.

#Cloudflare

bash
NITRO_PRESET=cloudflare-module pnpm exec vite build

Nitro writes .output/server/index.mjs as a module worker, .output/server/wrangler.json and .output/public/_headers, which holds the same cache headers. The server code uses web APIs only (Request, Response, crypto and streams). The worker needs the nodejs_compat flag, which Nitro writes in wrangler.json.

#Content security policy

csp: true of renderToResponse makes a nonce for each response, writes it on the listener, the preload links, the loader script and the state script, and sets this header:

default-src 'none'; script-src 'nonce-…' 'strict-dynamic'; style-src 'self' 'nonce-…'; base-uri 'none'; object-src 'none'; require-trusted-types-for 'script'; trusted-types idlip

default-src 'none' blocks images and requests, so a page that loads them adds its sources: csp: { extend: { 'img-src': ['https:'] } }. Sources are added to a directive of that name, or the directive is added. The policy allows the stylesheets of the build with style-src 'self' and the nonce. A style-src in extend replaces both, for a page that loads styles from another origin (see Styles). The script directives (script-src, script-src-elem, script-src-attr) and the Trusted Types directives (trusted-types, require-trusted-types-for) cannot be extended. A host that makes its own nonce passes it as nonce, and csp uses it.

#Cached pages after a deploy

A page names the chunk files of the build that rendered it. After a deploy, a page held in a CDN, a back-forward cache or an open tab can ask for files of an older build. The skew option of idlip() handles it, and it is on by default:

What this needs from the deploy:

To turn it off, pass idlip({ skew: false }). A failed chunk then never reloads the page.

#Preload the first click

renderToResponse writes <link rel="modulepreload"> tags for the chunks of the first click, so the browser fetches them with the page. For the todo page it writes three: the shared helpers module, the toggle handler and the todos.*.done binding. With them, the first click on a checkbox makes no request. examples/todo leaves them out with ?preload=off, by passing preload: false.

See idlip/server for the options.

#Stream the page

renderToResponse streams. The first chunk is the whole first render: the head with the listener, the preload links and the loader script, the body and the state script. Chunks for data that resolves later follow it.

For a host that sends its own Response, renderToStream(TodoApp, props, { nonce }) returns the document as a ReadableStream<string>, and streamParts(parts) streams the result of render.

#The dev server

vite dev serves the pages through Nitro, which calls the same fetch as production. When you save a *.tsx file, the page updates and keeps its model:

Without nitro() in the config, the plugin's own middleware calls the same fetch in vite dev. A production build holds none of this code.