From 7741627af369c8dfbe4dec5cdcff8a96b47736cb Mon Sep 17 00:00:00 2001 From: Nicolas Dorseuil Date: Wed, 25 Nov 2026 21:44:52 +0100 Subject: [PATCH] Read Shiki assets through the ASSETS binding on Cloudflare The deployed server worker uses openNext/customWorkers/defaultWrangler.jsonc directly (.github deploy actions), so the binding only needs adding there. Co-Authored-By: Claude Opus 5.5 --- .../2026-11-25-shiki-assets-measurements.md | 16 +++++++++++----- .../customWorkers/defaultWrangler.jsonc | 7 +++++++ .../CodeBlock/shiki-assets.server.ts | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/docs/superpowers/plans/2026-11-25-shiki-assets-measurements.md b/docs/superpowers/plans/2026-11-25-shiki-assets-measurements.md index a4bca01f5..e59ae0a15 100644 --- a/docs/superpowers/plans/2026-11-25-shiki-assets-measurements.md +++ b/docs/superpowers/plans/2026-11-25-shiki-assets-measurements.md @@ -17,11 +17,11 @@ Commands (from `packages/gitbook`): | Cold first highlight, 3 langs (median) | 195 ms | 188 ms | | | | RSS after first highlight (median) | 207 MB | 207 MB | | | | **Cloudflare server worker** | | | | | -| `handler.mjs` | 23.61 MB (gzip 5.21 MB) | | | | -| ↳ Shiki in handler | 8.69 MB | | | | -| Upload (wrangler dry-run) | 23 542 KiB (gzip 5 529 KiB) | | | | -| Server heap after first render | 53.3 MB | | | | -| Server heap after load | 71.6 MB | | | | +| `handler.mjs` | 23.61 MB (gzip 5.21 MB) | — | 14.85 MB (gzip 3.82 MB) | | +| ↳ Shiki in handler | 8.69 MB | — | 0 | | +| Upload (wrangler dry-run) | 23 542 KiB (gzip 5 529 KiB) | — | 15 032 KiB (gzip 4 184 KiB) | | +| Server heap after first render | 53.3 MB | — | 52.9 MB | | +| Server heap after load | 71.6 MB | — | 52.9 MB (not comparable, see notes) | | | **Client** | | | | | | JS chunks | 12.60 MB (437 files) | 3.59 MB (142 files) | | | | ↳ grammar chunks | 8.19 MB (236 files) | 0 (2 remaining `scopeName` chunks are the Shiki engine and an unrelated lib) | | | @@ -36,3 +36,9 @@ Notes: - Vercel: the grammars still ship in the function, as on-demand JSON instead of module files; the trace only shrinks by ~1 MB. A dynamic `join(process.cwd(), 'public/~gitbook/static', path)` made file tracing pull all of `static` (65 MB trace); keeping `shiki` in the literal fixed it. +- Cloudflare heap: the 3 follow-up requests re-rendered in the baseline run (~11 s each) but were + served in <1 s after Task 3, so "after load" is not comparable. Heap after the first render is + the like-for-like number: unchanged, since lazily imported grammar modules were never evaluated + unless used. The Cloudflare gain is script size (−37 % raw, −24 % gzip upload), not heap. +- Cloudflare render verified on the split workers: quickstart page 200, 154 highlighted tokens + (same as baseline), grammars read through the server worker's `ASSETS` binding. diff --git a/packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc b/packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc index 9d98c9150..fe7c2bf1d 100644 --- a/packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc +++ b/packages/gitbook/openNext/customWorkers/defaultWrangler.jsonc @@ -12,6 +12,13 @@ "observability": { "enabled": false, }, + // Lets the server read Shiki grammars and themes without a network hop. + // run_worker_first keeps every request going to the worker, as before. + "assets": { + "directory": "../../.open-next/assets", + "binding": "ASSETS", + "run_worker_first": true, + }, "vars": { "NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE": "true", }, diff --git a/packages/gitbook/src/components/DocumentView/CodeBlock/shiki-assets.server.ts b/packages/gitbook/src/components/DocumentView/CodeBlock/shiki-assets.server.ts index c496fe6fb..df0e0b539 100644 --- a/packages/gitbook/src/components/DocumentView/CodeBlock/shiki-assets.server.ts +++ b/packages/gitbook/src/components/DocumentView/CodeBlock/shiki-assets.server.ts @@ -2,6 +2,7 @@ import { readFile } from 'node:fs/promises'; import { join } from 'node:path'; import type { ShikiAssetLoader } from './highlighter'; +import { getCloudflareContext } from '@/lib/data/cloudflare'; const SHIKI_PREFIX = 'shiki/'; @@ -12,6 +13,23 @@ export const loadShikiAssetOnServer: ShikiAssetLoader = async (path) => { if (!path.startsWith(SHIKI_PREFIX)) { throw new Error(`Not a Shiki asset: ${path}`); } + + const cloudflare = getCloudflareContext(); + if (cloudflare) { + const assets = cloudflare.env.ASSETS; + if (!assets) { + throw new Error('The ASSETS binding is missing on this worker'); + } + // The host is ignored by the assets binding, only the path matters. + const response = await assets.fetch( + new URL(`/~gitbook/static/${path}`, 'https://assets.local') + ); + if (!response.ok) { + throw new Error(`Failed to read ${path} from assets: ${response.status}`); + } + return response.json(); + } + // Keep `shiki` in the literal so file tracing only pulls this folder, not all of `static`. const file = join( process.cwd(),