Complete the code to export a load function in +page.js that returns a simple message.
export const load = async () => {
return { message: [1] };
};The load function must return an object. The message value should be a string wrapped in quotes.
Complete the code to import the 'fetch' function and use it inside the load function to get JSON data.
export const load = async ({ [1] }) => {
const res = await fetch('/api/data');
const data = await res.json();
return { data };
};The load function receives an object with properties. To use the fetch function, destructure 'fetch' from the argument.
Fix the error in the load function by completing the code to return a 404 status when data is not found.
export const load = async ({ fetch }) => {
const res = await fetch('/api/item');
if (!res.ok) {
return { status: [1] };
}
const item = await res.json();
return { item };
};When the fetch response is not ok, returning status 404 indicates the item was not found.
Fill both blanks to destructure 'params' and 'fetch' from the load function argument and fetch data using a dynamic id.
export const load = async ({ [1], [2] }) => {
const res = await fetch(`/api/item/${params.id}`);
const item = await res.json();
return { item };
};The load function argument is an object. We destructure 'params' to get route parameters and 'fetch' to make requests.
Fill all three blanks to return a redirect status and location from the load function.
export const load = async () => {
return {
status: [1],
redirect: [2],
message: [3]
};
};To redirect, return status 302 with a redirect URL and an optional message string.