Build Derived Data at Deploy Time When Production Should Not See the Source Files

In progress FastAPI

Some pages are computed from a pile of source files: a knowledge graph from Markdown notes, a search index, a sitemap. Computing them on each request works on a laptop where the files sit next to the code, but in production the files may not be there (and shouldn't be mounted), and parsing hundreds of them per request costs CPU for an answer that only changes when the files do. Generating the result once at build time and serving it as a static file removes both problems.

Split Build from Serve

notes/*.md ──build script──> graph-data.json ──deploy──> app serves the file
                                                          │
                                                 browser fetches and renders
  1. Build: a script scans the source files, parses frontmatter and links, and writes the result (here nodes and links arrays for a D3 force-directed graph) to a JSON file inside the app.
  2. Serve: the endpoint returns the pre-built file, with no file-system walk and no parsing.
  3. Render: the browser runs the layout and drawing, so the server does none of the physics.
@router.get("/graph/data")
async def get_graph_data():
    async with aiofiles.open(JSON_PATH) as f:
        return JSONResponse(json.loads(await f.read()))

Make Freshness Visible

  • Embed a generation timestamp in the file and show it on the page ("data as of …"), so a stale build is obvious.
  • Version static assets (style.css?v=1.0.1, or a content hash in the file name) so browsers fetch new CSS after a deploy.

References

Review cards

Flashcard Why build a knowledge graph's JSON at deploy time instead of on each request?

In production the source notes may not be there, and shouldn't be mounted. And parsing hundreds of files per request wastes CPU on an answer that only changes when the files do.

Flashcard How do you make a stale build of derived data obvious?

Embed a generation timestamp in the file and show it on the page ("data as of …"). Rebuilding it in every deploy or make target keeps it from drifting.

Quiz How should a build script keep private notes out of the generated file?
  • Apply the site's own rules (visibility, excluded folders) inside the build
  • Rely on the site hiding private pages when it serves them
  • Read every note and filter private ones in the browser
  • Skip the check: the file only holds titles and links

A script that reads every note will happily include private ones, and titles and links leak too. Filter in the build, then check the generated file before publishing.