The load function must return an object whose keys become props for the page. Returning raw data (like an array or primitive) is invalid.
Step 2: Identify the fix
Wrap the fetched data inside an object, e.g., return { data }, so the page receives it properly.
Final Answer:
The returned data must be wrapped in an object -> Option A
Quick Check:
Load returns object with keys for props [OK]
Hint: Always return an object from load function [OK]
Common Mistakes:
Returning raw data instead of object
Forgetting to export load
Thinking fetch is disallowed in load
5. You want to redirect users to the login page if they are not authenticated inside a +page.js load function. Which code snippet correctly does this?
hard
A. import { redirect } from '@sveltejs/kit';
export const load = () => {
const user = null;
if (!user) return redirect(302, '/login');
return { user };
};
B. export const load = () => {
const user = null;
if (!user) return '/login';
return { user };
};
C. import { redirect } from '@sveltejs/kit';
export const load = () => {
const user = null;
if (!user) throw redirect(302, '/login');
return { user };
};
D. export const load = () => {
const user = null;
if (!user) window.location.href = '/login';
return { user };
};
Solution
Step 1: Understand how to redirect in load
SvelteKit uses the redirect function from '@sveltejs/kit' which throws an exception to redirect the user.
Step 2: Check each option
import { redirect } from '@sveltejs/kit';
export const load = () => {
const user = null;
if (!user) throw redirect(302, '/login');
return { user };
}; correctly imports redirect and throws it when user is not authenticated. export const load = () => {
const user = null;
if (!user) return '/login';
return { user };
}; returns a string which is invalid. export const load = () => {
const user = null;
if (!user) window.location.href = '/login';
return { user };
}; uses browser redirect which won't work server-side. import { redirect } from '@sveltejs/kit';
export const load = () => {
const user = null;
if (!user) return redirect(302, '/login');
return { user };
}; returns redirect instead of throwing it, which is incorrect.
Final Answer:
import { redirect } from '@sveltejs/kit'; export const load = () => { const user = null; if (!user) throw redirect(302, '/login'); return { user }; }; -> Option C
Quick Check:
Throw redirect to redirect in load [OK]
Hint: Throw redirect() from load to redirect users [OK]
Common Mistakes:
Returning redirect instead of throwing it
Using window.location in load (server-side)
Returning a string URL to redirect
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.