@ogygia/data-shake
Reads
The page.data reads dataShake() can pin to a key, the helpers it follows, the branches it knows run only on the server, and every read that ships all of page.data.
On this page
The build reads each module that imports the page store (page from $app/state, or page from $app/stores, read as $page) once, for the top-level page.data keys it reaches. When it’s sure, the module is pinned to those keys. When it isn’t, the module ships all of page.data, with a reason. It never guesses in the other direction: a key a module might read is never left out.
Pinned reads
These name their keys, in a <script>, a .svelte.ts module or the component’s markup:
page.data.user,page.data?.user,$page.data.user;page.data['user'], with a string literal;const { user, cart } = page.data(and= page.data || {});- nested reads such as
page.data.post.title, which pin the top-level key,post; - the same through an alias of the page:
const p = page,const p = get(page),derived(page, ($p) => $p.data.user), orderived([a, page], ([$a, $p]) => …); - the same in every template construct:
{#if},{#each},{#await},{@const}, attributes,bind:, handlers and snippets; - TypeScript wrappers around the page or
page.data(as,!,satisfies, parentheses).
Reads of page.url, params, route, status, error, form and state pin no page.data key at all. So does Object.keys(page), which reads names only.
<script lang="ts">
import { page } from '$app/state';
const { cart } = page.data;
</script>
{#each cart.items as item (item.id)}
<p>{item.name} for {page.data['currency']}</p>
{/each}Helpers
A module that hands the page to a function of its own is followed into that function: its parameter is the page inside it.
A function imported from another module is followed one level deep. The build resolves the import, reads the function it names, and folds in what that function reads of its parameter. It looks through up to three re-exports on the way, an export * included; a helper four or more re-exports away ships all of page.data (#2313). A helper that hands the page on to yet another function, isn’t a plain function, or can’t be found ships all of page.data for the module that called it.
The parameter must be a plain name. A helper that destructures it (function localeOf({ data })) ships all of page.data. Take the page as p and read p.data.locale inside, as below.
import type { Page } from '@sveltejs/kit';
export function localeOf(p: Page): string {
return (
p.data.locale ?? p.url.searchParams.get('lang') ?? 'en'
);
}<script lang="ts">
import { page } from '$app/state';
import { localeOf } from '$lib/locale';
let { amount }: { amount: number } = $props();
</script>
<!-- pinned to `locale`, read inside localeOf() -->
<span>{amount.toLocaleString(localeOf(page))}</span>Whole reads
Each of these ships all of page.data for the island, with the reason in the build report:
page.dataused bare: passed to a function, returned, spread ({ ...page.data }), stored, compared or put in an array;page.data[key]with a key that isn’t a string literal;- destructuring
page.datawith a rest (...rest) or a computed key; page.dataspread into a component’s or an element’s attributes;- the data object aliased (
const d = page.data) or destructured off the page (const { data } = page), even when onlyd.useris read after; - the whole page passed to a function the build can’t follow, or that hands it on;
- the page passed to a function whose parameter is destructured (
({ data }) => …); - any other field read off the page (
page.foo); page.subscribe(…),getStores(), orderived(page, …)with a callback it can’t read;- the page store imported as a namespace, with
import(), or re-exported; - a function given the page that reads
arguments.
The fix is always the same: read the keys you need by name. In the example, the first island ships everything; the second ships title and tags.
<script lang="ts">
import { page } from '$app/state';
import { describe } from 'some-seo-lib';
// ships all of page.data: the whole object goes to a function the build can't follow
const meta = describe(page.data);
</script>
<p>{meta}</p><script lang="ts">
import { page } from '$app/state';
import { describe } from 'some-seo-lib';
// ships title and tags
const meta = describe({
title: page.data.title,
tags: page.data.tags
});
</script>
<p>{meta}</p>