0
0
Svelteframework~20 mins

Why API routes serve data endpoints in Svelte - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Route Mastery in SvelteKit
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do API routes serve data endpoints in SvelteKit?

In SvelteKit, API routes are used to serve data endpoints. Why is this approach preferred over serving data directly from page components?

ABecause API routes separate data fetching logic from UI, making the app more modular and easier to maintain.
BBecause page components cannot fetch data at all in SvelteKit.
CBecause API routes automatically cache all data without any configuration.
DBecause API routes are the only way to serve HTML content in SvelteKit.
Attempts:
2 left
💡 Hint

Think about how separating concerns helps in organizing code.

component_behavior
intermediate
2:00remaining
What happens when you fetch data from an API route in SvelteKit?

Consider a SvelteKit app where a page component fetches JSON data from an API route. What is the expected behavior when the page loads?

AThe API route directly renders HTML and sends it to the page component.
BThe page component requests data from the API route, which returns JSON; the component then uses this data to render UI.
CThe page component cannot fetch data from API routes; it must use static data only.
DThe API route sends data as plain text, which the page component cannot parse.
Attempts:
2 left
💡 Hint

Think about how JSON data is commonly used between backend and frontend.

📝 Syntax
advanced
2:30remaining
Identify the correct SvelteKit API route syntax to return JSON data

Which of the following code snippets correctly defines a SvelteKit API route that returns JSON data?

Svelte
export async function GET() {
  // return JSON data
}
A
export async function GET() {
  return JSON.stringify({ message: 'Hello' });
}
B
export function get() {
  return { message: 'Hello' };
}
C
export async function GET() {
  return { body: { message: 'Hello' } };
}
D
export async function GET() {
  return new Response(JSON.stringify({ message: 'Hello' }), { headers: { 'Content-Type': 'application/json' } });
}
Attempts:
2 left
💡 Hint

Remember that API routes must return a Response object with proper headers.

🔧 Debug
advanced
2:30remaining
Why does this SvelteKit API route cause a runtime error?

Given this API route code, why does it cause a runtime error when called?

Svelte
export async function GET() {
  return { message: 'Hello' };
}
ABecause the function is missing the async keyword.
BBecause the JSON data is not stringified before returning.
CBecause the function must return a Response object, not a plain object.
DBecause the function should be named get with lowercase letters.
Attempts:
2 left
💡 Hint

Check what the API route function is expected to return.

state_output
expert
3:00remaining
What is the output of this SvelteKit page fetching data from an API route?

Consider a SvelteKit page component that fetches data from an API route returning { "count": 5 }. What will be the value of count displayed on the page after loading?

Svelte
<script>
  import { onMount } from 'svelte';
  let count = 0;
  onMount(async () => {
    const res = await fetch('/api/count');
    const data = await res.json();
    count = data.count;
  });
</script>

<p>Count: {count}</p>
ACount: 5
BCount: 0
CCount: undefined
DCount: NaN
Attempts:
2 left
💡 Hint

Think about when count is updated and what onMount does.