@knitkit/edge assembles a complete page at the edge from independent HTML fragments — each owned by a different team, built with a different framework (or no framework at all). It fetches all fragments in parallel, streams them into the host template in document order, and injects a negotiated import map before </head> so any client-side modules hydrate correctly. Because every fragment renders its own HTML, there is no shared React instance to coordinate and no “invalid hook call” risk. The package runs wherever the Web Fetch and Streams APIs are available: Cloudflare Workers, Deno Deploy, Vercel Edge Functions, and Node.js 18+.
Install
Concepts
Fragment placeholders
You mark insertion points in your host HTML template with<knitkit-fragment> custom elements:
name attribute maps to a Fragment object in the fragments array you pass to composeStream.
The Fragment type
Step-by-step
API overview
composeStream(options)
ReadableStream<Uint8Array> that flushes template text immediately and streams each fragment in document order as its fetch resolves. Use this when you need direct control over the stream — for example, to pipe it into a Node http.ServerResponse.
composeResponse(options, init?)
composeStream in a Response with content-type: text/html; charset=utf-8. The typical return value from an edge handler. You can pass a second ResponseInit argument to add extra headers or override the status code.
compose(options)
parseTemplate(template)
{ type: "text", value } and { type: "fragment", name, fallback } segments. Exported for unit testing your templates in isolation — you can assert on the parse tree without needing a network.
serializeImportMap(map)
@knitkit/edge for cases where you manage the import-map tag yourself instead of passing importMap to composeStream.
Graceful degradation
When a fragment fetch fails — network error, non-2xx status, or a timeout you set viaAbortSignal in init — composeStream falls back to the placeholder’s inner HTML. The page still renders completely; only the affected section shows the fallback content. Provide a custom error handler to override the default fallback:
onError, the fallback is the placeholder’s inner HTML. If the placeholder is self-closing (no inner HTML), knitkit emits an HTML comment so you can see the failure in the page source without a visible UI error.
Real gateway example
The following is drawn fromexamples/edge-composition/gateway.mjs in the knitkit repo. It adapts the Web ReadableStream to Node’s http module for local development — on a real edge runtime you would return composeResponse directly.