Skip to main content
This page builds on the core Queries, Mutations & Actions API. Make sure setupBijection() is in your root layout before using these features — see Overview. Import from bijection-svelte/sveltekit for SvelteKit-specific features: SSR transport with live upgrade, and a server-side HTTP client helper.
Why bother with SSR on a realtime backend?The client will open a WebSocket and get live updates anyway — so is SSR worth it? Almost always yes: it’s faster for time-to-data on first page load. See Why server-side rendering with Bijection? for the full comparison.
bijectionLoad() and bijectionLoadPaginated() fetch data on the server and automatically upgrade to live subscriptions on the client. No manual initialData wiring needed. Use bijectionLoad() for regular queries and bijectionLoadPaginated() for paginated queries.

Setup

Add initBijection() and the transport hooks to hooks.ts (universal hooks — runs on both server and client). initBijection() creates the BijectionClient singleton early so the transport decoder can upgrade SSR data to live subscriptions. setupBijection() in your root layout automatically reuses this singleton. If you only use bijectionLoad(), you only need the BijectionLoadResult transport. Add BijectionLoadPaginatedResult when using bijectionLoadPaginated().
src/hooks.ts

Usage with bijectionLoad

src/routes/+page.ts
src/routes/+page.svelte
The result has the same shape as useQuery() — .data, .isLoading, .error, .isStale — and is reactive. On first load, data arrives via SSR (no loading flash). After hydration, a live WebSocket subscription takes over automatically.

Usage with bijectionLoadPaginated

bijectionLoadPaginated() works the same way but for paginated queries. It fetches the first page on the server and upgrades to a live paginated subscription on the client — with loadMore() support for incremental loading.
src/routes/+page.ts
src/routes/+page.svelte
The result has the same shape as usePaginatedQuery() — .results, .status, .isLoading, .error, .loadMore() — and is reactive. On first load, the first page arrives via SSR (no loading flash). After hydration, a live WebSocket subscription takes over and loadMore() becomes functional.

Authenticated fetches

For authenticated SSR fetches, use withServerBijectionToken in your server hook. This stores the auth token per-request via AsyncLocalStorage, so bijectionLoad and createBijectionHttpClient pick it up automatically — no { token } option needed.
src/hooks.server.ts
Then use bijectionLoad in any load function — +page.ts or +page.server.ts:
src/routes/+page.ts
The explicit { token } option still works as a manual override:
src/routes/+page.server.ts

Skipping queries

Pass 'skip' as args to avoid fetching — useful for auth-gated queries that should not run when the user is unauthenticated:
src/routes/+page.server.ts
When skipped, bijectionLoad returns { data: undefined, isLoading: false, error: undefined, isStale: false } without making any request. bijectionLoadPaginated returns { results: [], status: 'Exhausted', isLoading: false, error: undefined, loadMore: () => false }.

Choosing between +page.ts and +page.server.ts

bijectionLoad works in both universal (+page.ts) and server-only (+page.server.ts) load functions. The difference is what happens during client-side navigation (after the first SSR page load):
  • +page.ts (universal): On client-side navigation, bijectionLoad runs in the browser and queries Bijection directly — no server roundtrip. Auth is handled implicitly via the already-authenticated BijectionClient singleton (configured by setupAuth() in your root layout).
  • +page.server.ts (server-only): On client-side navigation, SvelteKit fetches from your server, which then queries Bijection — adding an extra network hop. Auth is always explicit (server-side via withServerBijectionToken or locals.token).
Both produce identical SSR on first page load. Use +page.ts for best navigation performance. Use +page.server.ts if you need access to server-only data (e.g. locals, cookies) or prefer explicit auth handling.

SSR with initialData (manual alternative)

If you prefer server-only load functions (+page.server.ts) or need more control, you can use the initialData option on useQuery() and usePaginatedQuery() directly.
src/routes/+page.server.ts
src/routes/+page.svelte
Combining initialData with keepPreviousData: true (or never changing the query arguments) should be enough to avoid ever seeing a loading state.
When to use this over bijectionLoadUse initialData when building a library that needs to support Svelte-only, SvelteKit SPA, and SvelteKit SSR without requiring the transport hook setup.

Server helpers

These are server-only helpers (hooks.server.ts, +page.server.ts, form actions, endpoints) for authenticating SSR fetches and running one-off calls from the server. For one-off calls from the client, use getBijectionClient() instead. Import from bijection-svelte/sveltekit/server. Wraps your SvelteKit resolve() call to store the auth token per-request via AsyncLocalStorage. Both bijectionLoad and createBijectionHttpClient automatically read it during SSR.
src/hooks.server.ts
src/app.d.ts
With this setup, bijectionLoad() and createBijectionHttpClient() automatically authenticate during SSR — no { token } option needed in load functions.

Setting up locals.token (without withServerBijectionToken)

If you prefer not to use withServerBijectionToken, you can still extract the token and pass it explicitly:
src/hooks.server.ts
Then pass { token: locals.token } to bijectionLoad or createBijectionHttpClient in each load function.

createBijectionHttpClient

For server-only code (+page.server.ts, form actions, API routes), use createBijectionHttpClient():
src/routes/+page.server.ts
Explicit token still works as an override:
src/routes/+page.server.ts
The url option falls back to the URL set by initBijection().

Deploying

See Deploy Your Frontend and bijection deploy for detailed instructions on deploying your app and Bijection functions to production. For the biggest SSR performance win, co-locate your framework server in the same region as Bijection — see Co-locate your server with Bijection.

API reference

Functions and types exported from bijection-svelte/sveltekit: The server-only helper withServerBijectionToken is imported from bijection-svelte/sveltekit/server.