+page.server.js for Server-Only Logic
Keep secrets and database calls on the server by using +page.server.js.
+page.server.js for Server-Only Logic is a free Sveltejs Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Server-Only
Some data must stay on the server: database calls, secret API keys, file system access.
File Name
Use +page.server.js instead of +page.js.
Same Interface
Export load just like +page.js. SvelteKit knows it is server-only.
import { db } from "$lib/server/db";
export async function load({ params }) {
const post = await db.posts.find(params.slug);
return { post };
}Bundled Out of Client
The file and its imports are stripped from client bundles. Secrets stay secret.
Env Variables
Use $env/static/private for secrets you only want on the server.
import { DB_URL } from "$env/static/private";Database Connections
Initialize DB clients in $lib/server modules so they never reach the client.
Combining With +page.js
If both exist, the server load runs first; its result is passed to the universal load.
Form Actions
Server-only files also host form action handlers (next course).
Authentication
Read cookies and session info here. Set headers via the cookies helper.
Performance
Server load runs on every request; cache wisely with HTTP caching or memoization.
Type Safety
Server load types flow into the page automatically via ./$types.
Quick Check
Where does +page.server.js code run?
Recap
Use +page.server.js for server-only logic. The file is excluded from the client bundle.
Frequently asked questions
Is the “+page.server.js for Server-Only Logic” lesson free?
Yes — the full text of “+page.server.js for Server-Only Logic” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.
What will I learn in “+page.server.js for Server-Only Logic”?
Keep secrets and database calls on the server by using +page.server.js. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Sveltejs Academy?
No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “+page.server.js for Server-Only Logic” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Sveltejs Academy lesson?
Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- +page.js load Function
- Returning Data and Using $page.data
- Error Handling in load
- +page.server.js for Server-Only Logic