Static Builds (SSG)
Prerender pages to HTML, either as a true static site or as a live SPA with prerendered entry pages.
A Puzzle app can prerender its routes to plain HTML at build time, so each page is readable and crawlable before a line of JavaScript runs. There's no SSR server and no hydration protocol. The same views, layouts, and routes build two ways:
static: a true static site. Every page is its own HTML file with a small module that mounts only that page's components, so event handlers and local state still work. There's no router and no SPA: links are ordinary page loads. Pick this for docs, marketing sites, and blogs.hybrid: prerendered pages plus the full SPA bundle. The first paint is the prerendered HTML; the app then takes over and every later navigation is client-side, with transitions and morphs. Pick this for an app that wants search-friendly entry pages.
This site is a static build.
Turning it on
Set output in puzzle.config.js to 'static' or 'hybrid':
// puzzle.config.js
export default {
styles: {
use: ['tailwindcss'],
},
output: 'static', // or 'hybrid'
};The flags puzzle build --static and puzzle build --hybrid do the same without editing the config. The two are mutually exclusive, and a flag that disagrees with the config value is an error. A plain puzzle build with no output builds the SPA.
npm run build # with output set in the config, this prerenders # or, without editing the config: puzzle build --static puzzle build --hybrid
In development, puzzle dev on a static project serves the real static output: every rebuild runs the prerender, and pages are served with clean URLs, full page loads, and a real 404. Hybrid projects develop as the SPA, which is what they are after takeover. To check a finished build without a watcher, run puzzle build and then puzzle preview.
What gets emitted
Both modes write one HTML file per static route, directory-style:
dist/ ├── index.html # / → home ├── about/index.html # /about ├── guide/index.html # /guide → nested shell + index child ├── guide/templates/index.html # /guide/templates ├── 404.html # * → catch-all, served for unknown URLs ├── styles.css # compiled Tailwind ├── _puzzle/ # static: one small module per page │ └── chunks/ # + code shared between pages └── app.js # hybrid: the SPA bundle that takes over
Each page is your public/index.html shell with the route's markup injected into the #app element. The build also writes the page's head from the route's meta: the <title>, plus description, canonical, Open Graph, and Twitter tags from description, canonical, and socialImage. Each field resolves leaf to root through the matched route chain. Pages link absolute paths, so they work at any depth.
The top-level catch-all (path: '*') renders to 404.html, the filename GitHub Pages, Netlify, Render, and Cloudflare Pages serve for unknown paths, so unknown URLs get your NotFound view. Omit the catch-all and the build warns. A page that would overwrite a file copied from app/public/ is a build error.
Two requirements in app.js
1. Export the app
The prerenderer imports your app's default export to read its config, so app.js must end with:
const app = new PuzzleApp({ target: '#app', routes, models });
app.mount();
export default app; // required for prerendered builds2. Use an id selector as the mount target
target must be an '#id' selector pointing at an empty element in the shell. The prerenderer injects each route's markup there:
<body> <div id="app"></div> <script type="module" src="/app.js"></script> </body>
The rules that matter
Prerendering runs your data() and beforeMount under Node, where there's no browser. Missing these is the usual source of static-build bugs.
data() and beforeMount run at build time
They execute under Node during the build, so guard every browser global. Check before touching document, window, localStorage, or matchMedia:
data(params, props) {
const theme = typeof localStorage !== 'undefined'
? localStorage.getItem('theme')
: 'light';
return { theme };
}DOM behavior (listeners, intervals, reaching into the DOM, booting a library) belongs in mounted(), which only runs in the browser. Clean it up in destroyed(). No mounted(), animation, or DOM work happens at build time.
:param routes without staticPaths are skipped
A :param route is prerendered once per staticPaths entry:
{
path: '/tags/:tag',
view: TagView,
staticPaths: [{ tag: 'news' }, { tag: 'guides' }],
},Without one it is skipped with a warning, and no file is written:
[puzzle] skipped dynamic route "/tags/:tag" — a :param route is prerendered only when it lists its values in a `staticPaths` route field
In hybrid output a skipped route still works once the SPA takes over; in static output it has no page at all. For a content site you want indexed, treat any "skipped" in the build summary as a regression, not a warning to ignore.
Inline <script> in route markup is dead
The browser mount over the prerendered markup discards any inline <script> inside a route's template. Only the shell (app/public/index.html) may carry inline scripts, such as analytics or a theme pre-paint snippet. All other behavior goes in mounted().
prerender: false opts a route out
Flag a route prerender: false to skip rendering its markup. The build writes the shell at that path with an empty #app, and the page renders entirely in the browser: an SPA island in hybrid output, a client-rendered page in static output. Use it for routes that are pure interaction, but know that such a page is invisible to a static search indexer:
{
path: '/playground',
name: 'playground',
view: PlaygroundView,
layout: DefaultLayout,
prerender: false, // rendered in the browser only
meta: { title: 'Playground' },
},What static output changes
A static site has no router, and each page's module loads only what that page needs. A few things follow:
- Navigate with plain links.
this.ctx.router's navigation methods throw, so apush()is a bug. Hrefs built withlink()still render as plain paths, androuterBasestill applies. Hash routing doesn't apply to static output, and the build warns if you configure it. - No router features. Guards never run (the build warns once if a route declares one), and the router's focus and scroll management don't apply: every page is an ordinary document load.
beforeMountnever runs in the browser. The records it seeds at build time are serialized into each page and restored before the page mounts, so build-time credentials stay on the build machine. Browser-only setup must not live inbeforeMount.- App functions, models, and the adapter load from their own files. Export your template functions from
app/formatters.js(functions registered only inapp.jsexist at build time but not in the browser, and the build warns), keep models inapp/models/index.js, and export a configured adapter fromapp/adapter.js. An adapter configured inline inapp.jsstill works, but every page then imports the app entry, which costs page weight; the build prints a note. build.splittingonly hashes file names: static pages are already split into per-page bundles, with shared code in_puzzle/chunks/.
Build-time data
Because beforeMount runs at build time, records you seed there flow into the first data() of every page and land in the HTML. Under the prerenderer the hook receives a { store, config } facade, so destructuring { store } works in both places (at runtime the live app exposes the same .store):
// app.js
const app = new PuzzleApp({
target: '#app',
routes,
models,
beforeMount({ store }) {
store.createRecord('principle', {
id: 'p1',
title: 'One file per component',
body: 'Template, styles, and logic live together in a .pzl file.',
});
},
});A view's data() reads those records back, and they're baked into the page:
// About.pzl
data(params, props) {
return { principles: this.ctx.store.findMany('principle') };
}With server sync, the store reads in data() fetch at build time too, so the build machine has to reach your API. Give the app an absolute apiURL it can reach; an app-relative URL fails the build with a message naming the route and both fixes. A model with no endpoint and no read function never fetches, so seed it in beforeMount instead. beforeRequest runs at build time, so a private API needs its credentials available to the build. A static page carries what the build already loaded, so it repeats none of those requests in the browser.
Translations
With translations configured, every page prerenders in the default locale, sets <html lang> to it, and carries its string table inline, so a default-locale visitor makes no extra request. Another locale loads one small file in the browser.
A worked example: nested routes
Nested routes prerender to nested directories, which also shows the title walk:
{
path: '/guide',
name: 'guide',
view: GuideShell,
layout: DefaultLayout,
meta: { title: 'Guide · Puzzle Field Guide' },
children: [
{ path: '', name: 'guide-index', view: GuideIndex }, // → dist/guide/index.html
{
path: 'templates',
name: 'guide-templates',
view: GuideTemplates,
meta: { title: 'Template syntax · Puzzle Field Guide' }, // → dist/guide/templates/index.html
},
],
},The index child defines no meta.title, so its page inherits the shell's "Guide · …". The sibling defines its own, which wins at /guide/templates/.
Serving dist/
The output is plain static files. puzzle preview serves it the way a production host will for your output mode (clean URLs and a real 404 for static, the prerendered page or the shell for hybrid), and any static file server works too:
puzzle build && puzzle preview # or any static server npx serve dist
Pages are directory-style (dist/guide/index.html), so live URLs are directory URLs (/guide/, /guide/templates/). A single trailing slash is insignificant to route matching, so /guide/ matches the /guide route.
Post-build tooling
Beyond page content and head tags:
- Search: a static index tool such as Pagefind can crawl
dist/directly, since each page's content is baked into#app. - Sitemaps: set
site: 'https://example.com'inpuzzle.config.jsand the build writesdist/sitemap.xml.
Next
- Routing: nested routes, catch-alls, and head metadata.
- CLI: the
buildandpreviewcommands and their flags. - Data & Models: seeding and fetching data.