@drupflare/spool 0.0.0-stage โ†’ 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1557 -2
  3. package/dist/cdn/client.js +1 -0
  4. package/dist/cdn/location.d.ts +11 -0
  5. package/dist/cdn/location.js +15 -0
  6. package/dist/cdn/manifest.json +16 -0
  7. package/dist/cdn/spool-sw.js +1 -0
  8. package/dist/client/entry.d.ts +1 -0
  9. package/dist/client/entry.js +25 -0
  10. package/dist/client/index.d.ts +3 -0
  11. package/dist/client/index.js +2 -0
  12. package/dist/client/lifecycle.d.ts +10 -0
  13. package/dist/client/lifecycle.js +10 -0
  14. package/dist/client/loads.d.ts +16 -0
  15. package/dist/client/loads.js +41 -0
  16. package/dist/client/register.d.ts +34 -0
  17. package/dist/client/register.js +58 -0
  18. package/dist/index.d.ts +2 -0
  19. package/dist/index.js +2 -0
  20. package/dist/node/listener.d.ts +19 -0
  21. package/dist/node/listener.js +76 -0
  22. package/dist/protocol/artifact.d.ts +54 -0
  23. package/dist/protocol/artifact.js +79 -0
  24. package/dist/protocol/assembly.d.ts +34 -0
  25. package/dist/protocol/assembly.js +72 -0
  26. package/dist/protocol/beacon.d.ts +59 -0
  27. package/dist/protocol/beacon.js +86 -0
  28. package/dist/protocol/collector.d.ts +41 -0
  29. package/dist/protocol/collector.js +84 -0
  30. package/dist/protocol/config.d.ts +89 -0
  31. package/dist/protocol/config.js +13 -0
  32. package/dist/protocol/envelope.d.ts +47 -0
  33. package/dist/protocol/envelope.js +162 -0
  34. package/dist/protocol/errors.d.ts +7 -0
  35. package/dist/protocol/errors.js +9 -0
  36. package/dist/protocol/graph.d.ts +81 -0
  37. package/dist/protocol/graph.js +201 -0
  38. package/dist/protocol/index.d.ts +19 -0
  39. package/dist/protocol/index.js +19 -0
  40. package/dist/protocol/manifest.d.ts +29 -0
  41. package/dist/protocol/manifest.js +94 -0
  42. package/dist/protocol/normalise.d.ts +20 -0
  43. package/dist/protocol/normalise.js +29 -0
  44. package/dist/protocol/pattern.d.ts +12 -0
  45. package/dist/protocol/pattern.js +82 -0
  46. package/dist/protocol/provision.d.ts +100 -0
  47. package/dist/protocol/provision.js +105 -0
  48. package/dist/protocol/route.d.ts +179 -0
  49. package/dist/protocol/route.js +135 -0
  50. package/dist/protocol/sync.d.ts +31 -0
  51. package/dist/protocol/sync.js +87 -0
  52. package/dist/protocol/tag.d.ts +12 -0
  53. package/dist/protocol/tag.js +26 -0
  54. package/dist/protocol/template/compile.d.ts +35 -0
  55. package/dist/protocol/template/compile.js +82 -0
  56. package/dist/protocol/template/interpret.d.ts +9 -0
  57. package/dist/protocol/template/interpret.js +82 -0
  58. package/dist/protocol/vector.d.ts +32 -0
  59. package/dist/protocol/vector.js +56 -0
  60. package/dist/protocol/view.d.ts +110 -0
  61. package/dist/protocol/view.js +201 -0
  62. package/dist/proxy/eligible.d.ts +45 -0
  63. package/dist/proxy/eligible.js +96 -0
  64. package/dist/proxy/index.d.ts +4 -0
  65. package/dist/proxy/index.js +4 -0
  66. package/dist/proxy/proxy.d.ts +86 -0
  67. package/dist/proxy/proxy.js +388 -0
  68. package/dist/proxy/purge.d.ts +10 -0
  69. package/dist/proxy/purge.js +66 -0
  70. package/dist/proxy/tags.d.ts +8 -0
  71. package/dist/proxy/tags.js +36 -0
  72. package/dist/report/derive.d.ts +221 -0
  73. package/dist/report/derive.js +490 -0
  74. package/dist/report/economics.d.ts +14 -0
  75. package/dist/report/economics.js +24 -0
  76. package/dist/report/index.d.ts +8 -0
  77. package/dist/report/index.js +8 -0
  78. package/dist/report/inputs.d.ts +141 -0
  79. package/dist/report/inputs.js +216 -0
  80. package/dist/report/measurement.d.ts +48 -0
  81. package/dist/report/measurement.js +114 -0
  82. package/dist/report/model.d.ts +42 -0
  83. package/dist/report/model.js +74 -0
  84. package/dist/report/thirdparty.d.ts +62 -0
  85. package/dist/report/thirdparty.js +107 -0
  86. package/dist/report/traffic.d.ts +140 -0
  87. package/dist/report/traffic.js +228 -0
  88. package/dist/report/trim.d.ts +81 -0
  89. package/dist/report/trim.js +105 -0
  90. package/dist/server/beacon.d.ts +20 -0
  91. package/dist/server/beacon.js +47 -0
  92. package/dist/server/embedded.d.ts +6 -0
  93. package/dist/server/embedded.js +6 -0
  94. package/dist/server/ga4.d.ts +70 -0
  95. package/dist/server/ga4.js +165 -0
  96. package/dist/server/identity.d.ts +12 -0
  97. package/dist/server/identity.js +17 -0
  98. package/dist/server/index.d.ts +7 -0
  99. package/dist/server/index.js +7 -0
  100. package/dist/server/sink.d.ts +56 -0
  101. package/dist/server/sink.js +133 -0
  102. package/dist/server/size.d.ts +7 -0
  103. package/dist/server/size.js +18 -0
  104. package/dist/server/spool.d.ts +97 -0
  105. package/dist/server/spool.js +463 -0
  106. package/dist/server/stub.d.ts +9 -0
  107. package/dist/server/stub.js +19 -0
  108. package/dist/server/sync.d.ts +12 -0
  109. package/dist/server/sync.js +48 -0
  110. package/dist/server/work.d.ts +22 -0
  111. package/dist/server/work.js +30 -0
  112. package/dist/stores/durable-object.d.ts +19 -0
  113. package/dist/stores/durable-object.js +53 -0
  114. package/dist/stores/index.d.ts +2 -0
  115. package/dist/stores/index.js +2 -0
  116. package/dist/stores/memory.d.ts +4 -0
  117. package/dist/stores/memory.js +10 -0
  118. package/dist/stores/version-store.d.ts +11 -0
  119. package/dist/stores/version-store.js +1 -0
  120. package/dist/sw/analytics.d.ts +81 -0
  121. package/dist/sw/analytics.js +275 -0
  122. package/dist/sw/collect.d.ts +8 -0
  123. package/dist/sw/collect.js +33 -0
  124. package/dist/sw/core.d.ts +1 -0
  125. package/dist/sw/core.js +3 -0
  126. package/dist/sw/freshness.d.ts +15 -0
  127. package/dist/sw/freshness.js +20 -0
  128. package/dist/sw/graph/index.d.ts +67 -0
  129. package/dist/sw/graph/index.js +170 -0
  130. package/dist/sw/graph/invalidate.d.ts +23 -0
  131. package/dist/sw/graph/invalidate.js +109 -0
  132. package/dist/sw/graph/projections.d.ts +7 -0
  133. package/dist/sw/graph/projections.js +9 -0
  134. package/dist/sw/graph/records.d.ts +7 -0
  135. package/dist/sw/graph/records.js +9 -0
  136. package/dist/sw/graph/store.d.ts +32 -0
  137. package/dist/sw/graph/store.js +52 -0
  138. package/dist/sw/idb.d.ts +8 -0
  139. package/dist/sw/idb.js +117 -0
  140. package/dist/sw/index.d.ts +2 -0
  141. package/dist/sw/index.js +1 -0
  142. package/dist/sw/partition.d.ts +21 -0
  143. package/dist/sw/partition.js +137 -0
  144. package/dist/sw/policy.d.ts +8 -0
  145. package/dist/sw/policy.js +24 -0
  146. package/dist/sw/provision.d.ts +39 -0
  147. package/dist/sw/provision.js +61 -0
  148. package/dist/sw/render.d.ts +21 -0
  149. package/dist/sw/render.js +53 -0
  150. package/dist/sw/replica.d.ts +77 -0
  151. package/dist/sw/replica.js +299 -0
  152. package/dist/sw/router.d.ts +42 -0
  153. package/dist/sw/router.js +48 -0
  154. package/dist/sw/routing.d.ts +23 -0
  155. package/dist/sw/routing.js +48 -0
  156. package/dist/sw/runtime.d.ts +112 -0
  157. package/dist/sw/runtime.js +1070 -0
  158. package/dist/sw/scheduler.d.ts +83 -0
  159. package/dist/sw/scheduler.js +149 -0
  160. package/dist/sw/seal.d.ts +37 -0
  161. package/dist/sw/seal.js +183 -0
  162. package/dist/sw/serve.d.ts +15 -0
  163. package/dist/sw/serve.js +63 -0
  164. package/dist/sw/shadow.d.ts +16 -0
  165. package/dist/sw/shadow.js +27 -0
  166. package/dist/sw/static.d.ts +17 -0
  167. package/dist/sw/static.js +81 -0
  168. package/dist/sw/storage.d.ts +64 -0
  169. package/dist/sw/storage.js +34 -0
  170. package/dist/sw/thirdparty.d.ts +17 -0
  171. package/dist/sw/thirdparty.js +36 -0
  172. package/dist/trim/css.d.ts +38 -0
  173. package/dist/trim/css.js +328 -0
  174. package/dist/trim/index.d.ts +3 -0
  175. package/dist/trim/index.js +3 -0
  176. package/dist/trim/manifest.d.ts +67 -0
  177. package/dist/trim/manifest.js +134 -0
  178. package/dist/trim/tokens.d.ts +43 -0
  179. package/dist/trim/tokens.js +142 -0
  180. package/dist/version.d.ts +2 -0
  181. package/dist/version.js +3 -0
  182. package/package.json +146 -5
