🍡 mochi

SSR framework for Svelte 5 + Bun with islands-based selective hydration

On this page

Request context

Inside components and server-side helpers, import context values directly from mochi-framework:

import { url, params, cookies, locals } from 'mochi-framework';

Each export reads from the current request’s context on every property access, so you never thread values through props.

url

The current page URL as a standard URL object.

<!-- file: src/Post.svelte -->
<script>
  import { url } from 'mochi-framework';

  const page = Number(url.searchParams.get('page') ?? '1');
</script>

<p>Current path: {url.pathname}</p>

url is isomorphic. On the server it reads the parsed request URL. On the client it reflects the current browser URL, including after pushState / replaceState.

params

Route parameters matched by the Bun router. Server-only.

<!-- file: src/Post.svelte -->
<script>
  import { params } from 'mochi-framework';
</script>

<h1>{params.slug}</h1>

cookies

Since: 0.10.0 (not released yet): Before 0.10.0, cookies.set() and cookies.delete() in the browser wrote nothing when passed httpOnly: true and gave no development warnings, and cookies.set() in the browser ignored maxAge.

Read and write cookies on the server and the client through one API. In the browser, cookies.set() and cookies.delete() ignore httpOnly, because a browser discards any document.cookie write that carries it. See the Cookies demo.

In development, the browser jar warns in the console and the debug bar when a call passes httpOnly, and when a set() or delete() has no effect, for example a set() that would replace a cookie the server set with httpOnly. A cookie scoped to a path the page is not under is not checked, because the page cannot read it back.

<script>
  import { cookies } from 'mochi-framework';

  const theme = cookies.get('theme') ?? 'light';
</script>

maxAge (seconds) and expires (days, or a Date) set the same cookie lifetime on the server and in the browser. Delete a cookie with the same per-call options you set it with. See cookie:defaults.

With secureCookies on, a cookie the server sets is HttpOnly, so the browser can neither read nor overwrite it. Pass { httpOnly: false } where the server sets a cookie that an island reads or writes.

locals

Per-request data set by middleware. Server-only.

// file: src/middleware.ts
import type { Handle } from 'mochi-framework';

export const auth: Handle = async ({ event, resolve }) => {
  event.locals.user = await getUser(event.cookies);
  return resolve(event);
};
<!-- file: src/Dashboard.svelte -->
<script>
  import { locals } from 'mochi-framework';

  const user = locals.user;
</script>

getRequestContext()

Returns the full context object with all fields. Server-only. Prefer the individual exports above unless you need several fields at once.

import { getRequestContext } from 'mochi-framework';

const { url, params, cookies, locals, request, requestId } = getRequestContext();

url and cookies work on the server and the client. getRequestContext(), params, and locals are server-only and throw in the browser, so guard those branches with isServer.

The context also carries isWarmup — true when the request came from route warmup at startup, not a real client. Guard side effects in serverProps that should not fire for synthetic warmup hits:

serverProps: async () => {
  const ctx = getRequestContext();
  if (!ctx.isWarmup) await recordVisit(ctx.url.pathname); // skip warmup
  return { posts: await loadPosts() };
};

getCspNonce()

Since: 0.10.0 (not released yet): getCspNonce() ships in the next Mochi release (0.10.0).

Returns the current request’s CSP nonce, or undefined when the csp serve option is off. Use it to build the Content-Security-Policy header in middleware, or to stamp a script of your own:

<!-- file: src/Analytics.svelte -->
<script>
  import { getCspNonce } from 'mochi-framework';

  const nonce = getCspNonce();
</script>

<svelte:head>
  <script {nonce} src="/analytics.js"></script>
</svelte:head>

It also works in middleware for the fall-through 404, which has no full request context. In the browser it returns undefined.

See it in action

Live demos showing key concepts from this page