Skip to content
Advertisement

Should I return a promise in a JS Service Worker onFetch callback function if I don’t want to do anything with the original request?

My scenario is the following:

  1. I have a Progressive Web App that uses a Service Worker where I need to catch the request and do something with it every time the user requests a resource or leaves the current URL
  2. I’m handling that through adding a callback to the fetch event of the worker
  3. I only care about requested resources within our domain (e.g. example.com)
  4. If the requested resource is within our domain I return the promise result from a regular fetch, so that’s already covered
  5. But, if the requested resource is outside my domain (as shown in the below snippet) I want the original request to just continue
  6. I’m currently just doing a simple return if the scenario in bullet 5 is true

Snippet of my current code:

function onFetch(event) {
  if (!event.request.url.startsWith("example.com")) {
    return;
  } else {
    event.respondWith(
      fetch(event.request)
        .then(req => {
          // doing something with the request
        })
        .catch((error)=> {
          // handle errors etc.
        })
        .finally(()=> {
          // cleanup
        })
    );
  }
}

self.addEventListener('fetch', onFetch);

My question: Is it OK if I just return nothing like in the snippet, or, do I need to return something specific, like a new promise by fetching the original request (like I’m doing on the else block)?

Thanks!

Advertisement

Answer

It is absolutely okay to do what you’re doing. Not calling event.respondWith() is a signal to the browser that a given fetch handler is not going to generate a response to a given request, and you can structure your code to return early to avoid calling event.respondWith().

You might have multiple fetch handlers registered, and if the first one returns without calling event.respondWith(), the next fetch handler will then get a chance to respond. If all of the fetch handlers have executed and none of them call event.respondWith(), the browser will automatically handle the request as if there were no service worker at all, which is what you want.

In terms of observed behavior, not calling event.respondWith() at all ends up looking similar to what would happen if you called event.respondWith(event.request). But there is overhead involved in making a fetch() request inside of a service worker and then passing the response body from the service worker thread back to the main program, and you avoid that overhead if you don’t call event.respondWith(). So, I’d recommend the approach you’re taking.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement