Skip to content

@ogygia/data-shake

Datashake

@ogygia/data-shake ships only the page data your islands read, the way tree shaking ships only the code a page uses. The build reads each island's code for the page.data keys it reaches, so the page seed carries those keys and server islands that read none skip the page lookup.

On this page

An island that reads page.data gets a copy of it in the page, in a small script called the page seed. Without help, that copy is all of page.data: everything every load returned for the server render, even when the island only shows the user’s name. On a CMS page whose load returns a large content tree, the seed can be most of the HTML.

dataShake() is tree shaking for that data. At build time it 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, as before, and the build names the line.

<script lang="ts">
  import { page } from '$app/state';
</script>

<!-- with dataShake(), the seed carries page.data.user and nothing else -->
<p>Hello, {page.data.user.name}</p>

Installation

Install the package and list its plugin beside ogygia(), before sveltekit(). The factory comes from @ogygia/data-shake/vite, and it takes no options. There’s no handle and nothing to add to hooks.server.ts.

pnpm add @ogygia/data-shake
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { dataShake } from '@ogygia/data-shake/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [ogygia(), dataShake(), sveltekit()]
});

Page seed

The page seed ships only when an island on the page reads page (through $app/state or $app/stores). With dataShake(), its page.data holds the union of the keys the page’s islands read. An island pinned to no keys at all, one that reads only page.url or page.params, still gets the seed for those, with an empty page.data. The other fields (url, params, route, status, error, form and state) aren’t shaken.

Each island is judged on everything it bundles: its component and every module it imports. One module in that closure that reads page.data whole is enough to ship all of it for that island. Reads lists what the build can pin and what it can’t.

The keys are top-level only. Reading page.data.post.title ships all of page.data.post.

import type { PageServerLoad } from './$types';
import { getPost, getRelated } from '$lib/server/posts';

export const load: PageServerLoad = async ({ params }) => ({
  // read by an island: ships in the seed
  user: { name: 'Ada' },
  // rendered on the server, read by no island: left out of the seed
  post: await getPost(params.slug),
  related: await getRelated(params.slug)
});

Server islands

A server island renders in a request of its own. When its component might read page.data, ogygia has to look its page up for it, which runs the page’s server loads again on that request (Regions outside the page). Without dataShake(), ogygia can’t tell, so every server island’s request looks the page up.

With it, a server island whose code reads no page.data key skips the lookup. A live lake’s request does the same.

Dev and build

The shaping happens in the build. On the dev server the seed ships all of page.data, so a key you just started reading is never missing while you work. The analysis still runs in dev for the profiler, which shows which keys each island would ship and the line that makes one ship everything.

Postrender

postrender() requires dataShake(). Without the shaping, every server island on a stored page would look the page up, running its server loads on every fetch, which is the render the stored page was meant to save. Listing postrender() without dataShake() fails the build with plugin_needs_plugin, naming the line to add.

import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { postrender } from '@ogygia/postrender/vite';
import { dataShake } from '@ogygia/data-shake/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    ogygia(),
    postrender(),
    dataShake(),
    sveltekit()
  ]
});

Security

Shaping makes the seed smaller, not private. A key an island reads is in the HTML, and so is everything when a read can’t be pinned. Treat load data as visible to the browser, as on a csr = true page, and don’t return anything from load you wouldn’t send there. See Page data.

Package contents

PageWhat it covers
ReadsEvery way of reading page.data the build can pin, helpers it follows, and what ships it all.
Seed reportThe build’s page seed: line, the modules it names, and the profiler’s view.
@ogygia/data-shake/viteThe generated API reference for dataShake().