Complete the code to fetch data inside a load function in SvelteKit.
export async function load() {
const response = await fetch([1]);
const data = await response.json();
return { data };
}The load function fetches data from a server endpoint, so the URL string is needed.
Complete the load function to return the fetched data correctly.
export async function load() {
const res = await fetch('/api/info');
const info = await res.json();
return [1];
}props object like Next.js getServerSideProps.return info;) without an object.The load function returns an object whose properties are passed to the page component as data.
Fix the error in the load function to fetch data server-side without using browser globals.
export async function load() {
const data = await [1].then(res => res.json());
return { data };
}In load functions, avoid browser globals like window. Use relative URLs for server-side fetch.
Fill both blanks to fetch JSON data and return it properly in a load function.
export async function load() {
const response = await fetch([1]);
const jsonData = await response.[2]();
return { jsonData };
}Use a URL string for fetch and call json() to parse the response.
Fill all three blanks to fetch data, check response status, and return props in a load function.
export async function load() {
const res = await fetch([1]);
if (!res.[2]) {
throw new Error('Failed to fetch');
}
const data = await res.[3]();
return { data };
}Fetch from a URL string, check ok to confirm success, then parse JSON.