2
votes

I have an app that runs service worker. I'm using sw-toolbox library for dynamic caching of URLs, but I want to create wrapper over sw-toolbox that provides getters and setters to my application for URL caching.

As service worker runs in different thread and my application running in main thread, so just wondering how to create a wrapper in Javascript through which my application can communicate with service worker and cache resources on-demand?

3

3 Answers

1
votes

So far the Cache api is available from content, that means you can use it directly from your Javascript code, no need to run in the Service Worker.

Check this thread to find the resolution: https://github.com/slightlyoff/ServiceWorker/issues/698 You will be able to use caches from the window object.

Meaning you have all range of Cache methods to play with the content: https://developer.mozilla.org/en-US/docs/Web/API/Cache

Just another reminder, from Chrome 46, you will be able to store stuff just in secured origins.

0
votes

It depends how your worker caches data, it it just uses the standard "Cache" API than you can just query cache object which is attached to global scope.

In this particular case Cache.match() is your friend.

0
votes

From the Documentation, Cache API/interface is exposed to windowed scopes as well as workers.

You don't have to use it in conjunction with service workers, even thought it is defined in the service worker spec.