Skip to content

A mostly static site

Goal: most pages are HTML files made at build time, and a few are rendered per request or in the browser. In Nuxt this is routeRules with prerender, swr and ssr: false.

  1. Pick a preset with a server, so the pages that are not prerendered have somewhere to run, and say what is prerendered. prerender: true in a rule marks a path, prerender.routes lists more, and crawl: true follows the links of the pages it renders. swr serves a stored copy for that many seconds and renders again in the background. ssr: false sends a shell that the browser fills in:
ts
import { defineConfig } from "hamur"

export default defineConfig({
  framework: "react",
  preset: "node",
  prerender: { routes: ["/about", "/pricing"], crawl: true, failOnError: true },
  routeRules: {
    "/": { prerender: true },
    "/blog/**": { swr: 600 },
    "/dashboard/**": { ssr: false },
  },
})
  1. Build and look at what was written. hamur build writes the prerendered pages as files and the server answers the rest; failOnError: true fails the build when a page does not prerender:
sh
hamur build --preset node
hamur preview
  1. If every page can be prerendered, drop the server: with the static preset hamur generate prerenders every page without params and the output is files for any static host. A path in prerender.routes is written as a full page even under ssr: false; the other pages of that kind share the shell 200.html.

A page that reads the request (a cookie, the session) cannot be prerendered: add ensureStatic: true to its rule and the build names the line that reads it. A page that changes without a deploy needs swr or isr, and revalidateTag or revalidatePath drops it when its data changes (Caching and ISR).

See also: Route rules and prerender, Rendering modes, Deploy.