I'm using a NetworkFirst strategy for a NavigationRoute in my workbox-based service worker, so that navigation requests will be served cached responses while the user is offline.
workbox.routing.registerRoute(
new workbox.routing.NavigationRoute(
new workbox.strategies.NetworkFirst({
cacheName: 'static-pages',
}),
),
);
When I navigate to a page, the navigation request gets cached, and then I can visit it again while offline. So far so good.
However, I need to pre-cache the PWA start URL, so if the user installs the PWA from any page, goes offline, and tries to open it, the PWA start URL is cached and available. In fact, this will be a requirement for the installability prompt as of Chrome 91.
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.open('static-pages').then(cache => {
const startUrl = 'https://my-pwa-start-url...';
return cache.add(startUrl);
})
);
});
The issue here is that the request for startUrl is not a navigation request, so even though it gets cached in the static-pages cache, it is not used when navigating to the start URL.
I tried manually constructing a navigation request, e.g.
const startUrlRequest = new Request(startUrl, { mode: 'navigate' });
return cache.add(startUrlRequest);
But then I get this error:
TypeError: Failed to construct 'Request': Cannot construct a Request with a RequestInit whose mode member is set as 'navigate'.
Is there any way to pre-cache a NavigationRequest without actually navigating to the page?