Start
Getting Started
This page walks through a first working site: install the package or the script, declare one route, wire it into the server, run it, and check that the device answers the page locally. The README Quick Start gives the same three moves in less space. The Adapters index has one page per framework once the first route works.
Choose How the Runtime Arrives
The page runtime reaches a browser in one of three ways. Pick one before you wire the server.
-
Package. Install
@drupflare/spooland import it from your server code. Node, Deno, Bun, Workers and bundlers import the package directly. This is the path the examples use.bun add @drupflare/spool -
CDN script. Load the runtime from
cdn.drupflare.comwith a Subresource Integrity hash. The release notes list the hash for each version, andcreateSpoolinjects the same tag when it decorates a page.<script
src="https://cdn.drupflare.com/spool/<version>/client.js"
integrity="sha384-<hash from the release notes>"
crossorigin="anonymous"
defer
data-spool-client
></script> -
Direct files.
spool-sw.js,client.jsandmanifest.jsonare attached to every GitHub release and published underhttps://cdn.drupflare.com/spool/<version>/. A site that serves them as plain files needs no server library. Static Sites covers that case.
This page follows the package path.
Declare a Route
A route has a path pattern, a strategy, the tags it depends on and a load function that returns
the data for one page. load runs on the server and may check the visitor's identity. Only its
result reaches the device.
Save this as routes.ts. The template renders {{id}}, so the page the application returns has to
match it:
import { defineRoute } from '@drupflare/spool/server';
export const routes = [
defineRoute('/node/:id', {
strategy: 'template',
template: {
source:
'<!doctype html><html><head><title>Node {{id}}</title></head><body><h1>Node {{id}}</h1></body></html>'
},
depends: (ctx) => [`node:${ctx.params['id']}`],
load: (ctx) => ({ id: ctx.params['id'] })
})
];
const escape = (text: string) => text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
export const page = (request: Request) => {
const id = escape(new URL(request.url).pathname.split('/')[2] ?? '');
return new Response(
`<!doctype html><html><head><title>Node ${id}</title></head><body><h1>Node ${id}</h1></body></html>`,
{ headers: { 'content-type': 'text/html; charset=utf-8' } }
);
};
page stands in for your application. The device renders the route from its template and its
data without asking the server, so the two have to agree. If they drift, the shadow mode described
in Advanced Usage reports the mismatch before a visitor sees it.
Keep the Version Store
The version store records which tags changed. memoryStore serves a single process, and it is the
right choice for a first run. Every other deployment needs a store that all instances share. Any
object with three methods works: bump(tags), cursor() and diff(from). On Cloudflare,
durableObjectStore keeps the vector in a Durable Object, as the
Cloudflare Workers page shows.
import { memoryStore } from '@drupflare/spool/stores';
export const store = memoryStore();
Create the Spool
createSpool joins the routes and the store into one instance. Every adapter calls the same
methods on it: handle for a Fetch handler, and respond, decorate and stub for frameworks
that take the pieces one at a time.
import { createSpool } from '@drupflare/spool/server';
import { routes } from './routes';
import { store } from './store';
export const spool = createSpool({ routes, store });
When a write changes data, call spool.invalidate with the tags it touched. Devices that hold a
page depending on those tags fetch a fresh copy before they show it again:
await spool.invalidate(['node:1']);
Run It
The fastest route to a running site is the Node example. Its server is a node:http listener, so
it needs no framework. Install the example's dependencies in its folder first, as
examples/README.md describes, then run it:
cd examples/courses/node
node server.ts
Use the port the server listens on in the checks below, and open /node/1 in a browser.
Any other runtime follows the same shape. The Adapters index lists the import path and the example folder for each one.
Check That It Works
Three checks show the setup is correct. Run them against the running server.
-
The worker script is served. The runtime needs
/spool-sw.js, which answers with a 200 status:curl -i http://localhost: < port > /spool-sw.js -
The page carries the gate. The HTML the application returns for
/node/1contains thedata-spool-gateelement. Its absence meanshandleordecoratedid not run on that response:curl -s http://localhost: < port > /node/1 | grep -c data-spool-gate -
The envelope is set. The response carries the
Spool-Artifactheader, which describes the stored copy of the page:curl -sI http://localhost: < port > /node/1 | grep -i spool-artifact
If the first check fails, the server does not answer /spool-sw.js. If the second or third fails,
the page did not pass through spool.handle or spool.decorate.
To see a local view, open the page in a browser and wait for the worker to install. Later loads of the page then render from the device.
Know What Stays on the Network
A device answers a page locally only when the route is public, the stored copy is fresh under the route's freshness contract, and the URL has no query string the route does not declare. Everything else goes to the server as it always did. The defaults and the rules for each are in Freshness Contracts and Query Routes.
Writes go to the server. Queued writes are a separate feature, off by default, that a route declares. See Declared Writes.
What to Read Next
- Configuration: the settings a first deployment decides, with defaults and recommended values.
- Adapters: the page for your framework, with its import path and example.
- Advanced Usage: the route fields, freshness, datasets, authenticated data, declared writes, analytics, proxy mode and the reports.
- Wire Protocol: the paths, headers and JSON bodies, for anyone who writes an adapter or a second server.
- Security: the threat model, the controls and the limits of local answers.