SvelteKit
Run withBetterSupabase as a SvelteKit handle hook, with the caller's repositories on event.locals.
toSvelteKit(entries) from better-supabase/sveltekit returns a handle
hook. Every key withBetterSupabase contributes
lands on event.locals, and the response resolve(event) renders passes back
through the entries, so refreshed session cookies reach the browser:
import { createServer, withBetterSupabase } from "better-supabase/server";
import { toSvelteKit } from "better-supabase/sveltekit";
import { betterSupabase } from "$lib/supabase";
const bsb = createServer(betterSupabase);
export const handle = toSvelteKit([
withBetterSupabase(bsb, { refresh: true, allow: ["user", "none"] }),
]);Loads, actions and endpoints read the caller's repositories from locals:
export const load = async ({ locals }) => ({
notes: await locals.db.notes.list({ select: ["id", "title"] }).orThrow(),
});Declare the keys once in App.Locals:
import type { BetterSupabaseContributions } from "better-supabase/server";
import type { Functions, Models } from "$lib/supabase";
declare global {
namespace App {
interface Locals extends Partial<
BetterSupabaseContributions<Models, Functions, unknown>
> {}
}
}On Cloudflare, event.platform.env seeds the pipeline, so getEnv inside the
entries reads the bindings. Compose the hook with others through SvelteKit's
sequence. The bridge makes the request body readable twice, so an entry and
a form action both see it.
better-supabase init detects SvelteKit from @sveltejs/kit, sets gen.hooks to sveltekit and writes src/hooks.server.ts with the handle hook above, unless the file exists.
Session snapshot
The helpers from createSvelteKit(betterSupabase) share one verified session
per token across loads. bsb.require(event) returns snapshot next to the
context, and bsb.contextFromSession(snapshot) builds another context from
it with no second verification and no cookie read:
export const load = async (event) => {
const { snapshot } = await bsb.require(event);
const { db } = await bsb.contextFromSession(snapshot);
return { notes: await db.notes.list().orThrow() };
};A snapshot holds the access token: never return it in a response. A copy
of it (a spread, structuredClone, a serialized loader result) gets the
invalid context, so pass plain values such as the user id onward instead.
Prefetches refresh the session too. Parallel requests with the same refresh
token share one call to Auth, so they never race on it.
Remote functions
SvelteKit's remote functions (query, command and form from
$app/server) have no event argument. Pass getRequestEvent to
createSvelteKit and call bsb.caller() inside the function:
import { error, fail, redirect } from "@sveltejs/kit";
import { getRequestEvent, query } from "$app/server";
const bsb = createSvelteKit(betterSupabase, {
kit: { error, fail, redirect, getRequestEvent },
});
export const notes = query(async () => {
const { db } = await bsb.caller({ requireTenant: true });
return db.notes.list().orThrow();
});caller takes the same options as bsb.require. A refusal becomes
SvelteKit's error() and never a redirect, because a remote function answers
the client that called it. Call it before the first await where the runtime
has no AsyncLocalStorage, since getRequestEvent then works only
synchronously. Remote functions are experimental in SvelteKit; enable them
with kit.experimental.remoteFunctions.
Last updated on