PWAs & Service Workers Questions with Answers

13 practice questions on PWAs & Service Workers (Modern Web (Next.js, PWA, Performance)) with answers and step-by-step solutions. Try 10 samples below, then practise all 13 free in the AptiNest app.

Sample questions

Question 1Easy

Which file describes a PWA's name, icons and display mode for installation?

  1. The web app manifest
  2. The service worker
  3. package.json "pwa" field
  4. robots.txt

Answer: A. The web app manifest

Explanation

The manifest (manifest.json / .webmanifest) gives install metadata such as name, icons, start_url and display.

Step-by-step solution

  1. The web app manifest describes how a PWA looks when installed.
  2. It holds name, icons, start_url and display mode.
  3. It is a JSON file linked from the page.
  4. So the correct answer is the web app manifest.

Why the other options are wrong

  • B. The service worker handles requests, not install metadata.
  • C. package.json has no pwa field used for installation.
  • D. robots.txt controls crawlers, not app installation.

Tip: Manifest = install details (name, icons, display); service worker = offline and network logic.

Question 2Easy

Can a service worker access the DOM directly?

  1. Only after calling self.attachDOM()
  2. Yes; it can read and change document directly
  3. No; it communicates with pages through messages
  4. Only the DOM of the first page that registered it

Answer: C. No; it communicates with pages through messages

Explanation

Service workers run in a separate worker context without DOM access; they use postMessage and clients APIs.

Step-by-step solution

  1. Service workers run in a separate worker thread.
  2. Workers have no access to document or window.
  3. They talk to pages with postMessage and clients.

Why the other options are wrong

  • A. There is no attachDOM method.
  • B. No DOM access exists in workers.
  • D. No page's DOM is accessible.

Tip: Worker = no DOM, use postMessage.

Question 3Easy

What is a service worker?

  1. A Web Worker that runs CSS animations off the main thread
  2. A server process that renders pages before sending them to the browser
  3. A browser extension that blocks unwanted network requests
  4. A background script that can intercept network requests

Answer: D. A background script that can intercept network requests

Explanation

Service workers sit between the page and the network, enabling offline support, caching strategies and push notifications.

Step-by-step solution

  1. A service worker is a script that runs in the background.
  2. It sits between the page and the network.
  3. It can intercept requests through the fetch event.
  4. So the correct answer is a background script that can intercept network requests.

Why the other options are wrong

  • A. A service worker is not about CSS animations; it is for network interception.
  • B. A service worker runs in the browser, not as a server process.
  • C. A service worker is not a browser extension.

Tip: Service worker = programmable proxy in the browser for offline and caching.

Question 4Easy

Which event does a service worker listen to in order to intercept requests?

  1. request
  2. intercept
  3. load
  4. fetch

Answer: D. fetch

Explanation

The fetch event fires for requests within its scope; event.respondWith() supplies the response.

Step-by-step solution

  1. A service worker listens for events on the global scope.
  2. The fetch event fires for each request within its scope.
  3. event.respondWith() then provides the response.
  4. So the correct answer is the fetch event.

Why the other options are wrong

  • A. Request is not a service worker event name.
  • B. Intercept is not an event name; the event is fetch.
  • C. Load is a page event, not the interception event.

Tip: In a service worker, use the fetch event and call event.respondWith().

Question 5Medium

A new service worker has installed but the old one still controls open tabs. What is its state by default?

  1. Active immediately in all open tabs
  2. Discarded because only one worker is allowed
  3. Redundant and must be registered again
  4. Waiting until the old worker's clients close

Answer: D. Waiting until the old worker's clients close

Explanation

By default the new worker waits so one version controls a page at a time; skipWaiting() can activate it sooner.

Step-by-step solution

  1. A new service worker installs and then waits.
  2. While an old worker still controls open pages, the new one does not take over.
  3. This keeps one version controlling a page at a time.
  4. So the correct answer is that it waits until the old worker's clients close.

Why the other options are wrong

  • A. It does not activate at once by default; skipWaiting() changes that.
  • B. Several workers can exist; only one controls pages at a time.
  • C. The new worker is not redundant; it is waiting.

Tip: A new service worker waits by default; skipWaiting() and clients.claim() change that.

Question 6Medium

