If your load function can only run on the server. For example, if it needs to fetch data from a database or you need to access private environment variables like API keys, then you can rename +page.js to +page.server.js and change the PageLoad type to PageServerLoad.
// src/routes/blog/[slug]/+page.server.ts
import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
export const load = (async ({ params }) => {
const post = await getPostFromDatabase(params.slug);
if (post) {
return post;
}
throw error(404, 'Not found');
}) satisfies PageServerLoad;During client-side navigation, SvelteKit will load this data from the server, which means that the returned value must be serializable using devalue. See load for full details of the API.
Like +page.js, +page.server.js can export page options — prerender, ssr and csr.
A +page.server.js file can also export actions. If load lets you read data from the server, actions let you write data to the server using the <form> element. To learn how to use them, see the form actions section.