+page.svelte component (and its containing +layout.svelte components) can be rendered, we often need to get some data. This is done by defining load functions.
Page data
A+page.svelte file can have a sibling +page.js that exports a load function:
Universal vs server load functions
There are two types ofload function:
- Universal load
- Server load
Files:
+page.js and +layout.js- Run both on the server and in the browser
- Can return any values, including component constructors
- Useful for fetching from external APIs
When does which load function run?
1
Server load functions
Always run on the server.
2
Universal load functions
By default, run on the server during SSR when the user first visits your page. They will then run again during hydration, reusing any responses from fetch requests.
3
Concurrent execution
If a route contains both universal and server
load functions, the server load runs first.Layout data
Your+layout.svelte files can also load data via +layout.js or +layout.server.js:
Data returned from layout
load functions is available to child +layout.svelte components and the +page.svelte component.Using URL data
Theload function provides access to URL information:
url
url
An instance of
URL, containing properties like origin, hostname, pathname and searchParams:route
route
Contains the name of the current route directory:
params
params
Derived from
url.pathname and route.id:Making fetch requests
The providedfetch function has special features:
Credentialed requests
Inherits
cookie and authorization headers on the serverRelative requests
Can make relative requests on the server
Direct routing
Internal requests go directly to handler function
Response inlining
Responses are captured and inlined into rendered HTML during SSR
Working with cookies
A serverload function can get and set cookies:
Setting headers
Both server and universalload functions have access to a setHeaders function:
Setting the same header multiple times is an error. You can only set a given header once using the
setHeaders function.Using parent data
Aload function can access data from a parent load function with await parent():
1
Root layout
2
Child layout
3
Page
Error handling
If an error is thrown duringload, the nearest +error.svelte will be rendered:
Redirects
To redirect users, use theredirect helper:
Streaming with promises
When using a serverload, promises will be streamed to the browser as they resolve:
Rerunning load functions
SvelteKit tracks dependencies to avoid rerunningload functions unnecessarily.
When do load functions rerun?
Aload function will rerun when:
Parameter changes
Parameter changes
It references a property of
params whose value has changedURL changes
URL changes
It references a property of
url (like url.pathname or url.search) whose value has changedSearch params changes
Search params changes
It calls
url.searchParams.get(...) and the parameter in question changesParent rerun
Parent rerun
It calls
await parent() and a parent load function reranManual invalidation
Manual invalidation
A URL it depends on was marked invalid with
invalidate(url) or all functions were rerun with invalidateAll()Manual invalidation
You can rerunload functions using:
- invalidate()
- invalidateAll()
Authentication implications
Important considerations for auth checks: Recommended strategies:1
Use hooks
Protect multiple routes before any
load functions run using server hooks2
Page-level guards
Use auth guards directly in
+page.server.js for route-specific protection3
Avoid layout guards
Putting auth guards in
+layout.server.js requires all child pages to call await parent() before protected codeNext steps
Form actions
Learn how to handle form submissions
Page options
Configure rendering behavior for your pages