Skip to main content
Before a page component can be rendered, SvelteKit needs to get data. This guide explains how data flows through your application.

Page data

A +page.svelte file can have a sibling +page.js that exports a load function:
src/routes/blog/[slug]/+page.js
src/routes/blog/[slug]/+page.svelte
The return value of a load function is available to the page via the data prop.

Layout data

Layout load functions work the same way:
1

Define layout load

Create a +layout.server.js file that returns data
src/routes/blog/[slug]/+layout.server.js
2

Access in layout

Use the data in +layout.svelte
src/routes/blog/[slug]/+layout.svelte
3

Access in child pages

Data from parent layouts is available to all child pages
src/routes/blog/[slug]/+page.svelte
If multiple load functions return data with the same key, the last one wins. A page load will override a layout load for the same key.

page.data

The page store provides access to all data for the current page:
src/routes/+layout.svelte
Use page.data when a parent layout needs to access data from a child page or layout.

Universal vs server load

There are two types of load functions:

Universal load

  • Files: +page.js, +layout.js
  • Runs on both server and client
  • Can return any value including classes
  • Reruns in browser during hydration

Server load

  • Files: +page.server.js, +layout.server.js
  • Only runs on the server
  • Must return serializable data
  • Access to cookies, databases, secrets

When each runs

Always run on the server, both during SSR and when fetching data for client-side navigation.
  • Run on the server during SSR
  • Run again during hydration
  • Run in the browser for subsequent navigations
  • Can be disabled via export const ssr = false

Combining both

When you have both, the server load runs first and its output is passed to the universal load:

URL data

Load functions have access to URL information:
URL
Contains origin, hostname, pathname, and searchParams
object
Contains the route ID
object
Derived from url.pathname and route.id

Making fetch requests

Use the provided fetch function in load:
The fetch in load has special powers:
  • Inherits cookies and authorization headers
  • Makes relative requests on the server
  • Inlines responses into HTML during SSR
  • Reads from HTML during hydration

Parent data

Access data from parent load functions with await parent():
Be careful not to create waterfalls. Call parent() after independent operations:

When load reruns

SvelteKit tracks dependencies to avoid unnecessary reruns:
If your load uses params.slug, it reruns when slug changes.
If your load uses url.pathname or url.search, it reruns when those change.
Calling url.searchParams.get('x') makes it rerun when x changes.
If a parent load reruns and this load calls await parent().
Via invalidate(url), invalidateAll(), or depends().

Streaming with promises

Return unresolved promises from server load to stream data:
Streaming only works when JavaScript is enabled and on platforms that support streaming responses.

Learn more

Follow the interactive tutorial on loading data