mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-26 10:13:46 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user