svelte-hono

API reference

v0.1.0 โ€” minimal surface. Three exports. That's the API.

svelteRenderer(component, options)

Returns a Hono MiddlewareHandler that responds with an HTML document containing the SSR'd Svelte component, followed by a hydration script that mounts the matching client bundle.

OptionTypeRequiredNotes
hydrateAsstringyesMatches a key in the bundles map passed to attachSvelteRoutes.
propsRecord<string, unknown>noSame props used by SSR and hydration. JSON-serialized into the page.
titlestringnoSets <title>.
headstringnoExtra raw <head> HTML.
bodyClassstringnoClass on the <body>.
cacheControlstring | falsenoCache-Control for the HTML. Defaults to "public, max-age=0, must-revalidate" โ€” deploys are visible immediately. Pass "public, max-age=60, s-maxage=300" to let Cloudflare cache the HTML. Pass false to omit the header. See Caching.

attachSvelteRoutes(app, options)

Registers GET /__svelte/:id.{hash}.{js,css} routes on the Hono app (legacy unhashed :id.{js,css} also accepted, with weaker caching). Call once per app instance. Idempotent.

OptionTypeNotes
bundlesRecord<string, ClientBundle>Keyed by hydrateAs. The build helper produces this. Each ClientBundle is { js, css, hash? }; when hash is set, URLs are content-addressed and served with cache-control: immutable.
prefixstringDefaults to "/__svelte". Override only on collision.

buildHonoSvelte(options)

From svelte-hono/build. Three esbuild passes โ€” a shared Svelte runtime chunk, one per-component client chunk, and one server pass โ€” emitting a single bundled worker with all client bundles inlined as build-time string constants. Every bundle is content-hashed so deploys never collide with browser/edge caches.

OptionTypeNotes
workerEntrystringPath to your Worker entry (TS or JS).
outDirstringOutput dir. worker.bundled.mjs lands here.
componentsRecord<string, string>id โ†’ path to .svelte file. id matches hydrateAs.
targetstringDefaults "es2022".
devbooleanSource maps + no minify. Defaults false.
bundlesFilestringFilename of the generated bundles module, relative to the worker entry's directory. Defaults to "bundles.generated.ts".
skipWorkerBundlebooleanOnly build client bundles + write bundles.generated.ts; skip the final worker bundle. Useful when another tool (e.g. wrangler) bundles the worker.

The generated bundles import

buildHonoSvelte() writes bundles.generated.ts next to your worker entry. Import it like any other module:

import { bundles } from "./bundles.generated";
attachSvelteRoutes(app, { bundles });

Add it to .gitignore โ€” it's regenerated each build. No globals, no declare const, no esbuild define magic to learn.

Mounting on a custom DOM node

The generated hydrate function accepts an optional second argument:

hydrate(props, target?)

When omitted, it mounts on #svelte-hono-root (the default SSR target). Pass a different element to embed multiple components on one page โ€” for example, mounting Svelte islands into Hono JSX output.

Constraints