blokjs

blokjs-prerender

Writes the rendered HTML of a BlokJS page into the page itself, one file per route. Clients that do not run JavaScript (crawlers, link previews, payment provider site reviews, curl) then see the content, and blok.mount() adopts that HTML in the browser instead of rendering it again.

The page keeps working without this step. Prerendering only adds HTML.

Usage

bunx blokjs-prerender index.html
bunx blokjs-prerender index.html --routes / /about /users/42 --out dist
bunx blokjs-prerender index.html --inline --out dist   # one self-contained file per route
Option Default Description
--routes <paths...> every route without :param, * or guard; / without a router Routes to render
--out <dir> directory of the page Where the files go. Only HTML files are written; copy the site’s other files yourself
--inline off Embed local scripts, local stylesheets and the blokjs runtime, minified. Needs --out

/ is written to the page’s own file name, /about to about.html (static hosts such as GitHub Pages, Netlify, Cloudflare Pages and Vercel serve it at /about), /docs/ to docs/index.html. Without --out the page is updated in place; running the CLI again replaces the earlier output.

What it does

  1. Collects the page’s scripts in the order a browser runs them: inline scripts and local src files, then defer, async and module scripts. Module scripts are bundled with esbuild.
  2. Runs them in a sandbox where blok comes from @maleta/blokjs/server. blok.mount('#id', opts) and blok.mount(document.getElementById('id'), opts) calls are recorded, also inside DOMContentLoaded listeners.
  3. For each route, renders every recorded mount with renderToString and writes the HTML into the element with that id, adding the data-blok-ssr attribute. The rest of the file stays byte for byte.

One file per page: --inline

Limits

License

MIT