Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Why is this service worker logic not working?

I have the following service worker written in TypeScript:

self.addEventListener("fetch", (event: any) => {
    if (event.request.method == "POST") {
        event.respondWith(
            fetch(event.request).catch(error => {
                return caches.match(event.request);
            })
        );
    } else {
        event.respondWith(
            caches.match(event.request).then(cachedResponse => {
                // It can update the cache to serve updated content on the next request
                return cachedResponse || fetch(event.request);
            })
        );
    }
});

What I want it to do is if there's a POST request on the page, use a network first caching strategy (fetch from the network).

If there aren't any changes (not POST), use the cache first strategy (fetch from cache).

The issue is that it doesn't work. It will never fetch from the network. I have also tried to return on POST like this:

self.addEventListener("fetch", (event: any) => {
    if (event.request.method == "POST") return;
    event.respondWith(
        caches.match(event.request).then(cachedResponse => {
            // It can update the cache to serve updated content on the next request
            return cachedResponse || fetch(event.request);
        })
    );
});

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!