Skip to content

Data & state

Serialization

Every value a page hands an island, as a prop or through context, is serialized into the HTML and rebuilt in the browser. Data crosses as a copy, wired classes and stores cross live, and functions cross when marked with import.meta.og.$.

On this page

Each island is its own hydration root. A value the server-rendered page hands an island, as a prop or through context, is written into the HTML and rebuilt when the island wakes. So every such value crosses a serialization boundary, and one rule decides what happens to it: data crosses as a copy, and anything else crosses only if it knows how to rebuild itself on the other side.

Plain data needs no work. This page covers the rest: live objects, stores, functions, and what ogygia says when a value can’t cross.

In the example, a page passes one store and a number to two islands. Each island gets its own copy of step, but both get the same store, so a click in one repaints the other.

<script>
  import { writable } from 'svelte/store';
  import Counter from '$lib/Counter.svelte' with {
    wake: 'load'
  };

  const count = writable(0);
</script>

<Counter {count} step={1} />
<Counter {count} step={10} />
<script>
  let { count, step } = $props();
</script>

<button onclick={() => ($count += step)}>Count: {$count}</button
>

Value types

The same rules apply to island props, context values and the props of a held region.

You passHow it crossesWhat you do
Plain data, Date, Map, Set, URL, …A copy (devalue). Each island gets its own.Nothing.
An instance of a wired classLive: every island gets the same instance.Add a wire codec to the class.
A store (writable, anything with subscribe)Live: its value crosses, and every island shares one store.Nothing. See Stores.
A store with its own methodsLive, with its methods rebuilt from its factory.Usually nothing. See Factory stores.
A derived storeA frozen copy of its value. og_derived crosses live.See Derived stores.
A functionIts code ships as a build file, and what it captured crosses as data.Mark it with import.meta.og.$. See Pass a function.
Children and snippetsFrozen to HTML, or hydrated inside the island.Nothing. See Snippets.
A componentIt’s a held region, a different thing on purpose.region() and <Region>.
A bare function as a propTaken for a snippet, so it can freeze quietly.Mark it with import.meta.og.$, or don’t pass it.
A DOM node, a class with no codecRefused, with the path to the part that can’t cross.Change the value.

Functions

A function can’t be serialized: there’s no way to read what it closed over. What can cross is where its code lives plus what it captured. import.meta.og.$(fn) marks a function for that. The build moves the function into a module of its own, turns each local it captures into a parameter, and the island rebuilds the function from that module and the captured values. Its source is never written into the page.

Write the function inline in the mark. A function passed by name is checked as a value and refused (boundary_refused).

In the example, a layout provides a price formatter that captures the tax rate, and any island calls it as an ordinary function.

The rules:

  • Captures cross like props. A captured local has to be data, a wired instance or a store. A store or wired instance captured this way stays live, so every island’s copy of the function sees the same state.
  • Each island rebuilds its own copy. The function itself isn’t shared, so keep state in what it captures, not in the function.
  • A capture from a server-only module fails the build (macro_dollar_misuse): $app/server, $env/static/private, $env/dynamic/private or a *.server.* file. Its value would end up in the page’s HTML. Do the server work in a remote function and capture its result.
  • The mark takes exactly one argument, and a $() inside another $() fails the build. Mark the outer one.
  • It works in a component’s <script>, in its markup, and in a .ts module of your app. An installed package can use it only in files it lists in "ogygia.files" (macro_outside_app).

Check a value where it crosses

Give import.meta.og.$ something other than an inline function, and it checks the value where you wrote it. A store, a wired instance or plain data passes, and you get the same value back. A DOM node, a bare function inside an object or a class with no codec throws on that line when it runs (boundary_refused), with the path inside the value that failed, instead of failing later inside a serializer. You never need this for values that cross on their own, but it shows the next reader where the page splits.

<script>
  import { setContext } from 'ogygia';
  let { data, children } = $props();

  const tax = data.taxRate;
  setContext(
    'fmt',
   import.meta.og.$(
     (n) => `€${(n * (1 + tax)).toFixed(2)}`
    )
  );
</script>

