Skip to content
Foxipa
Code Completion 5 Challenges

Complete the code: Workers & Interception

Fill in the missing realm checks and Service Worker interception patterns.

Intermediate ~4 min read

Challenge 1

The Service Worker's beacon-detection heuristic.

js Your answer
self.addEventListener('fetch', (event) => {
  const r = event.request;
  if (r. === true && r.method === 'POST') {
    handleBeacon(event);
  }
});

Press Ctrl⌘Enter to check your answer

Challenge 2

Complete the check that proves a SW cannot originate beacons.

js Your answer
// inside the service worker:
const canBeacon = typeof navigator. === 'function'; // false — the SW's navigator lacks it

Press Ctrl⌘Enter to check your answer

Challenge 3

Respond to a beacon from the Service Worker.

js Your answer
async function handleBeacon(event) {
  await logPayload(event.request);
  event.respondWith(new (null, { status: 204 });
}

Press Ctrl⌘Enter to check your answer

Challenge 4

The worker-safe recipe's realm probe.

js Your answer
const isWorker = typeof  === 'undefined';

Press Ctrl⌘Enter to check your answer

Challenge 5

The worker substitute's fetch options.

js Your answer
return fetch(url, {
  method: '',
  body: data ?? undefined,
  keepalive: true,
});

Press Ctrl⌘Enter to check your answer