|
| 1 | +# Intercepting fetch in a service worker |
| 2 | + |
| 3 | +I'm learning service workers. I wanted to start with one that intercepts calls to a `/path` and returns "Hello World". |
| 4 | + |
| 5 | +Here's the initial recipe I came up with. |
| 6 | + |
| 7 | +`index.html` contained this: |
| 8 | + |
| 9 | +```html |
| 10 | +<h1>Service worker demo</h1> |
| 11 | + |
| 12 | +<script> |
| 13 | +const registerServiceWorker = async () => { |
| 14 | + if ('serviceWorker' in navigator) { |
| 15 | + try { |
| 16 | + const registration = await navigator.serviceWorker.register( |
| 17 | + '/sw.js', |
| 18 | + { |
| 19 | + scope: '/', |
| 20 | + } |
| 21 | + ); |
| 22 | + if (registration.installing) { |
| 23 | + console.log('Service worker installing'); |
| 24 | + } else if (registration.waiting) { |
| 25 | + console.log('Service worker installed'); |
| 26 | + } else if (registration.active) { |
| 27 | + console.log('Service worker active'); |
| 28 | + } |
| 29 | + } catch (error) { |
| 30 | + console.error(`Registration failed with ${error}`); |
| 31 | + } |
| 32 | + } |
| 33 | +}; |
| 34 | +
|
| 35 | +registerServiceWorker(); |
| 36 | +</script> |
| 37 | +``` |
| 38 | +This is using the service worker registration boilerplate [from MDN](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#registering_your_worker). |
| 39 | + |
| 40 | +Then my service worker script itself in `sw.js` just does this: |
| 41 | + |
| 42 | +```javascript |
| 43 | +self.addEventListener('fetch', (event) => { |
| 44 | + const request = event.request; |
| 45 | + const url = (new URL(request.url)); |
| 46 | + if (url.pathname == "/") { |
| 47 | + // Don't intercept hits to the homepage |
| 48 | + return; |
| 49 | + } |
| 50 | + const params = new URLSearchParams(url.search); |
| 51 | + const info = { |
| 52 | + url: request.url, |
| 53 | + method: request.method, |
| 54 | + path: url.pathname, |
| 55 | + params: Array.from(params.entries()) |
| 56 | + }; |
| 57 | + event.respondWith(new Response( |
| 58 | + `<p>Hello world! Request was: <pre>${JSON.stringify(info, null, 4)}</p>`, { |
| 59 | + headers: { 'Content-Type': 'text/html' } |
| 60 | + })); |
| 61 | +}); |
| 62 | +``` |
| 63 | +You have to run service workers with a real web server - you can't serve them directly from disk. |
| 64 | + |
| 65 | +I used the Python 3 one-liner recipe for that: |
| 66 | + |
| 67 | + python3 -m http.server 8009 |
| 68 | + |
| 69 | +Then I visited `http://localhost:8009/` to load the service worker. |
| 70 | + |
| 71 | +Then I visited `http://localhost:8009/foo/bar?a=1&b=2` and got this: |
| 72 | + |
| 73 | +<img alt="Hello world! Request was: { |
| 74 | + "url": "http://localhost:8009/foo/bar?a=1&b=2", |
| 75 | + "method": "GET", |
| 76 | + "path": "/foo/bar", |
| 77 | + "params": [ |
| 78 | + [ |
| 79 | + "a", |
| 80 | + "1" |
| 81 | + ], |
| 82 | + [ |
| 83 | + "b", |
| 84 | + "2" |
| 85 | + ] |
| 86 | + ] |
| 87 | +}" src="https://user-images.githubusercontent.com/9599/166125219-7820133e-4c9f-4ea2-898b-87b126f07115.png"> |
0 commit comments