Data & state
Page data
An island reads page from $app/state, or $page from $app/stores, as a Kit component does, on csr = false pages too. Your load's data, url, params, route, status, form and error arrive with the page, a promise in the data streams into the island, and with dataShake() only the keys islands read are shipped.
On this page
An island often needs the page’s own data: the signed-in user from your layout’s load, the route’s params, the result of a form action. Without Kit’s client, $app/state would be empty on a csr = false page. ogygia fills it: an island imports page from $app/state (or $page from $app/stores) and reads it exactly as a Kit component does. Your load runs on the server as always.
For data an island fetches itself, on demand or after a click, use a remote function instead.
In the example, the layout’s load returns the user, and an island reads it with the current path. The App.PageData declaration types page.data.user in a strict TypeScript app.
export const load = ({ locals }) => ({
user: { name: locals.user?.name ?? 'guest' }
});<script>
import { page } from '$app/state';
</script>
<p>
Bonjour, {/*MK<*/ page.data.user.name}. You're on {page.url
.pathname}.
</p>declare global {
namespace App {
interface Locals {
user?: { name: string };
}
interface PageData {
user: { name: string };
}
}
}
export {};Available fields
| Field | What it holds |
|---|---|
page.data | The merged data of every load for this page. |
page.url | The page’s URL. |
page.params, page.route | The route’s params and its id. |
page.status | The response status. |
page.form | The result of a form action, after a <form method="POST">. |
page.error | The error, on an error page. |
page.state | Always {}: there’s no shallow routing without Kit’s client. |
navigating.current is always null and updated.current is always false on these pages.
After a client navigation with the SPA router, and after a soft refresh (invalidateAll(), or a successful remote form), page holds the new page’s values, and islands that read it update. A form submit that calls .updates(…), or whose handler refreshes queries on the server, skips the soft refresh, so page keeps its old values (Submit forms).
page.form makes it easy to show a form action’s result in an island: the island reads it with no prop. Since a plain <form method="POST"> is a full page load, the page arrives with page.form already set.
Page seed
The page carries page to the browser in a small script, the page seed, only when an island on it reads page. A page whose islands don’t read it ships none.
By default the seed carries all of page.data. With dataShake() from @ogygia/data-shake listed beside ogygia(), the build reads each island’s code for the top-level page.data keys it reaches, and a page ships only the keys its islands read. A read it can’t pin to a key ships all of page.data, and the build names the line. Reads lists what it can pin.
So on a page whose load returns a big tree for the server render, list dataShake() and read keys by name in islands to keep the seed small.
An island prop that is a piece of page.data (the same object, or a copy) isn’t written twice: the props point into the seed.
Treat load data as visible to the browser, as on a csr = true page. Whatever the seed carries is in the HTML. Don’t return anything from load you wouldn’t send to the browser.
Streaming
Kit lets load return a promise anywhere in its data, so the page goes out right away and the value follows. On a csr = false page, Kit’s own stream has nothing to feed, since there’s no page-level client. An island does have one, and ogygia streams the promise into it.
The page and the island’s pending branch paint at once, so the first paint never waits for the slowest promise. Each promise resolves on its own as it settles, fast ones first, and the island’s {#await} switches to its result with no extra request. A promise that rejects shows the {:catch} branch. A promise that resolves to data with more promises inside streams those too, as in Kit.
On a client navigation, the router can’t run streamed scripts in the middle of a swap, so the server awaits each promise before it sends the page. page.data.recs is still a promise you {#await}, already settled, so the island’s code is the same either way.
A promise that’s still pending after 10 seconds keeps the response open, and dev says so (page_promise_pending). There’s no deadline, so give the work behind it a timeout of its own. If the response ends before the value arrives, the promise rejects with page_promise_unresolved, and {:catch} shows instead of a wait that never ends.
import { recommendationsFor } from '$lib/server/recs';
export const load = ({ locals }) => ({
country: 'FR',
recs: recommendationsFor(locals.user)
});<script>
import { page } from '$app/state';
</script>
{#await page.data.recs}
<p>Loading recommendations…</p>
{:then recs}
<ul>
{#each recs as r}<li>{r.title}</li>{/each}
</ul>
{:catch}
<p>Recommendations are unavailable.</p>
{/await}Custom types
Types you teach Kit with the transport hook arrive in islands as real instances, in plain data and inside a streamed promise. Keep ogygia’s entries in the same object.
In the example, page.data.price.pretty works inside an island, because decode rebuilt the class rather than a plain copy.
export class Money {
constructor(public cents: number) {}
get pretty() {
return `$${(this.cents / 100).toFixed(2)}`;
}
}import * as ogygia from 'ogygia';
import { Money } from '$lib/money';
export const transport = {
...ogygia.transport,
Money: {
encode: (v: unknown) => v instanceof Money && [v.cents],
decode: ([cents]: [number]) => new Money(cents)
}
};Regions outside the page
A server island, or a region a remote function renders, renders in its own request, not the page’s. It still reads the page it belongs to: ogygia looks the page up through Kit’s data request, with the visitor’s cookies, and fills page.url, route, params and page.data from it, once per request. Without dataShake(), every server island looks the page up. With it, only one whose code reads page.data does.
Only server loads run in that lookup. A key a universal load (+page.ts, +layout.ts) returns isn’t there, and dev names it (outside_page_data_key); return it from a server load, or pass it as a prop. If the lookup fails, the region renders with an empty page.data (outside_page_data_unavailable). Refresh a site header shows this at work.
Do and don’t
- Do read literal keys (
page.data.user) in islands, sodataShake()can trim the seed. - Do return a promise from
loadfor slow data below the fold, and{#await}it in an island. - Do give a streamed promise’s work its own timeout.
- Don’t put anything in
loaddata that the browser mustn’t see. - Don’t read universal-load keys in a server island: return them from a server load.