Which strategy suits frequently updated data where freshness matters but offline should still work?

  1. Cache-first with no expiry at all
  2. Network-first, falling back to the cache
  3. Cache-only, ignoring the network fully
  4. Network-only with no fallback

Answer: B. Network-first, falling back to the cache

Explanation

Network-first tries for fresh data and falls back to the last cached copy when offline.

Step-by-step solution

  1. The data changes often, so freshness matters.
  2. Network-first asks the network first for the latest data.
  3. If the network fails, the last cached copy is used, so offline still works.
  4. So the correct answer is network-first, falling back to the cache.

Why the other options are wrong

  • A. Cache-first with no expiry serves stale data for a long time.
  • C. Cache-only never gets fresh data.
  • D. Network-only fails offline, which the question wants to avoid.

Tip: Fresh-but-offline-capable data: network-first with cache fallback.

Question 7Medium

What is the "activate" event typically used for?

  1. Deleting caches left by older versions
  2. Responding to network requests
  3. Showing the install prompt
  4. Pre-caching the files for the first time

Answer: A. Deleting caches left by older versions

Explanation

Activate runs when the new worker takes over, a safe point to remove outdated caches.

Step-by-step solution

  1. The activate event fires when a new worker takes over.
  2. The old version is no longer in use.
  3. It is the standard place to delete old caches.

Why the other options are wrong

  • B. That is the fetch event.
  • C. The install prompt is separate (beforeinstallprompt).
  • D. Pre-caching belongs in the install event.

Tip: install = cache new, activate = clean old.

Question 8Medium

In a "cache-first" strategy, what happens on a request?

  1. Use the cached copy if present, else the network
  2. Never use the network once the cache is installed
  3. Return whichever of cache and network answers first
  4. Always go to the network and update the cache afterwards

Answer: A. Use the cached copy if present, else the network

Explanation

Cache-first is ideal for static assets: fast and offline-friendly, at the cost of possibly stale content.

Step-by-step solution

  1. Cache-first checks the cache before the network.
  2. If a cached copy exists, it is used.
  3. The network is only used when nothing is cached.
  4. So the correct answer is use the cached copy if present, else the network.

Why the other options are wrong

  • B. Cache-first still uses the network when the cache has nothing.
  • C. Racing both is a different strategy, not cache-first.
  • D. Going to the network first is network-first or network-only.

Tip: Cache-first suits static assets; network-first suits fresh data.

Question 9Hard

What does the scope of a service worker registered at /app/sw.js default to?

  1. The whole origin from /
  2. /app/ and pages beneath it
  3. Only the page that registered it
  4. Only the file /app/sw.js itself

Answer: B. /app/ and pages beneath it

Explanation

The default scope is the directory of the script, so it controls /app/ and below.

Step-by-step solution

  1. Scope defaults to the directory containing the worker script.
  2. /app/sw.js gives scope /app/.
  3. Pages under /app/ are controlled.

Why the other options are wrong

  • A. Widening scope needs the Service-Worker-Allowed header.
  • C. It controls all pages in scope, not just one.
  • D. Scope is a path prefix, not a single file.

Tip: Scope follows the script folder.

Question 10Hard

What is the "stale-while-revalidate" caching strategy in a service worker?

  1. Wait for the network and only use cached data when the request takes too long
  2. Respond only from the network and store nothing so that data is never stale
  3. Answer from cache now and refresh the cache from the network
  4. Delete the cache whenever a request fails, then wait for the next request

Answer: C. Answer from cache now and refresh the cache from the network

Explanation

It gives fast responses from cache while keeping it reasonably fresh for the next visit.

Step-by-step solution

  1. Stale-while-revalidate answers the request from the cache right away.
  2. At the same time it fetches from the network.
  3. The fresh response then updates the cache for the next visit.
  4. So the correct answer is answering from cache now and refreshing the cache from the network.

Why the other options are wrong

  • A. This describes a network-first timeout, not stale-while-revalidate.
  • B. Network-only stores nothing, which is not this strategy.
  • D. Deleting the cache on failure is not a standard strategy.

Tip: Stale-while-revalidate: instant cached answer, refresh in the background.

Practise all 13 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Modern Web (Next.js, PWA, Performance) topics · All topics