What is the purpose of +page.server.js in SvelteKit?

Server-only load boundaries in SvelteKit.
Exclusively server-side load feeding the data prop, used for secrets like DB queries and private env vars.
Claiming it runs in the browser or equating it with +page.js.
What's really being asked
Whether the candidate understands SvelteKit's file-based routing and the critical distinction between universal load functions and server-only load functions. The interviewer wants to know if you can place sensitive operations on the correct side of the network boundary and explain why +page.server.js exists as a separate concept from +page.js.
The full answer
Four things in order. First, the file is a sibling to +page.svelte and exports a load function that executes exclusively on the server during SSR or when an endpoint is hit directly. Second, the return value of that load function is serialized and provided to the page as the data prop. Third, it is the correct place for secrets like database queries, calls to internal microservices, or reading private environment variables through $env modules. Fourth, it contrasts with +page.js which runs universally on both server and browser and therefore cannot safely contain credentials or server-only modules.
The mistakes people make
Saying +page.server.js runs in the browser during client-side navigation. Claiming it is just a naming preference with no behavioral difference from +page.js. Storing API keys or direct SQL in +page.js and assuming they stay hidden. Forgetting that the returned data is still sent to the client, so you must not return raw password hashes or internal tokens even from a server file.
What usually comes next
How would you access cookies or request headers during a load? When would you use +page.js instead of +page.server.js? How does SvelteKit handle a load error thrown in a server file? What is the difference between a server load function and a form action? Can you stream promises from a server load?
A concrete example
Imagine a dashboard at routes/admin/+page.server.js. The load function imports a database client, reads a private connection string from a server-only env module, queries the users table for admin metrics, and returns { metrics }. The +page.svelte component receives metrics through its data prop and renders charts. Because the database client and connection string never appear in +page.js, they are never shipped to the browser. If the user navigates client-side from another page, SvelteKit still calls the server load via a fetch to the page endpoint, keeping the secrets server-bound.
Interview question
Why would you choose +page.server.js over +page.js for a SvelteKit route?
- a.To ensure the load function executes in the browser during SPA navigation
- b.To avoid serializing the load function's return value to the data prop
- c.To safely use private environment variables and database credentialsCorrect
- d.To indicate a naming preference with no difference from +page.js
Why? this is the answer
+page.server.js executes only on the server, so it is the safe place for private environment variables and database credentials. The most tempting distractor is B because, although the code stays server-side, the returned data is still serialized and sent to the client as the data prop.
Just read this? Test yourself on what you have been reading.
Read the original → svelte.dev
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles