When I just use pre-cache from Workbox Plugin, the service worker try to fetch from indexdb instead of cache(browser-cache).
Yes, service worker is faster than fetching from server. However, if it fetches from memory it takes 0 second.
I have seen all the strategies from workbox but it does not have a strategy that try to fetch from memory first.
This is everything for pre-cache.
new WorkboxPlugin.GenerateSW({
skipWaiting: true,
importWorkboxFrom: 'local',
})
service-worker.js
importScripts("/dist/workbox-v3.6.3/workbox-sw.js");
workbox.setConfig({modulePathPrefix: "/dist/workbox-v3.6.3"});
importScripts(
"/dist/precache-manifest.4b8be844a396ff2fc7335cebbab35f10.js"
);
workbox.skipWaiting();
/**
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
* requests for URLs in the manifest.
*/
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
You can't control how the request is served by the browser cache or the server, so you can't unfortunately first try browser cache, then Cache API, then server.
However, you can try to load both from Cache API and server (including browser cache) at the same time, and serve whichever gave an the fastest answer.
That's what Jake Archibald named "Cache & network race" in his Offline Cookbook: https://jakearchibald.com/2014/offline-cookbook/#cache-network-race