Skip to content

perf(website): compile each docs page once per request, not twice - #376

Merged
blove merged 1 commit into
mainfrom
blove/docs-load-per-request-cache
Aug 14, 2026
Merged

blove merged 1 commit into
mainfrom
blove/docs-load-per-request-cache

Conversation

@blove

@blove blove commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

/docs/[[...slug]] calls loadDocsPage twice for every request — once in
generateMetadata (page.tsx:31) and once in the body (page.tsx:61) — and each
call is an MDX compile plus a shiki highlight pass, the most expensive work the
route does. Nothing memoised it, so the second call redid all of it to produce
a value the first already had. Its sibling lib/docs/examples/registry.ts
memoises for exactly this reason; this module was simply missed.

Wrapping in React's cache() roughly halves the route's server time, which is
what removing one of two identical compiles should do. 10 docs URLs, 6 passes,
warmed, both builds measured back to back on the same machine under the same
load (1-min avg 6.6 vs 7.2):

          p50     p90     mean

before 43ms 108ms 51ms
after 27ms 58ms 30ms

Keyed on the joined slug, not on the slug array: cache() memoises on
argument IDENTITY, and the two call sites each derive their own array from
params, so a cache keyed on slug would miss every time and silently do
nothing — a fix that looks applied and isn't.

Deliberately no unit test. cache() only memoises inside a React request
scope; called from vitest it hands back a fresh cache each time, so a test
asserting "compiled once" would pass whether or not the memoisation works. A
test that cannot fail is worse than none. The behaviour is covered by the
existing load tests plus the timings above.

Found while investigating the destination stream closed early. log noise: the
double compile widens the window in which a render is still in flight when a
client disconnects. It does NOT fix that (34 lines after, vs 37-46 before —
noise). That is a known Next 16.3.0 regression, vercel/next.js#96704.

369 website tests, 112 e2e across chromium and webkit, typecheck, lint.

Co-Authored-By: Claude Opus 5 noreply@anthropic.com

🤖 Generated with Claude Code

`/docs/[[...slug]]` calls `loadDocsPage` twice for every request — once in
`generateMetadata` (page.tsx:31) and once in the body (page.tsx:61) — and each
call is an MDX compile plus a shiki highlight pass, the most expensive work the
route does. Nothing memoised it, so the second call redid all of it to produce
a value the first already had. Its sibling `lib/docs/examples/registry.ts`
memoises for exactly this reason; this module was simply missed.

Wrapping in React's `cache()` roughly halves the route's server time, which is
what removing one of two identical compiles should do. 10 docs URLs, 6 passes,
warmed, both builds measured back to back on the same machine under the same
load (1-min avg 6.6 vs 7.2):

              p50     p90     mean
  before      43ms   108ms    51ms
  after       27ms    58ms    30ms

Keyed on the joined slug, not on the `slug` array: `cache()` memoises on
argument IDENTITY, and the two call sites each derive their own array from
`params`, so a cache keyed on `slug` would miss every time and silently do
nothing — a fix that looks applied and isn't.

Deliberately no unit test. `cache()` only memoises inside a React request
scope; called from vitest it hands back a fresh cache each time, so a test
asserting "compiled once" would pass whether or not the memoisation works. A
test that cannot fail is worse than none. The behaviour is covered by the
existing load tests plus the timings above.

Found while investigating the `destination stream closed early.` log noise: the
double compile widens the window in which a render is still in flight when a
client disconnects. It does NOT fix that (34 lines after, vs 37-46 before —
noise). That is a known Next 16.3.0 regression, vercel/next.js#96704.

369 website tests, 112 e2e across chromium and webkit, typecheck, lint.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
pretable Ready Ready Preview Aug 14, 2026 12:46am

@github-actions

Copy link
Copy Markdown
Contributor

Vercel preview ready

Preview: https://pretable-pzghkzo4b-cacheplane.vercel.app
Commit: 5c35fdf25b5f125559b71fda2a0370b192d747b5

Updated automatically by the deploy-preview job.

@blove
blove merged commit 3faf71c into main Aug 14, 2026
17 checks passed
@blove
blove deleted the blove/docs-load-per-request-cache branch August 14, 2026 17:15
blove added a commit that referenced this pull request Aug 14, 2026
* docs(spec): design for validating the upstream Next abort fix

Rather than patch Next locally to quiet pretable #377, help the upstream fix
land: two community PRs sit unreviewed with no maintainer comment, and both
rest on one synthetic test.

The design's load-bearing part is the check the error count cannot provide. A
fix that correctly classifies the abort and a fix that stopped cancelling
renders BOTH show zero errors — the second silently burns server time finishing
responses nobody reads. So each arm runs a cancellation twin that fails loudly
in that case, and the twin is itself proven capable of firing first.

Nothing is posted upstream without explicit approval of the drafted text.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs(plan): implementation plan for upstream abort-fix validation

Three arms, each verified green before its numbers count, plus a cancellation
twin that fails loudly if a fix wins by no longer cancelling renders rather
than by classifying the abort correctly.

Task 1 exists to establish the twin's discriminating power BEFORE any PR is
measured: it proves the marker can fire at all, then proves it stays absent on
an aborted baseline render. Whether React declines to render a component after
an abort is left as a tested gate with a documented alternative, not an
assumption — a wrong guess is caught in the first task instead of silently
invalidating every later number.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs(plan): rewrite Task 1 after its gate failed, and fix two plan defects

