Last month I opened the browser console on my own site and found an error I had never seen before:
Uncaught (in promise) TypeError: Failed to execute 'put' on 'Cache': Request method 'POST' is not supported The site still worked. The downloader still downloaded. But the error kept coming back, and an ignored console error usually returns as a real bug later. So I went looking.
What the service worker was doing
My site, ramrom.io, is a small set of video downloader tools built in plain PHP. It has a service worker, sw.js, so the site is installable and loads fast on repeat visits. The first version of that worker was simple: on every fetch event, try the cache first, fall back to the network, then store the response for next time. It looked like this:
self.addEventListener('fetch', (event) => { event.respondWith( caches.open('v1').then(async (cache) => { const cached = await cache.match(event.request); if (cached) return cached; const response = await fetch(event.request); cache.put(event.request, response.clone()); return response; }) ); }); That looks harmless. It is not.
The two requests the Cache API refuses
A service worker's fetch handler sees every request the page makes — not just the ones for your own files. On my site that included two kinds of request the Cache API simply will not accept:
- Chrome extension requests. Visitors with extensions installed generate requests to
chrome-extension://URLs. The service worker intercepts them, andcache.put()fails on them. - POST requests. The downloader sends POST requests to my own
resolve.phpendpoint. The Cache API only stores GET requests, so every resolve call threw the same error.
Both failures were invisible to visitors — the page still worked, because only the caching step failed. But a noisy console hides the errors that actually matter.
The fix: guard the handler, cache only what is yours
Version 2 of sw.js starts by deciding whether a request is even worth caching. If it is not a GET request to my own origin, the worker steps aside and lets the browser handle it normally:
self.addEventListener('fetch', (event) => { const url = new URL(event.request.url); // Only cache GET requests for our own origin. if (event.request.method !== 'GET' || url.origin !== self.location.origin) { return; // let the browser handle it } event.respondWith( caches.open('v2').then(async (cache) => { const cached = await cache.match(event.request); if (cached) return cached; const response = await fetch(event.request); // Only cache successful responses. if (response.ok) { cache.put(event.request, response.clone()); } return response; }) ); }); Three small changes, each doing a real job:
- Method check.
event.request.method !== 'GET'skips POST calls toresolve.phpbefore they can reachcache.put(). The downloader's API calls were never meant to be cached anyway — a resolve answer from ten minutes ago is a stale answer. - Origin check.
url.origin !== self.location.originskips extension requests, third-party fonts, analytics pings, everything that is not my site. The worker should only ever manage its own files. - Status check.
if (response.ok)means error pages never get stored. Before this, a 404 or 500 response could be cached and served back to the next visitor as if it were the real page.
I also bumped the cache name from v1 to v2, and added an activate handler that deletes any old cache. Without that, the old v1 cache would sit there forever, holding the old files.
self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((names) => Promise.all( names.filter((name) => name !== 'v2').map((name) => caches.delete(name)) ) ) ); }); What I check now
The error is gone, and the worker still does its real job: the tool pages load from cache on repeat visits, the install prompt still appears, and the downloader's POST calls go straight to the server untouched. After any service worker change I now check: load the page twice, use the downloader once, and confirm zero console errors and a working download. A service worker is a layer that sits between your visitors and your site — if it is going to intercept everything, it should at least know what belongs to it and what does not.
