+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. This is 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, and your progress syncs across the web and the CoddyKit app. The Sveltejs Academy course includes 4 lessons in total.
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.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 41
- Lessons
- 163
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. 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. The Sveltejs Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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