The twin aborted a plain HTML document load. On unmodified 16.3.0 that gave
closed-early 0 and RENDER-COMPLETED 1 — it neither cancelled the render nor
reproduced the phenomenon at all, so it had no discriminating power to lend the
PR arms. They would have scored identically whether or not the PRs worked.

That matches both the source analysis (the RSC path pipes into a PassThrough
with a direct res.close -> pt.destroy() link; the document path goes through a
pipe chain where destroy does not propagate upstream) and the proxy data from
the original investigation, where the aborting requests were ?_rsc= router
fetches. Task 1 now drives a real client-router navigation with Playwright
rather than forging RSC headers, which 307 without the router state tree.

The gate the plan was missing: a positive control on the ERROR, not just on the
marker. A baseline that cannot produce the error makes every later zero
meaningless. That is now Gate B, and it is what would have caught this before
three arms of numbers.

Two defects of mine, both found by review rather than by running:
- RESULTS resolved to /Users/blove/repos/pretable/next-abort-results — inside
  the repo the plan forbids writing to. Off by one in the ../ count.
- The four files as written do not build; Next's generated /_global-error needs
  a root route.

And one found while rewriting: the driver took an id but the link hardcoded
?id=nav, so every per-run grep would have matched nothing — and an absent
marker is exactly what this twin reads as 'render cancelled'. A false pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs(plan): fix the Next build command, record arm B as built

`pnpm --filter next build` does not build workspace dependencies and fails in
~3s on a missing polyfill-module. Turbo's build task has dependsOn ^build and
does. Both Task 3 and Task 5 carried the broken command.

The spec's named risk did not materialise: Next's postinstall downloads a
prebuilt @next/swc-darwin-arm64, so no Rust compiles. Install 56s, build 63s.
The gate stays for later arms, but is no longer the thing to plan around.

Recorded what arm B actually is, since '7 files changed' oversells it: one
source file at +12 lines, six new e2e fixtures. The change registers a close
listener on the PassThrough BEFORE pipeable.pipe(pt), so Node runs it ahead of
React's own handler and aborts with a reason isAbortError recognises.

Also recorded two source facts that make the fix load-bearing rather than
redundant, both of which belong in the upstream write-up: the existing
if (signal) abort block is dead in production because its signal is gated on
__NEXT_DEV_SERVER, which prod inlines as ''; and __NEXT_USE_NODE_STREAMS is
inlined true for the app bundle, so the Node path is the one that serves
production.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs(plan): give arm C its own clone

Task 5 checked out PR #96717 in arm B's clone, which would overwrite arm B's
dist. Both builds have to survive until the measurement window, because the
spec requires all three arms measured back to back under one load profile — and
rebuilding an arm mid-window changes the load the other arms were measured
under. A second clone is 45s and 416M.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs(plan): kill servers by port, and fix stale probe.mjs invocations

Task 1 found pkill -f 'next start -p NNNN' can leave the listener up: the
wrapper exits and the actual 'next-server' child does not carry the pattern.
In the arm loops that is not a tidiness issue — a surviving server means the
next arm's suite runs against the PREVIOUS arm's build and reports a confident
wrong number, which is exactly the class of silent measurement error this plan
exists to avoid. Killing by port cannot miss.

(Verified separately that the pattern DID match for undefined
 ERR_PNPM_RECURSIVE_EXEC_FIRST_FAIL  Command "next" not found, so
the #376 perf measurement, which used that form, stands.)

Tasks 4 and 5 still called probe.mjs, which Task 1 deleted in favour of
drive.mjs — and the argument order swapped from id-then-mode to mode-then-id, so
those calls would have silently measured the wrong thing rather than failing
loudly. Also recorded that drive.mjs needs @playwright/test resolvable from the
twin's own directory, since Node resolves ESM bare specifiers from the importing
file, not from cwd.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs(plan): fix Task 5's clone step, correct arm B's identity claim

Task 5 Step 1 said 'cd $NEXTSRC' — a variable that does not exist — and omitted
the clone entirely, because it was written assuming one shared checkout. Run
from a shell where Task 3 had executed, cwd would still be arm B's clone, so it
would have checked #96717 out on top of arm B and destroyed it. Both arms would
then have measured the same build and agreed, which is the most convincing
possible form of a wrong answer.

Arm C also corrected arm B's recorded claim: stream-ops.node.ts is not
byte-identical between v16.3.0 and the merge-base (a 2-line unrelated drift in
continueFizzStream). The patched REGION is identical. The upstream write-up must
make that distinction rather than overclaim.

Tightened the diff expectation from '~95/~12' to an equality gate; it held
exactly on both arms.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs(plan): record the validation outcome

Both PRs eliminate the error (0/0/0 across three suite runs each) against a
28-42 baseline, and both preserve render cancellation — the distinction the
error count alone could not make, and the reason the cancellation twin existed.

The load confound was real enough to check: the PR arms ran at load 5-8 while
the first baseline ran at 13-25, and lower load narrows the window a disconnect
can land in. Re-measuring the unmodified baseline under the quiet load still
gave 28, so the zeros are the patches.

Two plan assumptions were wrong. A link:ed Next build cannot be used —
Turbopack refuses to compile files outside the workspace root, so its
resolution fails even though Node's succeeds. And pnpm install silently moved
^16.3.0 to 16.3.1, which would have made an arm partly a version bump; that
accident also established that 16.3.1 does not carry either fix.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
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