{@render children()}
<script>
  import { getContext } from 'svelte';
  let { amount } = $props();

  const fmt = getContext('fmt');
</script>

<span>{fmt(amount)}</span>
<script>
  import Cart from '$lib/Cart.svelte' with { wake: 'load' };
  import { cartStore } from '$lib/stores';
</script>

<Cart
  items={import.meta.og.$(cartStore)}
  checkoutUrl={import.meta.og.$((id) => `/checkout/${id}`)}
/>

Stores

A store (writable, or any object with subscribe) crosses live with no mark. Its current value crosses when the page renders, and in the browser every island that gets the store, by prop or context, gets the same store. A set in one island repaints every other. The same store under two context keys stays one store.

The server never shares a store between requests: each render rebuilds it fresh, as it does for a wired class.

Factory stores

A store with its own methods (add, reset) needs its factory on the other side, since methods are code and never cross. The factory’s first parameter is the seed: the store’s value, which does cross.

The build finds a factory on its own when it can prove the factory returns a store: an exported const arrow function that returns an object with subscribe, or calls writable() or readable() from svelte/store directly. For any other factory, such as one that returns a helper’s result, mark it with import.meta.og.store(factory). It takes exactly one argument, a function or a function’s name (macro_store_misuse). ogygia/types doesn’t declare store yet, so add its type yourself, as Add the types shows.

When a store reaches an island whose code never loaded its factory, it becomes a plain writable: the value stays, the methods are gone, and the browser warns, naming the module to import (store_factory_missing). It never crashes. A factory that returns something other than a store on some path warns at build time (macro_store_returns_non_store); return a store on every path, say writable(seed) behind your guard.

Derived stores

A derived store crosses as a frozen copy of its value, because the derivation is code. To keep it live across islands, build it with og_derived from ogygia and mark the formula with import.meta.og.$. Its sources cross as live stores and its formula as a function, so each island derives again from the shared sources. Without the mark, the formula can’t cross (og_derived_formula_missing).

A $store read inside a snippet that crosses into an island is a frozen copy too. See Snippets.

<script>
  import { setContext } from 'ogygia';
  import { writable } from 'svelte/store';
  let { children } = $props();

  setContext('theme', writable('dark'));
</script>

{@render children()}
import { writable } from 'svelte/store';
import { makeStore } from '$lib/make-store';

// found by the build: it returns an object with subscribe
export const createCart = (seed: string[] = []) => {
  const { subscribe, set, update } = writable(seed);
  return {
    subscribe,
    set,
    update,
    add: (item: string) => update((x) => [...x, item])
  };
};

// returns a helper's result, so mark it
export const createWishlist =  import.meta.og
  .store 
  ((seed: string[] = []) => makeStore(seed));
import { og_derived } from 'ogygia';
import { writable } from 'svelte/store';

export const tally = writable(1);
export const doubled = og_derived(
  tally,
  import.meta.og.$((n: number) => n * 2)
);

Server-only values

Some context is only read by the server-rendered page around the islands: element refs, a callback, a large config. setContext(key, value, { islands: false }) keeps it out of the HTML, and same-tree getContext still reads it. See Swap the import in an existing layout.

Secrets

Everything that crosses is written into the page’s HTML, readable by anyone who views the source. ogygia doesn’t filter it: a token in a prop or a context value ships. Pass the browser only what it may see, and read anything private on the server, in a remote function or a server island.

Refused values

What you see depends on where the value was going:

  • A prop that can’t be copied stops the server render with prop_not_serializable.
  • A bare function prop is the one quiet case. The server takes it for a snippet and freezes what it returns, so it can fail quietly instead of with function_prop_cannot_cross. A bare function in <Provide> values is treated the same way.
  • A <Provide> value that can’t be copied, such as a class with no codec, stops the server render.
  • A setContext value that can’t cross is dropped from the page, so islands read undefined, and dev names the key, the path to the part that failed and what to do (context_dropped, context_unserializable, context_value_function).
  • A value marked with import.meta.og.$ throws on the line where it’s marked (boundary_refused).
  • A wired instance whose class the island can’t load fails with transportable_class_missing.