Agent Wranglers

WEB-18Optional

Render at build time and serve from memory

Do all templating at build time. Render each page once, compress it once, and keep the bytes, ETag and security policy in memory.

How to do it

On start, or on first request, render each page and work out its CSP hashes and ETag. Compress it only in the encoding a client asks for, and cache those bytes. Key the cache by path or a short hash, never by the page text itself. Keep page sources on disk, not in memory, so a large site fits a small server. Insert content into the template literally, as the literal replacement rule describes.

Examples

JavaScript example

import { brotliCompressSync, gzipSync } from 'node:zlib';
import { createHash } from 'node:crypto';
const ENCODERS = { br: brotliCompressSync, gzip: gzipSync };
const cache = new Map();
function encoded(page, encoding) {
  const entry = cache.get(page.path) ?? {};
  entry[encoding] ??= ENCODERS[encoding](Buffer.from(render(page)));
  cache.set(page.path, entry);
  return entry[encoding];
}
// A function replacer inserts the content exactly as written
const fill = (template, html) => template.replace('<!--page-->', () => html);

Python example

import gzip
ENCODERS = {"gzip": lambda body: gzip.compress(body, 9)}  # add "br" with the brotli package
cache = {}
def encoded(page, encoding):
    entry = cache.setdefault(page.path, {})
    if encoding not in entry:
        entry[encoding] = ENCODERS[encoding](render(page).encode())
    return entry[encoding]
# str.replace inserts the content exactly as written
fill = lambda template, html: template.replace("<!--page-->", html)

How to check

With a Server-Timing header on page responses, a warm page shows a dur under 1 ms. After warming every page, the server's memory stays within the budget the load test sets.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/site-architecture/render-at-build-time-serve-from-memory">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.