0
0
Svelteframework~20 mins

Invalidation and reloading in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SvelteKit Invalidation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Svelte store is invalidated?

Consider a Svelte component that subscribes to a writable store. What is the immediate effect on the component when the store's value is invalidated using invalidate()?

Svelte
import { writable } from 'svelte/store';
const count = writable(0);

// Somewhere in the component
count.set(1);
// invalidate('count'); // <-- invalidate() is not a method on writable stores
AThe store throws an error because invalidate() is not a valid method on writable stores.
BThe store value resets to its initial value automatically without any update in the component.
CThe component stops subscribing to the store and no longer updates on changes.
DThe component re-runs its reactive statements and updates the DOM with the latest store value.
Attempts:
2 left
💡 Hint

Think about what invalidate() triggers in Svelte's reactive system.

state_output
intermediate
2:00remaining
What is the output after invalidation and reload in a SvelteKit page?

Given a SvelteKit page with a load function that fetches data, what will be the displayed value after calling invalidate('path')?

Svelte
export async function load({ fetch }) {
  const res = await fetch('/api/data');
  const data = await res.json();
  return { props: { data } };
}

// In the component
<script>
  import { invalidate } from '$app/navigation';
  let count = 0;
  function refresh() {
    invalidate('/api/data');
    count += 1;
  }
</script>
<button on:click={refresh}>Refresh {count}</button>
<p>{JSON.stringify(data)}</p>
AThe page does not fetch new data; the displayed JSON remains the same after clicking refresh.
BThe page fetches fresh data from '/api/data' and updates the displayed JSON accordingly.
CThe page reloads completely, losing the current state including the count variable.
DAn error occurs because invalidate cannot be called with an API path.
Attempts:
2 left
💡 Hint

Invalidate triggers a reload of the data for the specified path.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly invalidates a SvelteKit page data?

Choose the correct way to invalidate the data of the current page in SvelteKit.

A
import { invalidate } from '$app/navigation';

await invalidate();
B
import { invalidate } from '$app/navigation';

await invalidate(window.location.pathname);
C
import { invalidate } from '$app/navigation';

await invalidate('/current-page');
D
import { invalidate } from '$app/navigation';

await invalidate('/api/data');
Attempts:
2 left
💡 Hint

Think about how to specify the current page path dynamically.

🔧 Debug
advanced
2:00remaining
Why does this SvelteKit invalidate call not reload data?

Given this code snippet, why does calling invalidate('/api/data') not cause the page to reload fresh data?

Svelte
import { invalidate } from '$app/navigation';

async function refresh() {
  await invalidate('/api/data');
}

// load function
export async function load({ fetch }) {
  const res = await fetch('/api/data');
  const data = await res.json();
  return { props: { data } };
}
ABecause the load function does not return data in the correct format for invalidation.
BBecause invalidate requires a full URL including protocol and domain to work correctly.
CBecause '/api/data' is an API endpoint, not a page route, invalidate does not trigger load to run again.
DBecause invalidate only works on client-side stores, not on load functions.
Attempts:
2 left
💡 Hint

Consider what paths invalidate expects to reload.

🧠 Conceptual
expert
3:00remaining
How does SvelteKit handle invalidation and reloading for nested layouts?

In a SvelteKit app with nested layouts, what happens when you call invalidate() on a parent layout's path?

AThe entire app reloads from scratch, losing all client-side state.
BOnly the parent layout reloads; child layouts and pages keep their current data without reloading.
CNo layouts reload; invalidate only works on leaf page routes, not layouts.
DAll child layouts and pages under that parent path reload their data by re-running their load functions.
Attempts:
2 left
💡 Hint

Think about how SvelteKit cascades data loading in nested routes.