package/README.md CHANGED
@@ -1,3 +1,1558 @@
1
- # Temporary Holding Version
1
+ # ๐Ÿงต spool
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > An incremental application replica for the browser
4
+
5
+ [![Build](https://github.com/drupflare/spool/actions/workflows/build.yml/badge.svg)](https://github.com/drupflare/spool/actions/workflows/build.yml)
6
+ [![Browser](https://github.com/drupflare/spool/actions/workflows/browser.yml/badge.svg)](https://github.com/drupflare/spool/actions/workflows/browser.yml)
7
+ [![Prettier](https://github.com/drupflare/spool/actions/workflows/prettier.yml/badge.svg)](https://github.com/drupflare/spool/actions/workflows/prettier.yml)
8
+ [![codecov](https://codecov.io/gh/drupflare/spool/branch/master/graph/badge.svg)](https://codecov.io/gh/drupflare/spool)
9
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
10
+
11
+ spool keeps a replica of what a page needs on the visitor's device. The server sends templates
12
+ and authorised, versioned data. A service worker stores them and computes eligible pages on the
13
+ device, so a repeat view makes no request and costs the server no work.
14
+
15
+ It runs on any runtime that speaks `Request` and `Response`, bridges `node:http` for the rest, and
16
+ has no runtime dependencies. A site with no server code can load it from two files and a manifest.
17
+
18
+ ---
19
+
20
+ ## ๐Ÿ“‹ Table of Contents
21
+
22
+ - [Why Spool](#-why-spool)
23
+ - [Install](#-install)
24
+ - [Quick Start](#-quick-start)
25
+ - [Page Runtime](#-page-runtime)
26
+ - [Runtimes](#-runtimes)
27
+ - [Compatibility](#-compatibility)
28
+ - [Static Sites](#-static-sites)
29
+ - [Strategies](#-strategies)
30
+ - [Freshness Contracts](#-freshness-contracts)
31
+ - [Authenticated Data](#-authenticated-data)
32
+ - [Catalogue](#-catalogue)
33
+ - [Analytics Batching](#-analytics-batching)
34
+ - [Third-Party Files](#-third-party-files)
35
+ - [Subresource Handling](#-subresource-handling)
36
+ - [Shadow Mode](#-shadow-mode)
37
+ - [Proxy Mode](#-proxy-mode)
38
+ - [Trimmed Stylesheets](#-trimmed-stylesheets)
39
+ - [Out of Scope](#-out-of-scope)
40
+ - [Browser Support](#-browser-support)
41
+ - [Subpath Exports](#-subpath-exports)
42
+ - [Testing](#-testing)
43
+ - [Related Repositories](#-related-repositories)
44
+ - [License](#-license)
45
+
46
+ ---
47
+
48
+ ## ๐ŸŽฏ Why Spool
49
+
50
+ A response is a deterministic transformation of a set of authorised, versioned dependencies. The
51
+ server half names those dependencies and sends the snapshot the visitor may hold. The worker keeps
52
+ a graph of records, projections and responses, with their versions, and syncs only what the next
53
+ page needs.
54
+
55
+ Your application keeps every write, every access decision and every page it cannot reproduce
56
+ safely on the device. A route the worker cannot answer goes to the network, so adopting spool
57
+ changes no page that was not declared.
58
+
59
+ [docs/impact.md](docs/impact.md) holds the energy model and the measurements behind it.
60
+
61
+ ---
62
+
63
+ ## ๐Ÿ“ฅ Install
64
+
65
+ ### Package
66
+
67
+ ```sh
68
+ bun add @drupflare/spool
69
+ ```
70
+
71
+ The package ships ES modules and type declarations. Node, Deno, Bun, Workers and bundlers import
72
+ it directly.
73
+
74
+ ### CDN Script
75
+
76
+ A page can load the runtime from `cdn.drupflare.com` with a Subresource Integrity hash. The release
77
+ notes list the hash for each version.
78
+
79
+ ```html
80
+ <script
81
+ src="https://cdn.drupflare.com/spool/<version>/client.js"
82
+ integrity="sha384-<hash from the release notes>"
83
+ crossorigin="anonymous"
84
+ defer
85
+ data-spool-client
86
+ ></script>
87
+ ```
88
+
89
+ [Page Runtime](#-page-runtime) shows how `createSpool` injects the same tag.
90
+
91
+ ### Direct Files
92
+
93
+ `spool-sw.js`, `client.js` and `manifest.json` are attached to every GitHub release and published
94
+ under `https://cdn.drupflare.com/spool/<version>/`. A site that serves them as plain files needs no
95
+ server library. See [Static Sites](#-static-sites).
96
+
97
+ ---
98
+
99
+ ## ๐Ÿš€ Quick Start
100
+
101
+ Every runtime below makes the same three moves:
102
+
103
+ 1. Declare the routes the device may answer, and the version store that tracks changes.
104
+ 2. Wrap the application's handler with `spool.handle`. It answers `/.spool/*` and `/spool-sw.js`.
105
+ 3. Send the client tag, which is the script tag for the page runtime. `handle` injects it into
106
+ every HTML page the application returns, and `spool.decorate` does the same where there is no
107
+ Fetch response to wrap.
108
+
109
+ ### Declare Routes
110
+
111
+ A route has a path pattern, a strategy, the tags it depends on and a `load` function that returns
112
+ the data for one page. `load` runs on the server and may check the visitor's identity. Only its
113
+ result reaches the device.
114
+
115
+ ```ts
116
+ import { defineRoute } from '@drupflare/spool/server';
117
+
118
+ export const routes = [
119
+ defineRoute('/node/:id', {
120
+ strategy: 'template',
121
+ template: {
122
+ source:
123
+ '<!doctype html><html><head><title>Node {{id}}</title></head><body><h1>Node {{id}}</h1></body></html>'
124
+ },
125
+ depends: (ctx) => [`node:${ctx.params['id']}`],
126
+ load: (ctx) => ({ id: ctx.params['id'] })
127
+ })
128
+ ];
129
+
130
+ const escape = (text: string) => text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
131
+
132
+ export const page = (request: Request) => {
133
+ const id = escape(new URL(request.url).pathname.split('/')[2] ?? '');
134
+ return new Response(
135
+ `<!doctype html><html><head><title>Node ${id}</title></head><body><h1>Node ${id}</h1></body></html>`,
136
+ { headers: { 'content-type': 'text/html; charset=utf-8' } }
137
+ );
138
+ };
139
+ ```
140
+
141
+ `page` stands in for your application. The page it returns has to match what the template
142
+ renders from the route's data, because the device renders the same route without asking you.
143
+
144
+ The version store records which tags changed. `memoryStore` serves a single process. Every other
145
+ deployment needs a store that all instances share, which is any object with three methods:
146
+ `bump(tags)`, `cursor()` and `diff(from)`. On Cloudflare, `durableObjectStore` keeps the vector in
147
+ a Durable Object.
148
+
149
+ ```ts
150
+ import { memoryStore } from '@drupflare/spool/stores';
151
+
152
+ export const store = memoryStore();
153
+ ```
154
+
155
+ `createSpool` joins the two:
156
+
157
+ ```ts
158
+ import { createSpool } from '@drupflare/spool/server';
159
+ import { routes } from './routes';
160
+ import { store } from './store';
161
+
162
+ export const spool = createSpool({ routes, store });
163
+ ```
164
+
165
+ After a write, call `await spool.invalidate(['node:1'])` with the tags it touched. Devices
166
+ holding a page that depends on them fetch a fresh copy before they show it again.
167
+
168
+ ---
169
+
170
+ ## ๐Ÿ“ฆ Page Runtime
171
+
172
+ `handle` and `decorate` inject one script tag into each HTML page. By default the script is
173
+ served from the same origin at `/.spool/<version>/client.js`, which suits a strict
174
+ `script-src 'self'` policy. To serve it from `cdn.drupflare.com` instead, pass the integrity hash
175
+ that the release notes list for your version. The URL defaults to that version's file.
176
+
177
+ ```ts
178
+ import { createSpool } from '@drupflare/spool/server';
179
+ import { memoryStore } from '@drupflare/spool/stores';
180
+ import { routes } from './routes';
181
+
182
+ export const spool = createSpool({
183
+ routes,
184
+ store: memoryStore(),
185
+ cdn: { integrity: 'sha384-<hash from the release notes>' }
186
+ });
187
+ ```
188
+
189
+ The CDN copy carries `Cache-Control: public, max-age=31536000, immutable` and the browser checks
190
+ it against the hash, so a changed file never runs. Release files are never overwritten. If the
191
+ script fails to load, the page works as it did before.
192
+
193
+ The script registers `/spool-sw.js` and exposes `window.spool.ready`, which resolves to the client
194
+ handle or to `null` where service workers do not exist. To bundle the runtime yourself, import
195
+ `register` from `@drupflare/spool/client`.
196
+
197
+ ---
198
+
199
+ ## ๐Ÿงฉ Runtimes
200
+
201
+ Each example uses the `routes`, `page` and `spool` from [Quick Start](#-quick-start). The
202
+ [compatibility table](#-compatibility) lists what each framework hands a handler.
203
+
204
+ ### Cloudflare Workers
205
+
206
+ Bind the Durable Object that holds the version vector, then pass its namespace to
207
+ `durableObjectStore`.
208
+
209
+ ```jsonc
210
+ {
211
+ "durable_objects": { "bindings": [{ "name": "SPOOL_VERSIONS", "class_name": "SpoolVersions" }] },
212
+ "migrations": [{ "tag": "v1", "new_sqlite_classes": ["SpoolVersions"] }]
213
+ }
214
+ ```
215
+
216
+ ```ts
217
+ import { createSpool } from '@drupflare/spool/server';
218
+ import { durableObjectStore } from '@drupflare/spool/stores/durable-object';
219
+ import { page, routes } from './routes';
220
+
221
+ export { SpoolVersions } from '@drupflare/spool/stores/durable-object';
222
+
223
+ export default {
224
+ fetch(request: Request, env: Cloudflare.Env) {
225
+ const spool = createSpool({ routes, store: durableObjectStore(env.SPOOL_VERSIONS) });
226
+ return spool.handle(request, page);
227
+ }
228
+ };
229
+ ```
230
+
231
+ ### Cloudflare Pages Functions
232
+
233
+ A function takes a context with the request and a `next` that runs the next function or the
234
+ static asset server.
235
+
236
+ ```ts
237
+ import { spool } from './spool';
238
+
239
+ export const onRequest = (context: { request: Request; next(): Promise<Response> }) =>
240
+ spool.handle(context.request, () => context.next());
241
+ ```
242
+
243
+ ### Hono
244
+
245
+ Hono ignores the response a middleware returns once a route has answered, so assign it to
246
+ `c.res`.
247
+
248
+ ```ts
249
+ import { Hono } from 'hono';
250
+ import { page } from './routes';
251
+ import { spool } from './spool';
252
+
253
+ const app = new Hono();
254
+
255
+ app.use('*', async (c, next) => {
256
+ c.res = await spool.handle(c.req.raw, async () => {
257
+ await next();
258
+ return c.res;
259
+ });
260
+ });
261
+ app.get('/node/:id', (c) => page(c.req.raw));
262
+
263
+ export default app;
264
+ ```
265
+
266
+ ### Elysia
267
+
268
+ `onRequest` answers the spool paths and returns nothing for the rest. The hook has to be an
269
+ `async` function, because Elysia does not await a promise that a plain function returns.
270
+
271
+ ```ts
272
+ import { Elysia } from 'elysia';
273
+ import { page } from './routes';
274
+ import { spool } from './spool';
275
+
276
+ export const app = new Elysia()
277
+ .onRequest(async ({ request }) => {
278
+ const { pathname } = new URL(request.url);
279
+ return pathname === '/spool-sw.js' ? spool.stub() : await spool.respond(request);
280
+ })
281
+ .get('/node/:id', ({ request }) => spool.decorate(request, page(request)));
282
+ ```
283
+
284
+ ### itty-router
285
+
286
+ A handler that returns `undefined` passes the request to the next one, which is what
287
+ `spool.respond` returns for a path that is not spool's.
288
+
289
+ ```ts
290
+ import { IttyRouter } from 'itty-router';
291
+ import { page } from './routes';
292
+ import { spool } from './spool';
293
+
294
+ const router = IttyRouter();
295
+
296
+ router
297
+ .all('*', (request) => spool.respond(request))
298
+ .get('/spool-sw.js', () => spool.stub())
299
+ .get('/node/:id', (request) => spool.decorate(request, page(request)));
300
+
301
+ export default { fetch: router.fetch };
302
+ ```
303
+
304
+ ### Bun
305
+
306
+ Bun serves a default export that has a `fetch` function.
307
+
308
+ ```ts
309
+ import { page } from './routes';
310
+ import { spool } from './spool';
311
+
312
+ export default {
313
+ port: 3000,
314
+ fetch: (request: Request) => spool.handle(request, page)
315
+ };
316
+ ```
317
+
318
+ ### Deno
319
+
320
+ Run it with `deno serve`.
321
+
322
+ ```ts
323
+ import { page } from './routes';
324
+ import { spool } from './spool';
325
+
326
+ export default {
327
+ fetch: (request: Request) => spool.handle(request, page)
328
+ };
329
+ ```
330
+
331
+ ### Netlify Functions
332
+
333
+ A function takes a `Request` and returns a `Response`. `config.path` sends the spool paths and the
334
+ pages to it.
335
+
336
+ ```ts
337
+ import type { Config, Context } from '@netlify/functions';
338
+ import { page } from './routes';
339
+ import { spool } from './spool';
340
+
341
+ export default (request: Request, context: Context) => spool.handle(request, page);
342
+
343
+ export const config: Config = { path: ['/node/*', '/spool-sw.js', '/.spool/*'] };
344
+ ```
345
+
346
+ ### Netlify Edge Functions
347
+
348
+ An edge function runs in front of the site. `context.next()` returns the response the site would
349
+ have sent, which `handle` decorates.
350
+
351
+ ```ts
352
+ import type { Config, Context } from '@netlify/edge-functions';
353
+ import { spool } from './spool';
354
+
355
+ export default (request: Request, context: Context) => spool.handle(request, () => context.next());
356
+
357
+ export const config: Config = { path: '/*' };
358
+ ```
359
+
360
+ ### Vercel Functions
361
+
362
+ A function under `api/` takes a default export with `fetch`. Send every path to it from
363
+ `vercel.json`, so the function sees `/.spool/*` and `/spool-sw.js` as well as the pages. Function
364
+ instances do not share memory, so `store` has to be a shared `VersionStore`.
365
+
366
+ ```json
367
+ { "rewrites": [{ "source": "/(.*)", "destination": "/api" }] }
368
+ ```
369
+
370
+ ```ts
371
+ import { page } from './routes';
372
+ import { spool } from './spool';
373
+
374
+ export default {
375
+ fetch: (request: Request) => spool.handle(request, page)
376
+ };
377
+ ```
378
+
379
+ ### Next.js
380
+
381
+ Next.js route handlers return `Response` objects, so `handle` wraps one directly. The spool
382
+ endpoints and the worker script go through the proxy, which returns `undefined` for every other
383
+ request. Next.js 16 calls the file `proxy.ts` and the export `proxy`. Earlier versions call them
384
+ `middleware.ts` and `middleware`. The proxy and the route handlers need the same shared store.
385
+
386
+ ```ts
387
+ import { spool } from './spool';
388
+
389
+ export function proxy(request: Request) {
390
+ const { pathname } = new URL(request.url);
391
+ return pathname === '/spool-sw.js' ? spool.stub() : spool.respond(request);
392
+ }
393
+ ```
394
+
395
+ ```ts
396
+ import { page } from './routes';
397
+ import { spool } from './spool';
398
+
399
+ export const GET = (request: Request) => spool.handle(request, page);
400
+ ```
401
+
402
+ ### Nuxt and Nitro
403
+
404
+ h3 turns an event into a standard request with `toWebRequest`, and it sends a `Response` that a
405
+ handler returns. A server middleware that returns nothing passes the request on. Put the first
406
+ file in `server/middleware/` and the second in `server/routes/node/[id].ts`. Both share one store
407
+ from `./store`.
408
+
409
+ ```ts
410
+ import { defineEventHandler, toWebRequest } from 'h3';
411
+ import { spool } from './spool';
412
+
413
+ export default defineEventHandler((event) => {
414
+ const request = toWebRequest(event);
415
+ return new URL(request.url).pathname === '/spool-sw.js' ? spool.stub() : spool.respond(request);
416
+ });
417
+ ```
418
+
419
+ ```ts
420
+ import { defineEventHandler, toWebRequest } from 'h3';
421
+ import { page } from './routes';
422
+ import { spool } from './spool';
423
+
424
+ export default defineEventHandler((event) => spool.handle(toWebRequest(event), page));
425
+ ```
426
+
427
+ ### AWS Lambda
428
+
429
+ Lambda gives a function a JSON event and expects a JSON result, so spool needs an adapter. Hono's
430
+ `hono/aws-lambda` converts Function URL, API Gateway HTTP API and ALB events. Save the Hono app
431
+ above as `hono.ts` and wrap it.
432
+
433
+ ```ts
434
+ import { handle } from 'hono/aws-lambda';
435
+ import app from './hono';
436
+
437
+ export const handler = handle(app);
438
+ ```
439
+
440
+ ### Azure Functions
441
+
442
+ An Azure v4 HTTP function takes an `HttpRequest`, which follows the Fetch standard, and returns an
443
+ `HttpResponseInit`. Route every path to it with `{*path}`.
444
+
445
+ ```ts
446
+ import { app, type HttpRequest, type HttpResponseInit } from '@azure/functions';
447
+ import { page } from './routes';
448
+ import { spool } from './spool';
449
+
450
+ export async function handler(request: HttpRequest): Promise<HttpResponseInit> {
451
+ const hasBody = request.method !== 'GET' && request.method !== 'HEAD';
452
+ const response = await spool.handle(
453
+ new Request(request.url, {
454
+ method: request.method,
455
+ headers: [...request.headers],
456
+ body: hasBody ? await request.arrayBuffer() : undefined
457
+ }),
458
+ page
459
+ );
460
+ return {
461
+ status: response.status,
462
+ headers: [...response.headers],
463
+ body: await response.arrayBuffer()
464
+ };
465
+ }
466
+
467
+ app.http('spool', {
468
+ route: '{*path}',
469
+ methods: ['GET', 'POST'],
470
+ authLevel: 'anonymous',
471
+ handler
472
+ });
473
+ ```
474
+
475
+ ### Node
476
+
477
+ `toNodeListener` turns a Fetch-style handler into a `node:http` listener. Its `toRequest` and
478
+ `writeResponse` parts are what the frameworks below use to cross between Node and Fetch.
479
+
480
+ ```ts
481
+ import { createServer } from 'node:http';
482
+ import { toNodeListener } from '@drupflare/spool/node';
483
+ import { page } from './routes';
484
+ import { spool } from './spool';
485
+
486
+ export const server = createServer(toNodeListener((request) => spool.handle(request, page)));
487
+ ```
488
+
489
+ ### Express
490
+
491
+ Express responds through its own `res`, so it has no Fetch response to wrap. `spool.respond`
492
+ answers `/.spool/*` and returns `undefined` for everything else, which sends the request on to the
493
+ next Express handler. A route passes its page through `spool.decorate` to get the envelope and the
494
+ script tag.
495
+
496
+ ```ts
497
+ import express from 'express';
498
+ import { toNodeListener, toRequest, writeResponse } from '@drupflare/spool/node';
499
+ import { page } from './routes';
500
+ import { spool } from './spool';
501
+
502
+ export const app = express();
503
+
504
+ app.use(toNodeListener((request) => spool.respond(request)));
505
+ app.get('/spool-sw.js', (req, res) => writeResponse(res, spool.stub()));
506
+ app.get('/node/:id', async (req, res) => {
507
+ const request = toRequest(req, res);
508
+ await writeResponse(res, await spool.decorate(request, page(request)));
509
+ });
510
+ ```
511
+
512
+ ### Fastify
513
+
514
+ Fastify's `reply.send` takes a Fetch `Response`. An `onRequest` hook answers the spool paths from
515
+ the raw request before Fastify reads the body.
516
+
517
+ ```ts
518
+ import Fastify from 'fastify';
519
+ import { toRequest } from '@drupflare/spool/node';
520
+ import { page } from './routes';
521
+ import { spool } from './spool';
522
+
523
+ export const app = Fastify();
524
+
525
+ app.addHook('onRequest', async (req, reply) => {
526
+ const response = await spool.respond(toRequest(req.raw, reply.raw));
527
+ if (response) return reply.send(response);
528
+ });
529
+ app.get('/spool-sw.js', (req, reply) => reply.send(spool.stub()));
530
+ app.get('/node/:id', async (req, reply) => {
531
+ const request = toRequest(req.raw, reply.raw);
532
+ return reply.send(await spool.decorate(request, page(request)));
533
+ });
534
+ ```
535
+
536
+ ### Koa
537
+
538
+ Koa exposes the Node objects as `ctx.req` and `ctx.res`. Setting `ctx.respond` to `false` stops
539
+ Koa from writing a second response.
540
+
541
+ ```ts
542
+ import Koa from 'koa';
543
+ import { toRequest, writeResponse } from '@drupflare/spool/node';
544
+ import { page } from './routes';
545
+ import { spool } from './spool';
546
+
547
+ export const app = new Koa();
548
+
549
+ app.use(async (ctx, next) => {
550
+ const request = toRequest(ctx.req, ctx.res);
551
+ const response = ctx.path === '/spool-sw.js' ? spool.stub() : await spool.respond(request);
552
+ if (!response) return next();
553
+ ctx.respond = false;
554
+ await writeResponse(ctx.res, response);
555
+ });
556
+ app.use(async (ctx) => {
557
+ if (!ctx.path.startsWith('/node/')) return;
558
+ const request = toRequest(ctx.req, ctx.res);
559
+ ctx.respond = false;
560
+ await writeResponse(ctx.res, await spool.decorate(request, page(request)));
561
+ });
562
+ ```
563
+
564
+ ### hapi
565
+
566
+ hapi keeps the Node objects at `request.raw`. Returning `h.abandon` tells it the response is
567
+ already written.
568
+
569
+ ```ts
570
+ import Hapi from '@hapi/hapi';
571
+ import { toRequest, writeResponse } from '@drupflare/spool/node';
572
+ import { page } from './routes';
573
+ import { spool } from './spool';
574
+
575
+ export const server = Hapi.server();
576
+
577
+ server.ext('onRequest', async (request, h) => {
578
+ const { req, res } = request.raw;
579
+ const spoolRequest = toRequest(req, res);
580
+ const response =
581
+ request.path === '/spool-sw.js' ? spool.stub() : await spool.respond(spoolRequest);
582
+ if (!response) return h.continue;
583
+ await writeResponse(res, response);
584
+ return h.abandon;
585
+ });
586
+ server.route({
587
+ method: 'GET',
588
+ path: '/node/{id}',
589
+ handler: async (request, h) => {
590
+ const { req, res } = request.raw;
591
+ const spoolRequest = toRequest(req, res);
592
+ await writeResponse(res, await spool.decorate(spoolRequest, page(spoolRequest)));
593
+ return h.abandon;
594
+ }
595
+ });
596
+ ```
597
+
598
+ ### NestJS
599
+
600
+ On the Express adapter, `app.use` takes spool's listener and a controller method that injects
601
+ `@Req()` and `@Res()` writes the page. The example needs `experimentalDecorators` in
602
+ `tsconfig.json`.
603
+
604
+ ```ts
605
+ import { Controller, Get, Module, Req, Res } from '@nestjs/common';
606
+ import { NestFactory } from '@nestjs/core';
607
+ import type { Request, Response } from 'express';
608
+ import 'reflect-metadata';
609
+ import { toNodeListener, toRequest, writeResponse } from '@drupflare/spool/node';
610
+ import { page } from './routes';
611
+ import { spool } from './spool';
612
+
613
+ @Controller()
614
+ class NodeController {
615
+ @Get('node/:id')
616
+ async show(@Req() req: Request, @Res() res: Response) {
617
+ const request = toRequest(req, res);
618
+ await writeResponse(res, await spool.decorate(request, page(request)));
619
+ }
620
+ }
621
+
622
+ @Module({ controllers: [NodeController] })
623
+ class AppModule {}
624
+
625
+ export async function bootstrap() {
626
+ const app = await NestFactory.create(AppModule, { logger: false });
627
+ app.use(toNodeListener((request) => spool.respond(request)));
628
+ app.use('/spool-sw.js', (req: Request, res: Response) => writeResponse(res, spool.stub()));
629
+ return app;
630
+ }
631
+ ```
632
+
633
+ ### Google Cloud Functions
634
+
635
+ The Functions Framework gives an HTTP function Express-style `req` and `res`, so the Node listener
636
+ fits as it is.
637
+
638
+ ```ts
639
+ import { http } from '@google-cloud/functions-framework';
640
+ import { toNodeListener } from '@drupflare/spool/node';
641
+ import { page } from './routes';
642
+ import { spool } from './spool';
643
+
644
+ http(
645
+ 'spool',
646
+ toNodeListener((request) => spool.handle(request, page))
647
+ );
648
+ ```
649
+
650
+ ---
651
+
652
+ ## ๐Ÿ“Š Compatibility
653
+
654
+ The gate column says how far each row is checked. **runs** means the fixture above drives the
655
+ framework's own package. **stand-in** means a fixture calls the platform's handler with a plain
656
+ object where the platform would pass its context. **not tested** means the adapter column comes
657
+ from the framework's documentation and nothing here has run it.
658
+
659
+ | Framework or host | What a handler receives | Spool entry | Gate |
660
+ | ------------------------------------ | -------------------------------------------------------------- | ------------------------------------------------------------ | -------------------------------------------- |
661
+ | Express | Node `req`, `res` | `toNodeListener`, `respond`, `decorate` | runs |
662
+ | Fastify | Node `req.raw`, `reply.raw`; `reply.send` takes a `Response` | `toRequest`, `respond`, `decorate` | runs |
663
+ | Koa | Node `ctx.req`, `ctx.res` | `toRequest`, `writeResponse` | runs |
664
+ | hapi | Node `request.raw.req`, `request.raw.res` | `toRequest`, `writeResponse` | runs |
665
+ | NestJS, Express adapter | Node, through Express | `app.use` with `toNodeListener`; `toRequest` in a controller | runs |
666
+ | NestJS, Fastify adapter | Node, through Fastify | the Fastify entry | not tested |
667
+ | AdonisJS | Node at `ctx.request.request` and `ctx.response.response` | `toRequest`, `writeResponse` in a middleware | not tested |
668
+ | Hono | Fetch | `handle` | runs |
669
+ | Elysia | Fetch | `respond`, `decorate` | runs |
670
+ | itty-router | Fetch | `respond`, `decorate` | runs |
671
+ | Oak | Fetch, through `app.handle`; published on JSR only | `handle` | not tested |
672
+ | h3, Nitro, Nuxt | Fetch, through `toWebRequest` | `respond`, `handle` | runs |
673
+ | Analog | Nitro and h3 routes | as Nuxt | not tested |
674
+ | Next.js | Fetch | `respond` in the proxy, `handle` in the route | runs |
675
+ | SvelteKit | Fetch `event.request` in the `handle` hook | `handle(event.request, () => resolve(event))` | not tested |
676
+ | Astro | `context.request` in `onRequest(context, next)` | `handle(context.request, () => next())` | not tested |
677
+ | React Router v7, Remix | Fetch `request` in `handleRequest` and the server build | `handle` around the request handler | not tested |
678
+ | TanStack Start | Fetch, in the server entry's `fetch(request)` | `handle(request, () => handler.fetch(request))` | not tested |
679
+ | SolidStart | an event in `onRequest` that may return a `Response` | `respond` in the middleware | not tested |
680
+ | Qwik City | `requestEv` in `onRequest`; `send(Response)` | `respond` in the middleware | not tested |
681
+ | Cloudflare Workers | Fetch | `handle` | runs, in workerd |
682
+ | Cloudflare Pages Functions | Fetch, in `onRequest(context)` | `handle(request, () => context.next())` | stand-in |
683
+ | Netlify Functions | Fetch | `handle` | stand-in |
684
+ | Netlify Edge Functions | Fetch; returning `undefined` passes on | `handle(request, () => context.next())` | stand-in |
685
+ | Deno, Deno Deploy | Fetch, through `Deno.serve` or `deno serve` | `handle` | stand-in |
686
+ | Bun | Fetch | `handle` | runs, under `Bun.serve` |
687
+ | Vercel Functions | Fetch | `handle` | stand-in |
688
+ | AWS Lambda, Function URL or HTTP API | JSON event version 2.0 | `hono/aws-lambda` around a Hono app | stand-in event |
689
+ | AWS Lambda, API Gateway REST API | JSON event version 1.0 | `hono/aws-lambda` around a Hono app | not tested |
690
+ | Lambda@Edge | CloudFront event with header arrays | none; a converter to `Request` is needed | not tested |
691
+ | Fastly Compute | Fetch, through `event.request` in a `fetch` listener | `event.respondWith(spool.handle(event.request, next))` | not tested |
692
+ | Azure Functions v4 | `HttpRequest`, which follows Fetch; returns `HttpResponseInit` | convert, then `handle` | runs, with a real `HttpRequest` |
693
+ | Google Cloud Functions | Node `req`, `res` from the Functions Framework | `toNodeListener` | runs, on the Functions Framework test server |
694
+ | Google Cloud Run | any HTTP server on `PORT` | the Node example | not tested |
695
+
696
+ A host that serves static files and runs no code, such as an object store or a plain web server, uses
697
+ [Static Sites](#-static-sites).
698
+
699
+ ---
700
+
701
+ ## ๐Ÿงฑ Static Sites
702
+
703
+ A site with no spool server can still answer repeat views from the device. It serves three files
704
+ from one directory:
705
+
706
+ - `spool-sw.js`, the worker, from a release or `https://cdn.drupflare.com/spool/<version>/`.
707
+ - `client.js`, the page script, from the same place.
708
+ - `spool.json`, the list of files the worker may keep.
709
+
710
+ Each page loads the script with the tag from [CDN Script](#-install), or from the site's own copy.
711
+ The worker has no server to talk to, so it reads `spool.json` from the directory its own script
712
+ sits in.
713
+
714
+ ```json
715
+ {
716
+ "schema": 1,
717
+ "routes": [
718
+ {
719
+ "pattern": "/",
720
+ "strategy": "static",
721
+ "version": "sha256-<base64 digest of the page>",
722
+ "freshness": { "maxStale": 3600, "mode": "soft" }
723
+ },
724
+ { "pattern": "/about", "strategy": "static", "version": "sha256-<base64 digest of the page>" },
725
+ { "pattern": "/css/site.css", "strategy": "static", "version": "build-42" },
726
+ {
727
+ "pattern": "/client.js",
728
+ "strategy": "static",
729
+ "version": "sha384-<base64 digest of the file>"
730
+ }
731
+ ]
732
+ }
733
+ ```
734
+
735
+ A route names one path, or a `:name` pattern whose paths share one version. `version` says what
736
+ the file is now:
737
+
738
+ - A `sha256-`, `sha384-` or `sha512-` value is checked against the bytes the worker fetched. A
739
+ mismatch keeps nothing.
740
+ - An entity tag such as `"abc"` is checked against the response's `ETag`.
741
+ - Any other string is a label you change when the file changes.
742
+
743
+ The first view of a path goes to the network, and the worker keeps a copy of the response as it
744
+ arrives: the bytes and the headers, including any `Content-Security-Policy`. Later views of that
745
+ path make no request until the route's `maxStale` passes, which defaults to 15 minutes. After that
746
+ a `soft` route shows the copy and fetches `spool.json` once, and a `hard` route fetches it first.
747
+ A file whose version changed, or whose route left the manifest, is dropped and the next view
748
+ fetches it again. Set `sync` to `deferred` to wait for a request the page makes anyway, and
749
+ `offline` to `refuse` to keep a hard route from showing a copy it cannot check.
750
+
751
+ The worker keeps files up to 4 MiB and never a redirect or an error. It sends no analytics batches
752
+ and makes no request to `/.spool/*`. Serve `spool-sw.js` with `Cache-Control: max-age=86400`; that
753
+ header is what lets Chromium and Firefox skip the update check. To pin the script to the CDN, make
754
+ `/spool-sw.js` a one-line file:
755
+
756
+ ```js
757
+ importScripts('https://cdn.drupflare.com/spool/<version>/spool-sw.js');
758
+ ```
759
+
760
+ `importScripts` has no integrity check, so this form trusts the CDN host with the worker.
761
+
762
+ ---
763
+
764
+ ## ๐Ÿ”€ Strategies
765
+
766
+ A route is answered locally in one of five ways. A `template` route stores JSON and renders it
767
+ through a compiled template. An `assembly` route stores a shell with named holes, fragment markup
768
+ for the holes and values for named slots. A `static` route stores a file as the server sent it. A
769
+ `data` route stores JSON and answers a GET for it. A `projection` route stores JSON and renders a
770
+ page from it with a function your site bundles into its own worker.
771
+
772
+ ```ts
773
+ import { placeholder, slot } from '@drupflare/spool/protocol';
774
+ import { defineRoute } from '@drupflare/spool/server';
775
+
776
+ export const about = defineRoute('/about', {
777
+ strategy: 'assembly',
778
+ depends: () => ['menu'],
779
+ load: () => ({
780
+ shell: `<!doctype html><html><head><title>About</title></head><body>${placeholder('menu')}<main>Updated ${slot('updated')}</main></body></html>`,
781
+ fragments: { menu: '<nav><a href="/">Home</a></nav>' },
782
+ slots: { updated: '2026-10-19' }
783
+ })
784
+ });
785
+ ```
786
+
787
+ The worker leaves a hole it cannot fill in place and refuses to render a page with a gap, so the
788
+ request goes to the network instead.
789
+
790
+ A page the worker renders keeps the site's security headers. The worker remembers
791
+ `Content-Security-Policy` (and its report-only form), `X-Frame-Options`, `X-Content-Type-Options`,
792
+ `Referrer-Policy`, `Permissions-Policy` and the three `Cross-Origin-*` policies from the network
793
+ answer to each path, and sends them with every local render of it. A path with no answer of its own
794
+ uses the latest set from any navigation. A page with neither goes to the network until the worker
795
+ has seen one, so a local view never goes out without the site's policy.
796
+
797
+ ### Records and Projections
798
+
799
+ Pages that read the same data share one stored copy. `ctx.record(id, data)` stores an
800
+ authoritative value once under its id, and `ctx.projection(id, data)` stores a value derived from
801
+ records. Both return a reference to put in the page's data. A record's id is its invalidation tag.
802
+ Invalidating `course:101` refreshes the records and projections that read it, and no others.
803
+
804
+ ```ts
805
+ import { defineRoute } from '@drupflare/spool/server';
806
+
807
+ const courses: Record<string, string> = { '101': 'Intro', '102': 'Advanced' };
808
+
809
+ export const course = defineRoute('/courses/:id', {
810
+ strategy: 'template',
811
+ template: {
812
+ source: '<h1>{{title}}</h1><nav>{{#each menu}}<a>{{this}}</a>{{/each}}</nav>'
813
+ },
814
+ load: (ctx) => {
815
+ const id = ctx.params['id'] ?? '';
816
+ return {
817
+ title: ctx.record(`course:${id}`, courses[id]),
818
+ menu: ctx.projection(
819
+ 'menu',
820
+ Object.keys(courses).map((key) => ctx.record(`course:${key}`, courses[key]))
821
+ )
822
+ };
823
+ }
824
+ });
825
+ ```
826
+
827
+ The server sends only the records and projections the device does not already hold at a current
828
+ version. A sync for the next page transfers the entries that page reads and that changed.
829
+
830
+ ### Templates
831
+
832
+ Templates use Mustache-shaped syntax with no expressions, so the worker needs no `eval`.
833
+
834
+ | Syntax | Meaning |
835
+ | ------------------------------------ | -------------------------------------------------------- |
836
+ | `{{field}}` | the field, HTML-escaped; `a.b` reaches into objects |
837
+ | `{{{field}}}` | the field as markup; allowed only for names in `trusted` |
838
+ | `{{#each list}}...{{/each}}` | repeats for each item; `{{this}}` and `{{@index}}` work |
839
+ | `{{#if field}}...{{else}}...{{/if}}` | renders a branch on whether the field is truthy |
840
+ | `{{! note }}` | a comment that renders nothing |
841
+
842
+ ```ts
843
+ import { defineRoute } from '@drupflare/spool/server';
844
+
845
+ export const news = defineRoute('/news/:slug', {
846
+ strategy: 'template',
847
+ template: {
848
+ source:
849
+ '<article><h1>{{title}}</h1>{{#if byline}}<p>{{byline}}</p>{{else}}<p>Staff</p>{{/if}}<div>{{{body}}}</div><ul>{{#each tags}}<li>{{@index}} {{this}}</li>{{/each}}</ul></article>',
850
+ trusted: ['body']
851
+ },
852
+ depends: (ctx) => [`news:${ctx.params['slug']}`],
853
+ load: () => ({
854
+ title: 'Hello',
855
+ byline: null,
856
+ body: '<p>Trusted markup</p>',
857
+ tags: ['a', 'b']
858
+ })
859
+ });
860
+ ```
861
+
862
+ A template that names a field it does not trust for markup fails when the route is declared.
863
+
864
+ ### Static Files
865
+
866
+ A `static` route has no `load` and no template. The application serves the file as it always did,
867
+ and `handle` or `decorate` marks the response for the worker, which keeps the bytes. A tag in
868
+ `depends` ties the file to a deploy, and `spool.invalidate` after one makes devices fetch the
869
+ file again. A static route is always public and is always answered from the device, so it takes no
870
+ `scope`, `persist` or `mode`. A query string makes a different file: `/app.js?v=2` is not
871
+ answered with the copy of `/app.js`, and a query the worker has not stored goes to the network.
872
+
873
+ ```ts
874
+ import { defineRoute } from '@drupflare/spool/server';
875
+
876
+ export const about = defineRoute('/about', {
877
+ strategy: 'static',
878
+ depends: () => ['site'],
879
+ freshness: { maxStale: 3600, mode: 'soft' }
880
+ });
881
+ ```
882
+
883
+ ### Projection and Data Routes
884
+
885
+ A `data` route answers a GET for JSON, such as an API read, from the stored value, so the page's
886
+ `fetch` makes no request. The worker answers with `JSON.stringify` of what `load` returned, which
887
+ means the application's own handler has to return the same JSON. A navigation to the URL still goes
888
+ to the network.
889
+
890
+ A `projection` route needs a renderer. Spool ships no bundler: write a worker with `serve` from
891
+ `@drupflare/spool/sw`, bundle it with your own toolchain, and pass the bundle to `createSpool` as
892
+ `worker`. The server puts the config in front of your bundle as `self.__SPOOL_CONFIG__`, and
893
+ `spool.config` is the same object as JSON if you would rather inline it at build time. A route with
894
+ no renderer is refused by the worker and the network answers.
895
+
896
+ ```ts
897
+ import { defineRoute } from '@drupflare/spool/server';
898
+
899
+ export const course = defineRoute('/api/courses/:id', {
900
+ strategy: 'data',
901
+ depends: (ctx) => [`course:${ctx.params['id']}`],
902
+ load: (ctx) => ({ id: ctx.params['id'], title: 'Intro' }),
903
+ provision: { reuse: 0.8, futureViews: 6 }
904
+ });
905
+
906
+ export const roster = defineRoute('/rosters/:id', {
907
+ strategy: 'projection',
908
+ depends: (ctx) => [`roster:${ctx.params['id']}`],
909
+ load: (ctx) => ({ id: ctx.params['id'], names: ['Ada', 'Bo'] })
910
+ });
911
+ ```
912
+
913
+ ```ts
914
+ import { escapeHtml } from '@drupflare/spool/protocol';
915
+ import { serve } from '@drupflare/spool/sw';
916
+
917
+ declare const CONFIG: string;
918
+
919
+ serve(JSON.parse(CONFIG), {
920
+ '/rosters/:id': (artifact) => {
921
+ const { names } = (artifact.body as { data: { names: string[] } }).data;
922
+ return `<!doctype html><html><head><title>Roster</title></head><body><ul>${names.map((name) => `<li>${escapeHtml(name)}</li>`).join('')}</ul></body></html>`;
923
+ }
924
+ });
925
+ ```
926
+
927
+ The server also decides whether to send an artifact at all. It weighs the bytes it would send,
928
+ records included, against what a local view saves: reuse times future views times the bytes a view
929
+ avoids, less the cost of the request. When that is not positive it answers `204` with
930
+ `Spool-Provision: declined`, and the worker leaves that path to the network until it restarts.
931
+ The priors are scenario assumptions: set them from your own telemetry in `provision`, or per route
932
+ as above. A `data` route weighs against its own size, so it needs reuse times future views above
933
+
934
+ 1. With the defaults of 0.5 and 2 it breaks even and is left out.
935
+
936
+ ---
937
+
938
+ ## โณ Freshness Contracts
939
+
940
+ Each route declares how long a stored page may go without validation. `maxStale` is in seconds.
941
+
942
+ - **hard:** once `maxStale` passes, the worker validates the page with the server before it
943
+ shows it. Offline, the route's `offline` rule decides.
944
+ - **soft:** the worker shows the stored page at once and syncs afterward. A soft bound is a
945
+ stale-while-revalidate window and promises nothing about how fresh the shown page is.
946
+
947
+ Public routes default to 15 minutes soft, and routes with `scope: 'principal'` to five minutes
948
+ hard; [Authenticated Data](#-authenticated-data) covers how their pages are stored. `offline`
949
+ defaults to `serve` for public routes and `refuse` for principal routes. A soft route may set `sync: 'deferred'` to queue its refresh
950
+ for the next network request that happens anyway.
951
+
952
+ ```ts
953
+ import { defineRoute } from '@drupflare/spool/server';
954
+
955
+ export const stock = defineRoute('/stock/:sku', {
956
+ strategy: 'template',
957
+ template: { source: '<p>{{sku}}: {{count}}</p>' },
958
+ depends: (ctx) => [`stock:${ctx.params['sku']}`],
959
+ load: (ctx) => ({ sku: ctx.params['sku'], count: 3 }),
960
+ freshness: { maxStale: 60, mode: 'hard' },
961
+ offline: 'refuse'
962
+ });
963
+
964
+ export const notice = defineRoute('/notices/:id', {
965
+ strategy: 'template',
966
+ template: { source: '<p>{{text}}</p>' },
967
+ depends: (ctx) => [`notice:${ctx.params['id']}`],
968
+ load: () => ({ text: 'Closed on Friday' }),
969
+ freshness: { maxStale: 3600, mode: 'soft' },
970
+ sync: 'deferred'
971
+ });
972
+ ```
973
+
974
+ The worker syncs the smallest set of changed dependencies the next page needs. One worker
975
+ leads the sync for every tab of an origin. A response to a request that happens anyway can carry
976
+ invalidations, so a sync does not always need a request of its own.
977
+
978
+ ---
979
+
980
+ ## ๐Ÿ” Authenticated Data
981
+
982
+ Set `scope: 'principal'` on a route that depends on who is signed in, and give `createSpool` an
983
+ `identity` option. The server sends `Spool-Partition`, an HMAC of the principal under your secret,
984
+ so the browser can tell users apart without learning who they are. The worker keeps each partition
985
+ in its own IndexedDB database with the artifacts, records, projections and sync state sealed with
986
+ AES-GCM. Each value is bound to its partition, kind and id, so a row copied into another slot does
987
+ not decrypt.
988
+
989
+ `retention` picks the key. `persistent` is a non-extractable key in IndexedDB that survives a
990
+ restart and is deleted on logout or a change of partition. `session`, the default, is a key held in
991
+ the worker's memory, which the browser discards when it stops the worker. Deleting a key makes what
992
+ it sealed unreadable, which is crypto-shredding of data at rest. It does not protect against script
993
+ running on your origin, which can ask the worker for whatever the page can. The ids and versions
994
+ that serve as storage keys stay readable, so use opaque ids for anything sensitive.
995
+
996
+ A principal route defaults to a five-minute hard bound and `offline: 'refuse'`. Revocation rides
997
+ the same bound: tag a page with the permission it needs, call `spool.invalidate` when the
998
+ permission goes, and the first required sync past the bound finds the page invalid.
999
+
1000
+ A different `Spool-Partition` wipes the partition before it. A login or account-switch handler has
1001
+ to send the new value itself with `spool.partition(principal)`, because the request it answers
1002
+ still carries the old identity. A sync answer carries `Spool-Partition` for the request's identity
1003
+ too, so a sign-in the worker never saw ends at the first required sync, no later than the route's
1004
+ `maxStale` plus the next view. Paths in `logout` are wiped when the request reaches the worker,
1005
+ before it is sent, so logout empties every tab even with the network down. The worker first sends
1006
+ the same-origin posts it holds in `beacons`, while they still carry the session that is ending,
1007
+ and drops any that do not get through. The page runtime
1008
+ dispatches a `spool:cleared` event on `window` in every tab when that happens, so a page can blank
1009
+ what it shows. A response can also send
1010
+ `Spool-Clear`: `*` wipes everything, a partition id wipes one. `decorate(request, response, { clear:
1011
+ ['*'], hardReset: true })` adds `Clear-Site-Data: "storage"` as well, the browser's own reset
1012
+ of everything the origin holds, service worker included. Its Safari support is unverified, so rely
1013
+ on the worker's wipe.
1014
+
1015
+ ```ts
1016
+ import { createSpool, defineRoute } from '@drupflare/spool/server';
1017
+ import { memoryStore } from '@drupflare/spool/stores';
1018
+
1019
+ export const grades = defineRoute('/grades', {
1020
+ strategy: 'template',
1021
+ scope: 'principal',
1022
+ retention: 'persistent',
1023
+ template: { source: '<h1>Grades for {{owner}}</h1>' },
1024
+ depends: (ctx) => [`perm:${ctx.identity?.principal}`, `grades:${ctx.identity?.principal}`],
1025
+ load: (ctx) => ({ owner: ctx.identity?.principal })
1026
+ });
1027
+
1028
+ export const spool = createSpool({
1029
+ routes: [grades],
1030
+ store: memoryStore(),
1031
+ identity: {
1032
+ secret: 'a long random value kept on the server',
1033
+ resolve: (request) => {
1034
+ const user = /user=(\w+)/.exec(request.headers.get('cookie') ?? '')?.[1];
1035
+ return user ? { principal: user } : null;
1036
+ }
1037
+ },
1038
+ logout: ['/logout']
1039
+ });
1040
+
1041
+ export async function login(user: string): Promise<Response> {
1042
+ return new Response('Signed in', {
1043
+ headers: {
1044
+ 'set-cookie': `user=${user}; Path=/`,
1045
+ 'spool-partition': await spool.partition(user)
1046
+ }
1047
+ });
1048
+ }
1049
+ ```
1050
+
1051
+ A sync that asks for everything changed lists the tags of every change, so tags for principal data
1052
+ should be opaque too. The worker syncs a principal page with `need`, which names only the tags
1053
+ that page reads.
1054
+
1055
+ ---
1056
+
1057
+ ## ๐Ÿ“š Catalogue
1058
+
1059
+ A catalogue sends the data of pages a visitor may open next, so that opening one makes no
1060
+ request. An entry costs its bytes whether or not the page is opened, and an opened one saves a
1061
+ document request, so an entry pays only when enough visitors open it. The break-even share is
1062
+ `k * E / (k * D + o)`, where `E` is the entry's compressed bytes, `D` the bytes of the document
1063
+ it replaces, `k` the joules to move a byte and `o` the origin request a view avoids. An entry the
1064
+ size of the document needs 96% of visitors to open it, which no page gets. An entry that is only
1065
+ the data for a template the device already holds is a few hundred bytes to a few kilobytes and
1066
+ pays at a few percent. On the `payload` reading, the rendered fields of a node were a median 473
1067
+ bytes compressed on 19 pages from two sites (L1, `measurements/payload`), against a document of
1068
+ about 17,000 bytes. References such as media and terms are left out of that figure, so a real entry
1069
+ is larger. An entry is data for a `template`, `projection` or `data` route and never HTML.
1070
+
1071
+ The site supplies the candidates. A route's `catalogue` returns the pages a visitor on it may open
1072
+ next, each with the share expected to. Take the share from your own `nextShare` over the sink
1073
+ (each `spool.view` names the route the visitor came from), from link position, or from a fixed
1074
+ prior. `documentBytes` on the entry's route says what a hit saves, and the provisioning prior is
1075
+ used when it is absent.
1076
+
1077
+ ```ts
1078
+ import { createSpool, defineRoute } from '@drupflare/spool/server';
1079
+ import { memoryStore } from '@drupflare/spool/stores';
1080
+
1081
+ export const home = defineRoute('/', {
1082
+ strategy: 'template',
1083
+ template: { source: '<h1>Courses</h1>' },
1084
+ load: () => ({}),
1085
+ catalogue: () => [
1086
+ { path: '/courses/101', p: 0.3 },
1087
+ { path: '/courses/102', p: 0.1 }
1088
+ ]
1089
+ });
1090
+
1091
+ export const course = defineRoute('/courses/:id', {
1092
+ strategy: 'template',
1093
+ template: { source: '<h1>{{title}}</h1>' },
1094
+ depends: (ctx) => [`course:${ctx.params['id']}`],
1095
+ load: (ctx) => ({ title: `Course ${ctx.params['id']}` }),
1096
+ documentBytes: 17_000
1097
+ });
1098
+
1099
+ export const spool = createSpool({
1100
+ routes: [home, course],
1101
+ store: memoryStore(),
1102
+ catalogueBytes: 16_384
1103
+ });
1104
+ ```
1105
+
1106
+ The server sends an entry only when its share reaches its own break-even, best expected net first,
1107
+ until `catalogueBytes` is spent. The cap counts compressed bytes and defaults to 32 KiB. It builds
1108
+ at most 32 pages for one request, measures each entry with Brotli where the runtime has it and
1109
+ gzip where it does not, and skips a `static` or `assembly` route. A request with `Save-Data: on`
1110
+ gets no entries.
1111
+
1112
+ The ask rides the beacon batch the worker already sends. A due ask also makes the next network
1113
+ moment send that batch, when a view is held to carry it, so a visit of two or three pages gets its
1114
+ entries without waiting for 20 views or an hour. That costs one `POST /.spool/beacon` for each
1115
+ catalogue page, device and day, sent at a request that happens anyway. A local view never sends
1116
+ it, and a moment with nothing held sends nothing. The worker stores the entries like provisioned
1117
+ pages, and the first view of an unopened one is reported with the reason `catalogue`. `catalogueFigures` reads the views and the worker's ledger for the entries sent
1118
+ and used, the hit share, and the bytes sent against the bytes saved. Until shadow views measure
1119
+ the shares, the saving is a scenario.
1120
+
1121
+ A hit saves the document request and nothing else. On the Drupal pages measured (75 first views of
1122
+ internal pages on 10 sites, L1, `measurements/trim`), a view of a page still fetched that page's
1123
+ new images and stylesheet and script aggregates: only 3% of those first views moved nothing but
1124
+ the document. A catalogue view takes the document off the wire, and the rest of the page is
1125
+ the site's own HTTP cache and [asset trimming](#-trimmed-stylesheets).
1126
+
1127
+ A `principal` route can be an entry. The worker then names every catalogue entry no page has
1128
+ opened at each sync of a principal page, so a revoked one is dropped at the first required sync.
1129
+ The request that asks for a catalogue names the path of the page the visitor is on, including one
1130
+ the worker showed locally, so a route that should not tell the server leaves `catalogue` off.
1131
+ [docs/security.md](docs/security.md#catalogue-entries) covers both.
1132
+
1133
+ ---
1134
+
1135
+ ## ๐Ÿ“ก Analytics Batching
1136
+
1137
+ A page that keeps analytics scripts posting to an endpoint makes a request on every view, which
1138
+ defeats a local view. List the same-origin POST paths in `beacons`. The worker answers each post
1139
+ with a `204` and queues it in IndexedDB. At the next network request that happens anyway it sends
1140
+ the post to the URL the page meant, with its method, content type, query and body, so the vendor
1141
+ receives what the page sent. Spool's own events go to `/.spool/beacon` in one batch, where a
1142
+ `sink` receives them. A held post goes out on its own at most once every 24 hours, when the oldest
1143
+ is a day old. A body over 64 KiB, counted in bytes, or a binary body goes to the network at once.
1144
+ Firefox does not route `fetch` calls made with `keepalive: true` through the worker, so a script
1145
+ that posts that way is not queued there.
1146
+
1147
+ ```ts
1148
+ import { createSpool, jsonLinesSink } from '@drupflare/spool/server';
1149
+ import { memoryStore } from '@drupflare/spool/stores';
1150
+ import { routes } from './routes';
1151
+
1152
+ export const spool = createSpool({
1153
+ routes,
1154
+ store: memoryStore(),
1155
+ beacons: ['/cdn-cgi/rum'],
1156
+ sink: jsonLinesSink((line) => console.log(line))
1157
+ });
1158
+ ```
1159
+
1160
+ `jsonLinesSink` writes one JSON line per event. `analyticsEngineSink` writes to a Workers
1161
+ Analytics Engine dataset, splitting a long body across data points so nothing is cut off, and
1162
+ `memorySink` keeps events for tests. The worker also queues one
1163
+ `spool.view` event per page view with the tier, the reason, the staleness and the route the
1164
+ visitor came from (a pattern or `external`, never a URL). `viewsOf` reads
1165
+ those events back. Set `views: false` to turn that off. `workSeries` records how long each route's
1166
+ handler took, for peak-work reports.
1167
+
1168
+ ### Collectors
1169
+
1170
+ A `collectors` entry holds requests to another origin, such as an analytics host. The page gets an
1171
+ empty answer at once and the request leaves at a network moment, inside `maxAgeMs`. An event past
1172
+ the cap is dropped, never sent late, and each drop is counted and written to the sink as
1173
+ `/.spool/dropped`. With `delivery: 'replay'` the worker sends the held request to the vendor with no
1174
+ cookies, so identity has to be in the payload, and `stamp` writes the event's time into a query
1175
+ parameter for a vendor that takes one. With `delivery: 'sink'` the server half forwards it.
1176
+
1177
+ GA4's browser collector takes no backdating. The Measurement Protocol accepts events with their
1178
+ own timestamps up to 72 hours old and wants them within 48 hours to attribute them, and it needs an
1179
+ API secret the page must not hold. `GA4_COLLECTOR` holds the `g/collect` hit for 48 hours and
1180
+ `ga4Sink` sends it as a Measurement Protocol request carrying the original `timestamp_micros`.
1181
+ `ga4Sink` checks its requests against the rules spool knows (`mpViolations`); set `debug: true` to
1182
+ send them to `/debug/mp/collect` with your own measurement id and secret. Spool holds events
1183
+ whether or not the visitor consented, so gate the analytics tag itself, and ask your counsel
1184
+ whether holding an event in IndexedDB needs consent where you operate. Programmatic ads are never
1185
+ held, replayed or answered locally.
1186
+
1187
+ ```ts
1188
+ import { GA4_COLLECTOR } from '@drupflare/spool/protocol';
1189
+ import { combineSinks, createSpool, ga4Sink, jsonLinesSink } from '@drupflare/spool/server';
1190
+ import { memoryStore } from '@drupflare/spool/stores';
1191
+ import { routes } from './routes';
1192
+
1193
+ export const spool = createSpool({
1194
+ routes,
1195
+ store: memoryStore(),
1196
+ collectors: [
1197
+ GA4_COLLECTOR,
1198
+ {
1199
+ vendor: 'matomo',
1200
+ match: 'https://stats.example.edu/matomo.php',
1201
+ maxAgeMs: 23 * 3_600_000,
1202
+ delivery: 'replay',
1203
+ stamp: { param: 'cdt', kind: 'datetime-utc' }
1204
+ }
1205
+ ],
1206
+ sink: combineSinks(
1207
+ jsonLinesSink((line) => console.log(line)),
1208
+ ga4Sink({ measurementId: 'G-XXXXXXXXXX', apiSecret: 'kept on the server' })
1209
+ )
1210
+ });
1211
+ ```
1212
+
1213
+ ---
1214
+
1215
+ ## ๐Ÿงท Third-Party Files
1216
+
1217
+ A view that loads a file from another origin is not zero-network. The page reports, when it is
1218
+ hidden, how many cross-origin resources it loaded and from which origins (origins only, never
1219
+ paths). `thirdPartyReport` reads those reports from `viewsOf` and ranks the origins by the views
1220
+ each one alone blocks, with a class and a remedy from a short table you can replace. The counts
1221
+ are an upper bound on network requests, since a cross-origin timing entry says nothing about cache
1222
+ hits without `Timing-Allow-Origin`.
1223
+
1224
+ The opt-in `thirdParty` cache removes the request for files that name their own bytes. List the
1225
+ origins in `allow`. The worker learns a file from the first view and keeps it only when the URL
1226
+ carries a hash, a version or a `/v12/` segment, or the origin marks it immutable. A file versioned
1227
+ only by a query parameter is checked again at a network moment once it is a day old. It keeps
1228
+ nothing else, and nothing it cannot read: an opaque response counts about 7 MB against quota in
1229
+ Chromium, so `allowOpaque` is off by default. WebKit does not send font requests from a stylesheet
1230
+ through the worker, so fonts stay on the network there.
1231
+
1232
+ ```ts
1233
+ import { thirdPartyReport } from '@drupflare/spool/report';
1234
+ import { createSpool, memorySink, viewsOf } from '@drupflare/spool/server';
1235
+ import { memoryStore } from '@drupflare/spool/stores';
1236
+ import { routes } from './routes';
1237
+
1238
+ const sink = memorySink();
1239
+
1240
+ export const spool = createSpool({
1241
+ routes,
1242
+ store: memoryStore(),
1243
+ sink,
1244
+ thirdParty: {
1245
+ allow: ['https://fonts.gstatic.com', 'https://cdn.example.edu'],
1246
+ maxEntries: 32
1247
+ }
1248
+ });
1249
+
1250
+ export const report = () => thirdPartyReport(viewsOf(sink.events));
1251
+ ```
1252
+
1253
+ ---
1254
+
1255
+ ## โšก Subresource Handling
1256
+
1257
+ By default the worker answers an immutable same-origin asset from CacheStorage. `assets` changes
1258
+ that. `passthrough` leaves assets to the browser's cache and never touches them. `static-routing`
1259
+ asks the browser through `InstallEvent.addRoutes` to send only navigations, beacons and routed
1260
+ paths to the worker and the rest of the origin to the network, and acts as `passthrough` where the
1261
+ API does not exist: Chromium has it, Playwright's WebKit has it on macOS and not on Linux, and
1262
+ Firefox does not. Both options cut the service worker's fetch events (Static Routing took them from
1263
+ 32 to 3 per view on a desktop Chromium rig); neither changes server-side Worker invocations.
1264
+ Neither is measured to save energy. In the same run, new-tab views drew 2.78 J,
1265
+ 2.83 J and 2.84 J of RAPL package energy above idle with CacheStorage, navigation-only handling
1266
+ and Static Routing (8 rounds each), and the ranges overlap. Under Static Routing a same-origin write no
1267
+ longer reaches the worker, so it is not a network moment and its response cannot carry
1268
+ invalidations; navigations still are.
1269
+
1270
+ ```ts
1271
+ import { createSpool } from '@drupflare/spool/server';
1272
+ import { memoryStore } from '@drupflare/spool/stores';
1273
+ import { routes } from './routes';
1274
+
1275
+ export const spool = createSpool({
1276
+ routes,
1277
+ store: memoryStore(),
1278
+ assets: 'static-routing'
1279
+ });
1280
+ ```
1281
+
1282
+ ---
1283
+
1284
+ ## ๐Ÿ” Shadow Mode
1285
+
1286
+ In shadow mode the worker decides whether it could serve a page locally, renders it, still fetches
1287
+ the page from the server, compares the two with `normalise` and reports the result. It never
1288
+ serves the local page. Use it to measure eligibility and mismatches on real traffic before turning
1289
+ serving on. A `static` route has no shadow comparison.
1290
+
1291
+ ```ts
1292
+ import { createSpool } from '@drupflare/spool/server';
1293
+ import { memoryStore } from '@drupflare/spool/stores';
1294
+ import { routes } from './routes';
1295
+
1296
+ export const shadowed = createSpool({ routes, store: memoryStore(), mode: 'shadow' });
1297
+ export const canary = createSpool({ routes, store: memoryStore(), shadowSample: 0.05 });
1298
+ ```
1299
+
1300
+ `mode: 'shadow'` applies to every route that does not name its own `mode`. `shadowSample` keeps a
1301
+ share of views in shadow while the rest are served, which gives a standing canary. A mismatch
1302
+ names its route, so a route whose template and application disagree shows up before a visitor
1303
+ sees it. The worker records the shadow comparison for navigations only. Subresources and other
1304
+ requests keep the browser's own path.
1305
+
1306
+ ---
1307
+
1308
+ ## ๐Ÿ”Œ Proxy Mode
1309
+
1310
+ `spoolProxy` fronts an origin that cannot load the library, such as PHP, Drupal with no module,
1311
+ or anything else that speaks HTTP. It returns a Fetch handler. The proxy answers `/.spool/*` and
1312
+ `/spool-sw.js`. Every other request goes to the origin with its method, headers and body, and a
1313
+ redirect is passed on, not followed. A page the proxy may keep is decorated like a `createSpool`
1314
+ response, so the worker stores it and answers later views with no request.
1315
+
1316
+ `spool` takes the options `createSpool` takes, minus `routes`. The proxy adds one public
1317
+ `assembly` route that matches every path (`/**`) and uses the origin's finished HTML as the
1318
+ artifact. `routes` lists overrides, such as a shorter freshness bound for one pattern. They
1319
+ match first and take the same fields as `defineRoute`, without `load`.
1320
+
1321
+ On Node the handler goes through `toNodeListener`. `origin` is the address of the real server
1322
+ and has no path.
1323
+
1324
+ ```ts
1325
+ import { createServer } from 'node:http';
1326
+ import { toNodeListener } from '@drupflare/spool/node';
1327
+ import { spoolProxy } from '@drupflare/spool/proxy';
1328
+ import { memoryStore } from '@drupflare/spool/stores';
1329
+
1330
+ const proxy = spoolProxy({
1331
+ origin: process.env['ORIGIN'] as string,
1332
+ spool: { store: memoryStore() },
1333
+ purgeSecret: process.env['PURGE_SECRET']
1334
+ });
1335
+
1336
+ export const server = createServer(toNodeListener(proxy));
1337
+ ```
1338
+
1339
+ On Workers the proxy is built once per isolate and keeps the version vector in a Durable Object,
1340
+ bound as in [Cloudflare Workers](#cloudflare-workers). Set the purge secret with
1341
+ `wrangler secret put PURGE_SECRET`.
1342
+
1343
+ ```ts
1344
+ import { spoolProxy, type SpoolProxy } from '@drupflare/spool/proxy';
1345
+ import { durableObjectStore } from '@drupflare/spool/stores/durable-object';
1346
+
1347
+ export { SpoolVersions } from '@drupflare/spool/stores/durable-object';
1348
+
1349
+ let proxy: SpoolProxy | undefined;
1350
+
1351
+ export default {
1352
+ fetch(request: Request, env: Cloudflare.Env) {
1353
+ proxy ??= spoolProxy({
1354
+ origin: 'https://origin.example.edu',
1355
+ spool: { store: durableObjectStore(env.SPOOL_VERSIONS) },
1356
+ purgeSecret: env.PURGE_SECRET
1357
+ });
1358
+ return proxy(request);
1359
+ }
1360
+ };
1361
+ ```
1362
+
1363
+ ### Tags
1364
+
1365
+ The tags the origin sends become the page's dependencies. The proxy reads `Cache-Tag` (comma
1366
+ separated), `Surrogate-Key` and `X-Drupal-Cache-Tags` (space separated) and `xkey` (spaces or
1367
+ commas), then removes them from the response unless `exposeTags` is true. A page with no tags
1368
+ depends on one tag derived from its path.
1369
+
1370
+ ### Eligibility
1371
+
1372
+ The proxy keeps a response only when all of these hold:
1373
+
1374
+ - the request is a GET or HEAD, has no query string and no `Authorization` header, and carries
1375
+ no cookie outside `publicCookies`, which is empty by default
1376
+ - the response is a 200 `text/html`, or the answer of a `data` route
1377
+ - the page is UTF-8, or declares no charset in its `Content-Type` or in a `<meta>` within its
1378
+ first 1024 bytes
1379
+ - the response sets no cookie outside `publicCookies`, has no `private` or `no-store` in
1380
+ `Cache-Control`, a `Vary` that names only `Accept-Encoding` and `Cookie`, no more than
1381
+ `maxTags` tags and no more than `maxBytes` bytes
1382
+
1383
+ Everything else is forwarded untouched. A page the proxy declines tells the worker to drop its
1384
+ copy of that path, and a response that sets a cookie outside `publicCookies` tells the worker to
1385
+ clear everything it holds, so a login leaves no anonymous copy to show. Put cookies that carry no
1386
+ identity on the list, such as a bot-check or load-balancer cookie set on every response; otherwise
1387
+ proxy mode never keeps a page on that site. Writes always go to the origin and are never
1388
+ answered locally.
1389
+
1390
+ Principal routes are off unless `spool.identity` is set. With it, a route declared
1391
+ `scope: 'principal'` follows the partitions in [Authenticated Data](#-authenticated-data). The
1392
+ cookie, `Authorization` and `private` rules do not apply to it. `Set-Cookie` and `no-store`
1393
+ still do.
1394
+
1395
+ ### Invalidation
1396
+
1397
+ Two signals reach the proxy. When a page it forwards differs from the last body it saw, it bumps
1398
+ that page's tags. The proxy hashes the body after `normalise`, and `ignore` takes patterns to
1399
+ blank, such as a per-render form token. `POST /.spool/purge` bumps the tags it is given, so a CMS
1400
+ hook or Drupal's purge module can call it. It needs `Authorization: Bearer <purgeSecret>` and a
1401
+ JSON body, and it refuses every call when no secret is set.
1402
+
1403
+ ```sh
1404
+ curl -X POST https://example.edu/.spool/purge \
1405
+ -H "Authorization: Bearer $PURGE_SECRET" \
1406
+ -H "Content-Type: application/json" \
1407
+ -d '{"tags": ["node:1", "config:system.menu.main"]}'
1408
+ ```
1409
+
1410
+ ### Limits
1411
+
1412
+ The proxy sees finished HTML, not the database behind it. An artifact is a whole page, the
1413
+ replica holds no records, and two pages never share a fragment. A page stays on a device until its
1414
+ route's `maxStale` passes and the next sync reports a change, so freshness is bounded by
1415
+ `maxStale` plus the time a purge takes to arrive.
1416
+
1417
+ A tag shared by many pages invalidates all of them when one changes. The remembered bodies live
1418
+ in the instance: a restart forgets them, and each instance of a scaled-out proxy keeps its own.
1419
+ A whole page is a large artifact, and the provisioning rule declines one that costs more than the
1420
+ priors say it saves, so set `provision` on the spool for large pages. A `Content-Security-Policy`
1421
+ that allows only nonce or hash scripts blocks the injected client tag. A URL with a query string
1422
+ always goes to the origin. [Trimmed Stylesheets](#-trimmed-stylesheets) covers the `trim` option.
1423
+
1424
+ ---
1425
+
1426
+ ## ๐Ÿงน Trimmed Stylesheets
1427
+
1428
+ A new page of a Drupal site usually loads a stylesheet aggregate the visitor has not seen, and
1429
+ most of its rules match nothing on the page. `@drupflare/spool/trim` builds a manifest that removes
1430
+ the rules no page of the site can match, and `spoolProxy` serves the trimmed text in place of the
1431
+ origin's file. Each file is trimmed against every page and script of the site, never against one
1432
+ page, so it keeps its URL and its one cached copy. Rules are only removed, so the cascade of what
1433
+ stays is unchanged.
1434
+
1435
+ ```ts
1436
+ import { spoolProxy } from '@drupflare/spool/proxy';
1437
+ import { memoryStore } from '@drupflare/spool/stores';
1438
+ import { buildTrimManifest } from '@drupflare/spool/trim';
1439
+
1440
+ export const manifest = await buildTrimManifest({
1441
+ pages: ['<body class="hero"><a class="button">Apply</a></body>'],
1442
+ scripts: [{ url: '/js/site.js', text: 'menu.classList.add("open")' }],
1443
+ styles: [{ url: '/css/site.css', text: '.hero{a:b}.button{c:d}.open{e:f}.print-only{g:h}' }]
1444
+ });
1445
+
1446
+ export const proxy = spoolProxy({
1447
+ origin: 'https://origin.example.edu',
1448
+ spool: { store: memoryStore() },
1449
+ trim: manifest
1450
+ });
1451
+ ```
1452
+
1453
+ `pages` is HTML, `scripts` and `styles` are the same-origin files the pages load, and `url` is the
1454
+ address the page links. Crawl with a browser and pass the page's DOM after load and after a
1455
+ scroll as well as the origin's HTML: a class that a script builds from parts, such as
1456
+ `'fs-carousel' + '-enabled'`, is only in the rendered page. Pass the scripts of other origins that
1457
+ the pages load as `foreignScripts`, since a widget that builds markup adds classes too.
1458
+
1459
+ Two checks protect a visitor. The proxy serves a trimmed file only when the origin's bytes still
1460
+ hash to the manifest's `source`, and otherwise sends the origin's file and counts a mismatch. It
1461
+ also reads every HTML page before it leaves: a page with a token the trim removed a rule for, or a
1462
+ same-origin script the manifest was not built with, links the full file through `spool-trim=0`.
1463
+ `proxy.trim` counts the files served, the mismatches and the guard firings, and the sink gets
1464
+ each mismatch and firing as an event. [docs/security.md](docs/security.md#trimmed-stylesheets) names
1465
+ what the guard cannot see: a class a vendor script adds after the crawl, markup injected after load
1466
+ from a page the crawl never requested, and a class name built at runtime that no crawl rendered.
1467
+
1468
+ The proxy trims CSS and leaves JavaScript alone. Coverage at load cannot see what a click or a
1469
+ scroll will need: the used share of same-origin JavaScript rose from 36% at load to 40% after one
1470
+ scroll on the pages measured (L1), so no load-time figure says which of the rest is dead. The same
1471
+ saving from a build step that runs PurgeCSS with a safelist needs no proxy. The proxy gets it
1472
+ without a change to the origin. The saving applies to a network view of a page the visitor has not
1473
+ seen, never to a repeat or local view, and it is not part of the per-view headline;
1474
+ [docs/impact.md](docs/impact.md#asset-trimming) has the bytes, joules and prices.
1475
+
1476
+ ---
1477
+
1478
+ ## ๐Ÿšซ Out of Scope
1479
+
1480
+ - spool never authorises on the client. The device holds only what the server already sent that
1481
+ principal.
1482
+ - It never shows a hard route past its bound without validating it first.
1483
+ - It never writes. Your application owns every write, and `invalidate` only announces one.
1484
+ - It never infers dependencies from opaque HTML. A route names its tags, or the records it
1485
+ reads, and in proxy mode the origin names them in its cache tag headers.
1486
+ - It never replaces a published release file on the CDN.
1487
+
1488
+ ---
1489
+
1490
+ ## ๐ŸŒ Browser Support
1491
+
1492
+ | Engine | Controlled navigation |
1493
+ | -------- | ------------------------------------------------------------------------------------------------------------------ |
1494
+ | Chromium | no request, new tabs included |
1495
+ | Firefox | no request, new tabs included |
1496
+ | WebKit | one request to re-check `/spool-sw.js`, and one refetch of each cacheable subresource on the first controlled load |
1497
+
1498
+ The worker registers with `updateViaCache: 'all'` and the script is served at
1499
+ `max-age=86400`, which is what lets Chromium and Firefox skip the update check. A deploy changes
1500
+ the script bytes and reaches a browser within that day. A browser without service workers gets the
1501
+ page it always got.
1502
+
1503
+ A page that loads a resource from another origin is not a zero-request view. spool counts those
1504
+ loads per view and reports the view as such.
1505
+
1506
+ ---
1507
+
1508
+ ## ๐Ÿงญ Subpath Exports
1509
+
1510
+ | Import | What is in it |
1511
+ | ---------------------------------------- | -------------------------------------------------------------------- |
1512
+ | `@drupflare/spool` | the protocol and report modules |
1513
+ | `@drupflare/spool/protocol` | envelopes, artifacts, patterns, templates, the manifest, view events |
1514
+ | `@drupflare/spool/server` | `createSpool`, `defineRoute`, sinks, identity and work series |
1515
+ | `@drupflare/spool/node` | `toNodeListener`, `toRequest`, `writeResponse` |
1516
+ | `@drupflare/spool/proxy` | `spoolProxy`, the tag readers and the eligibility verdict |
1517
+ | `@drupflare/spool/stores` | `memoryStore` and the `VersionStore` type |
1518
+ | `@drupflare/spool/stores/durable-object` | `durableObjectStore` and the `SpoolVersions` class |
1519
+ | `@drupflare/spool/sw` | `serve`, for a site that bundles its own worker |
1520
+ | `@drupflare/spool/client` | `register` and the page handle |
1521
+ | `@drupflare/spool/report` | the energy model |
1522
+ | `@drupflare/spool/trim` | `buildTrimManifest`, `trimCss` and `guardPage` |
1523
+
1524
+ ---
1525
+
1526
+ ## ๐Ÿงช Testing
1527
+
1528
+ ```sh
1529
+ bun run typecheck
1530
+ bun run test
1531
+ bun run test:coverage
1532
+ bun run test:browser
1533
+ bun run prettier:check
1534
+ bun run docs:build
1535
+ bun run build
1536
+ ```
1537
+
1538
+ `test` runs the unit project in workerd and the node project, which includes a test that packs the
1539
+ package and imports every subpath in plain Node. `test:browser` runs Chromium, Firefox and WebKit
1540
+ against real servers and counts the requests that reach them. `bunx playwright install chromium
1541
+ firefox webkit` fetches the browsers.
1542
+
1543
+ ---
1544
+
1545
+ ## ๐Ÿ”— Related Repositories
1546
+
1547
+ | Repository | What it is |
1548
+ | --------------------------------------------------------------- | -------------------------------------------------------- |
1549
+ | [`drupflare/worker`](https://github.com/drupflare/worker) | Drupal on Cloudflare Workers |
1550
+ | [`drupflare/cartridge`](https://github.com/drupflare/cartridge) | runs a blocking wasm interpreter inside a Durable Object |
1551
+ | [`drupflare/durabledb`](https://github.com/drupflare/durabledb) | the limits of Durable Object SQLite, encoded as a codec |
1552
+ | [`drupflare/burrow`](https://github.com/drupflare/burrow) | executes WebAssembly on Workers |
1553
+
1554
+ ---
1555
+
1556
+ ## ๐Ÿ“„ License
1557
+
1558
+ MIT (c) Gregory Mitchell 2026. See [LICENSE](LICENSE).