Skip to content

perf(flame): keep the page cache when docu.json lists plugins - #491

Merged
gitfromwildan merged 2 commits into
mainfrom
perf/flame-plugin-page-cache
Nov 6, 2026
Merged

gitfromwildan merged 2 commits into
mainfrom
perf/flame-plugin-page-cache

Conversation

@gitfromwildan

Copy link
Copy Markdown
Member

Summary

flame build stopped using its page cache as soon as docu.json listed a plugin, so every docs page was re-rendered on every build. This PR adds the plugins to the cache key and removes that bypass. Unchanged pages are now skipped with or without plugins. A plugin upgrade, a rebuilt plugin, or an edited local plugin still re-renders every page.

Why the cache was off with plugins

Both page loops (build.ts for Bun, build.impl.ts for Node/Deno) only skipped a page when no plugin was configured:

if (rebuildDecision === "no" && !builder) { /* skip */ }
if (rebuildDecision === "hash_check" && !builder) { /* skip */ }

That bypass (added in #378) was the only guard against stale output. The page key covers only the MDX source, and the rest of the key material covers docu.json (configStamp()), the framework sources, and the @docubook/* packages. Plugin code was in none of them. So docu.json caught a plugin's options changing, but nothing caught an upgraded plugin package or an edited local plugin file.

What changed

cache-key.ts: new pluginsStamp(), a fingerprint of every plugin in docu.json:

Plugin specifier Fingerprint
npm package (@docubook/plugin-search-algolia) version from its package.json + hash of its built dist/ (reuses resolvePackages / packageFingerprint from dev-watch.ts)
local path (./plugins/reading-time) hash of the file the runtime loads: the path as written, then .ts, .tsx, .mts, .js, .mjs, .cjs, then index.ts / index.js / index.mjs in a directory, since the docs write local plugins without an extension
does not resolve "missing" (never throws; loadPlugins reports the real error)

It is folded into renderToolchainStamp(), which readCache() checks (meta.render) and hashMdxSources() hashes. When a plugin changes, the whole page cache is discarded and the client bundle is rebuilt. BUILD_CACHE_VERSION goes from 9 to 10, following the existing convention for new key material.

build.ts, build.impl.ts: the && !builder bypass is removed from both page loops.

Docs: the plugin Lifecycle section in plugins.mdx now explains that the cache can skip a page.

Changeset: @docubook/flame minor.

Behavior change for plugin authors

  • onLoad still runs on every file, in the pre-pass that compiles the client bundle.
  • Per-page hooks (transformFrontmatter, remark/rehype, injectHead, injectBody, transformHtml) now run only for pages that are rendered again.
  • onEnd still receives every page (mdxFiles, not just the rendered ones), and still runs on every build.

A plugin that collects data from per-page hooks to use in onEnd will only see the re-rendered pages. A plugin whose output depends on anything outside its code and docu.json (environment variables, remote APIs, the clock) will keep the HTML from the last render until flame build --force. The first-party Algolia plugin uses only injectHead (static per build) and onEnd, so neither case applies to it.

Known limit: for a local plugin, only the plugin's own file is hashed, not the files it imports. After editing one of those, run flame build --force. A shortcut: comment in the code records this.

Benchmark

Flame's own docs: 27 pages, @docubook/plugin-search-algolia 0.1.2, Bun, macOS, one machine. "Before" is main. Page phase is the Built N pages (… ms) timing; total is the whole pnpm run build.

Scenario Before: pages rendered Before: page phase Before: total After: pages rendered After: page phase After: total
Rebuild, nothing changed (avg of 3) 27 / 27 ~1,100 ms ~2,960 ms 0 / 27 ~125 ms ~2,010 ms
Only mtime changed (touch) 27 / 27 1,151 ms 3,079 ms 0 / 27 128 ms 1,969 ms
One page edited 27 / 27 1,088 ms 3,011 ms 1 / 27 319 ms 2,222 ms
Edit reverted 27 / 27 1,091 ms 2,926 ms 1 / 27 332 ms 2,225 ms
--force 27 / 27 1,179 ms 4,776 ms 27 / 27 1,100 ms 4,710 ms

The page phase is ~9× faster when nothing changed. The rest of the total (startup, bundle, Tailwind, search index) is untouched. The saving grows with the page count, because before, every page was rendered on every build.

Plugin assets: verified intact

Every scenario started from an empty .docu/dist. In each one, a checker confirmed:

  • every local src/href in every HTML file points to a file that exists (flame CSS/JS/chunks);
  • every docs page has the Algolia <head>, and its client module (assets/docsearch/client.js) exists;
  • every relative import inside that client module exists.
# Scenario Rendered Pages with Algolia head Missing assets
1 Clean full build 27 28/28 0
2 Cached rebuild 0 28/28 0
3 Cached rebuild after deleting assets/docsearch/ 0 28/28 0
4 One page edited 1 28/28 0
5 Edit reverted 1 28/28 0
6 Installed plugin version bumped (0.1.2 → 0.1.3) 27 28/28 0
7 Version restored 27 28/28 0
8 --force 27 28/28 0

HTML from scenarios 2, 3, 5, 7, and 8 is byte-identical to the clean full build across all 30 HTML files, ignoring the per-render CSP nonce. Why the assets stay intact:

  • The plugin copies its client files in onEnd, which runs every build. Scenario 3 shows a cached build restores deleted plugin assets.
  • The CSP and its nonce live in a <meta> inside each page, so a cached page's nonce still matches its own scripts.
  • Changing the installed plugin's dist/ also re-rendered every page, and restoring it did the same.

Test plan

  • pnpm exec vitest run in packages/flame: 869/869 passed
  • tsc --noEmit, oxlint clean
  • New tests in cache-key.test.ts:
    • pluginsStamp changes on a package version bump, a dist/ change, a local file edit, an extensionless .ts local plugin edit, and a directory index.js edit, and does not throw for unresolvable plugins.
    • The h.update("plugins\0").update(pluginsStamp()) call is present, and the !builder bypass is gone from both build files.
  • Benchmark and asset scenarios above, run by hand on flame's own docs
  • Not tested: Node/Deno builds (build.impl.ts gets the same two-line change), plugins other than Algolia, and whether pluginsStamp resolves the same plugin copy as loadPlugins when flame is installed from npm in a user project (it does in this monorepo)

flame build skipped the page cache as soon as one plugin was configured
(`&& !builder` in both page loops), so every docs page was re-rendered on
every build. The bypass was the only guard against stale output: the page
key only covers the MDX source, and nothing in the key covered plugin code.

Add pluginsStamp() to the cache key material, folded into
renderToolchainStamp(): a plugin package hashes as its version plus its
built dist/, a local plugin as the file the runtime loads (extensionless
specifiers and directory index files included), anything unresolvable as
"missing". A plugin upgrade or rebuild now discards the cache and the
bundle, so the bypass can go. BUILD_CACHE_VERSION goes to 10.

Per-page hooks (transformFrontmatter to transformHtml) now run only for
pages that are rendered again; onEnd still receives every page. The plugin
docs and a changeset say so.
@vercel

vercel Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docu Ignored Ignored Preview Nov 6, 2026 11:28am UTC

@gitfromwildan gitfromwildan self-assigned this Nov 6, 2026

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ One gap in the plugin fingerprint is worth closing before merge. Everything else looks sound.

Reviewed changes

Senior-mode review of the flame build cache change: plugin code now goes into the render stamp, so the page cache stays on in projects that configure plugins.

  • pluginsStamp() in cache-key.ts: fingerprints each docu.json plugin. npm plugins use their version and built dist/, local plugins hash the file the loader would pick, and unresolvable plugins become "missing". The stamp is folded into the memoized renderToolchainStamp() and BUILD_CACHE_VERSION goes up to 10.
  • Page loops in build.ts / build.impl.ts: the && !builder bypass is gone, so unchanged pages are skipped whether or not plugins are configured.
  • Docs + changeset: the Lifecycle diagram and prose say per-page hooks now run only for re-rendered pages. onEnd still receives every page.

Assessment: READY WITH MINOR FIXES. One P2 finding (confidence 0.85, dimension: correctness):

  1. [P2] cache-key.ts:193: plugins that only export an import condition always fingerprint as "missing", so upgrading one leaves stale HTML. Details inline.

The remaining paths look correct to me. A change to onLoad output still reaches the bundle hash through the prepass, which sets assetsChanged and re-renders every page. The removed bypass guarded nothing besides the plugin-code gap that this stamp now covers. The PR description already lists the remaining limits: the clock, environment variables and files imported by a local plugin.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using claude-opus-5-5 | 𝕏

Comment thread packages/flame/.docu/node/cache-key.ts Resolved
pluginsStamp() resolved npm plugins with createRequire().resolve(), which
only matches the require/node/default export conditions. A plugin whose
exports map has only an `import` condition threw
ERR_PACKAGE_PATH_NOT_EXPORTED, was skipped, and kept the stamp at
"missing" across upgrades, so cached pages kept the old plugin's HTML.

Find the package by walking up node_modules from the directory
plugin-loader imports from (cache-key sits next to it in .docu/node and
.docu/lib), like import() does. This does not depend on export
conditions. The manifest stands in for the entry, since only dist/ is
hashed. Adds a test with an import-only exports map.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found. The ESM-only plugin gap from the previous review is closed.

Reviewed changes

Senior-mode re-review of 7d1a5a7. This commit changes how pluginsStamp() finds npm plugin packages.

  • Replaced require.resolve lookup with findInstalledPackage(): the new function walks up node_modules/<name>/package.json from packagesFrom and realpaths the match. It no longer depends on exports conditions, so import-only plugins now fingerprint by version and dist/. packageFingerprint only checks that entry exists, so passing the manifest as the entry is safe.
  • Added a packagesFrom parameter, defaulting to cache-key's own directory: plugin-loader.ts sits in the same directory in .docu/node and in the compiled .docu/lib (compile-lib outDir). Node realpaths module URLs, so under pnpm the walk covers the same directories as the loader's import(specifier).
  • Added a regression test: it uses an import-only exports package found from a nested directory and checks that the stamp changes on a version bump and on a dist/ edit. The existing cases now pass packagesFrom explicitly.

Assessment: READY. No P0–P2 findings.

Pullfrog  | View workflow run | Using claude-opus-5-5 | 𝕏

@gitfromwildan
gitfromwildan merged commit 5665075 into main Nov 6, 2026
15 checks passed
@gitfromwildan
gitfromwildan deleted the perf/flame-plugin-page-cache branch November 6, 2026 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant