Export a load
function from a +page.js
script to prepare or fetch data for the page component. Load functions may be async
functions.
Data from the page script is passed to a data
props on the page component.
Page scripts allow data to be fetched or prepared before the page component is rendered. When a SvelteKit app is first visited, page scripts run on the server for the initial render. On subsequent visits, page scripts only run on the client.
In this example, we'll pass an array of todos retrieved from an API to a page component.
Export a load
function from a +page.js
script to prepare or fetch data for the page component. Load functions may be async
functions.
Data from the page script is passed to a data
props on the page component.
The load
function receives a context argument to access route parameters and other data.
Page scripts can throw errors and modify the HTTP response code.
The load
function can be typed with JSDoc or TypeScript.
The data
prop too.
Further reading on this topic.