Svelte logo

SvelteKit by Example: Server Scripts

Server scripts also allow data to be fetched or prepared before the page component is rendered. However, server scripts only run on the server.

In this example, we'll pass an array of todos retrieved from a database to a page component.

Export a load function from a +page.server.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.

import { getListsFromDatabase } from './db.js';

/** @type {import('./$types').PageServerLoad} */
export async function load() {
  return {
    lists: await getListsFromDatabase(),
  /** @type {import('./$types').PageData} */
  export let data;

  {#each data.lists as list}
      <a href={`/lists/${}`}>