0
0
Svelteframework~20 mins

Service workers in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Service Worker Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Svelte app registers a service worker with this code?

Consider this Svelte code snippet that registers a service worker:

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
    .then(() => console.log('Service Worker registered'))
    .catch(err => console.error('Registration failed:', err));
}

What is the expected behavior when this code runs in a supported browser?

Svelte
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
    .then(() => console.log('Service Worker registered'))
    .catch(err => console.error('Registration failed:', err));
}
AThe service worker script is downloaded but never registered or activated.
BThe service worker is immediately activated and controls all pages without waiting.
CThe service worker registration fails silently without any console messages.
DThe browser tries to register the service worker script at '/sw.js' and logs success or failure accordingly.
Attempts:
2 left
💡 Hint

Think about what navigator.serviceWorker.register does and how promises work.

state_output
intermediate
1:30remaining
What is the value of navigator.serviceWorker.controller after page load if no service worker was previously controlling the page?

After registering a service worker for the first time and reloading the page, what will navigator.serviceWorker.controller be on the first load?

Svelte
console.log(navigator.serviceWorker.controller);
Aundefined
Bnull
CThrows a ReferenceError
DAn object representing the active service worker
Attempts:
2 left
💡 Hint

Think about when a service worker starts controlling a page.

🔧 Debug
advanced
2:30remaining
Why does this service worker fail to cache files correctly?

Examine this service worker code snippet:

self.addEventListener('install', event => {
  event.waitUntil(
    caches.open('v1').then(cache => {
      cache.addAll([
        '/index.html',
        '/styles.css',
        '/script.js'
      ]);
    })
  );
});

Why might the caching fail silently?

Svelte
self.addEventListener('install', event => {
  event.waitUntil(
    caches.open('v1').then(cache => {
      return cache.addAll([
        '/index.html',
        '/styles.css',
        '/script.js'
      ]);
    })
  );
});
ABecause the service worker script is missing the 'activate' event listener.
BBecause the cache name 'v1' is invalid and causes an error.
CBecause cache.addAll returns a promise that is not returned inside waitUntil, so install may finish before caching completes.
DBecause the files listed are not valid URLs and cause network errors.
Attempts:
2 left
💡 Hint

Check how promises are handled inside event.waitUntil.

🧠 Conceptual
advanced
1:30remaining
What is the main purpose of the 'activate' event in a service worker?

In service worker lifecycle, what is the primary role of the 'activate' event?

ATo clean up old caches and take control of uncontrolled pages.
BTo register the service worker script with the browser.
CTo intercept network requests and serve cached responses.
DTo install new files into the cache during the first load.
Attempts:
2 left
💡 Hint

Think about what happens after installation but before the service worker controls pages.

📝 Syntax
expert
2:30remaining
Which option correctly uses SvelteKit's service worker registration in a +layout.svelte file?

In SvelteKit, you want to register a service worker only on the client side inside a +layout.svelte component. Which code snippet correctly does this?

A
<script>
  import { onMount } from 'svelte';
  onMount(() => {
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('/sw.js');
    }
  });
</script>
B
<script>
  if (typeof window !== 'undefined') {
    navigator.serviceWorker.register('/sw.js');
  }
</script>
C
<script>
  import { onMount } from 'svelte';
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/sw.js');
  }
</script>
D
<script>
  import { onMount } from 'svelte';
  onMount(() => {
    navigator.serviceWorker.register('/sw.js');
  });
</script>
Attempts:
2 left
💡 Hint

Remember that navigator is only available in the browser and onMount runs only on client side.