Tuesday, July 12, 2022

[FIXED] How to not cache server worker client

Issue

I've a webpage that sends a message to a service worker script. To be able to do this, the webpage needs to be a client of the server worker script. There are different methods to do this, but as far as I know all methods results in that client webpages are cached in the service worker.

I was wondering if it's possible that a webpage can send messages to a server worker script, withouth it having to be cached to the server worker?


Solution

Sure—there's no requirement that a service worker cache anything at all, or even that a service worker implement a fetch handler and intercept network requests. You could create a service worker that just has a message handler that listens for incoming messages from the pages it controls:

// service-worker.js:
self.addEventListener('message', event => {
  // Do something with event.data
});

But if you're just using a service worker to listen for messages, and not intercept network requests, I wonder whether an alternative like a SharedWorker or just a plain-vanilla Worker would be a better alternative?



Answered By - Jeff Posnick
Answer Checked By - Robin (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.