-
Notifications
You must be signed in to change notification settings - Fork 829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow specifying specific strategies for precaching #1767
Comments
Assigning this to @philipwalton for the latest thinking. I know that he's been looking into this space, but the ultimate resolution might just be not to proceed with this feature request. (For the record, I am of the opinion that allowing folks to swap in alternative strategies for precaching is a Bad Idea, and that the best approach for folks who want something other than cache-first behavior is to runtime caching instead.) |
Personally, I would love to have online first strategy for precaching. For someone like me who is only just getting into service workers, it would feel much safer to only rely on the cache when offline, but otherwise run normally. Eventually I would probably switch to offline first anyway, but the onlinefirst mode can be an important step in between allowing people to feel more confident about going fully offline first. |
I don't think you should use You can accomplish that by setting up a runtime caching route that uses a network-first strategy, and if needed, calling
|
My use-case is to:
The default strategy implemented by I can not use runtime caching with Actually, I need the network first strategy only for This solution isn't suitable too because the cache is stored for each individual navigation route (instead of a single cache like required for SPA shell). Also @jeffposnick How can I configure |
I've found an almost perfect (but still not suitable) service worker code for my case: // The ordinary SW stuff here like importScripts, self.addEventListener, etc.
// self.__precacheManifest includes index.html
workbox.precaching.precacheAndRoute(self.__precacheManifest, {
directoryIndex: '', // To make the code below works as expected at the root path
});
// Implements the network-first strategy for navigation routes that falls back to preloaded cache (index.html)
workbox.routing.registerRoute(
new workbox.routing.NavigationRoute(async ({ url }) => {
const cacheName = workbox.core.cacheNames.precache;
const cacheKey = workbox.precaching.getCacheKeyForURL('/index.html');
const cache = await caches.open(cacheName);
let networkResponse;
let networkError;
try {
networkResponse = await fetch(url);
if (networkResponse.ok) {
await cache.put(cacheKey, networkResponse.clone());
return networkResponse;
}
} catch (error) {
networkError = error;
}
const cacheMatch = await cache.match(cacheKey);
if (cacheMatch) {
return cacheMatch;
} else if (networkResponse) {
return networkResponse;
} else {
throw networkError;
}
})
);
// Don't add `workbox.routing.registerNavigationRoute(workbox.precaching.getCacheKeyForURL("/index.html"));` The lack is that when I reload the updated application, then go offline and reload the page again, my new HTML file is used but Workbox doesn't load the new versions of assets (added through I assume that the Workbox precaching module is built with «apply update when tabs closed» strategy and I can't mix strategies: either obey or write your own precaching mechanism. It would be great if Workbox has an option to use the network first strategy for precaching. |
I've been talking to @philipwalton about this for v6, as he's been planning on a rewrite of the The main takeaway has been that it's not safe to allow developers to override the default There are legitimate use cases for pre-populating a cache while using a non-cache-first
Given that, I'm going to close this issue. |
Just sharing a little hack I made to force network-first for index.html: // sw.js
import { precacheAndRoute } from 'workbox-precaching'
import { registerRoute } from 'workbox-routing'
import { skipWaiting, clientsClaim } from 'workbox-core'
import navigationRoute from './js/sw/navigation'
precacheAndRoute([
// for some reason, it's an object and precacheAndRoute complains when it's not converted to array
...self.__WB_MANIFEST
])
registerRoute(navigationRoute())
skipWaiting()
clientsClaim() // navigation.js
import { cacheNames } from 'workbox-core'
import { NavigationRoute } from 'workbox-routing'
import { NetworkFirst } from 'workbox-strategies'
export default function() {
const networkFirst = new NetworkFirst({
cacheName: cacheNames.precache
})
return new NavigationRoute(options => {
options.request = new Request('/index.html')
return networkFirst.handle(options)
})
} I don't know what are the gotchas yet for this hack. If anyone knows or encounters some, I'd love to know as well. |
To those that come in the future and want to work @jeffposnick's solution but it doesn't see m to use the right cache, use the runtime cache mentioned here instead : |
Library Affected:
workbox-precaching
Browser & Platform:
All
Issue or Feature Request Description:
By default precaching uses a bespoke cache-first strategy. This is the right strategy most of the time, but not all the time. See discussion here: https://twitter.com/jeffposnick/status/1067081651803619328.
It is possible to work around this right now, but only by creating routes from scratch and configuring them to use the precache cache directly, and doing so can silently cause problems and break things with some strategies.
It would be useful to be able to pass a strategy into precache route setup directly. With that in place, workbox could:
The text was updated successfully, but these errors were encountered: