I'm developing a PWA starting from a blank CRA template. The application needs to fully work offline after the installation, so I'm exploiting Workbox's methods to precache all static content.
Unfortunately I have severals contents between 5 and 10 MB (audio files) and in the Create React App Service Worker the limit is set to 5MB (previosly 2MB - see here).
Those files are not precached and indeed i get warnings during the build process:
/static/media/song.10e30995.mp3 is 5.7 MB, and won't be precached. Configure maximumFileSizeToCacheInBytes to change this limit.
.
Pity that maximumFileSizeToCacheInBytes
seems not to be configurable in CRA SW :(
I cannot decrese the files size because of audio quality requirements.
I also wrote a custom SW logic to precache external resources from the internet and I thought of adding the audio files there. but the React build process adds an hashcode to the filenames based on their content, so I would need to change the SW code every time I update the audio content, and that's not ideal.
So my questions are:
- is there any way to force the
maximumFileSizeToCacheInBytes
limit to a custom value, within a CRA application? - I was thinking of trying to get the hashcodes after the build process and automatically update the SW code, but is not very convincing to me.
- is there any other solution or method to achieve what I need?
Here's my SW code (the default CRA code + my custom logic at the end)
import { clientsClaim } from 'workbox-core';
import { ExpirationPlugin } from 'workbox-expiration';
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { StaleWhileRevalidate } from 'workbox-strategies';
import packageJson from '../package.json';
clientsClaim();
// Precache all of the assets generated by your build process.
// Their URLs are injected into the manifest variable below.
// This variable must be present somewhere in your service worker file,
// even if you decide not to use precaching. See https://cra.link/PWA
precacheAndRoute(self.__WB_MANIFEST);
// Set up App Shell-style routing, so that all navigation requests
// are fulfilled with your index.html shell. Learn more at
// https://developers.google.com/web/fundamentals/architecture/app-shell
const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$');
registerRoute(
// Return false to exempt requests from being fulfilled by index.html.
({ request, url }) => {
// If this isn't a navigation, skip.
if (request.mode !== 'navigate') {
return false;
} // If this is a URL that starts with /_, skip.
if (url.pathname.startsWith('/_')) {
return false;
} // If this looks like a URL for a resource, because it contains // a file extension, skip.
if (url.pathname.match(fileExtensionRegexp)) {
return false;
} // Return true to signal that we want to use the handler.
return true;
},
createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
);
// An example runtime caching route for requests that aren't handled by the
// precache, in this case same-origin .png requests like those from in public/
registerRoute(
// Add in any other file extensions or routing criteria as needed.
({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'), // Customize this strategy as needed, e.g., by changing to CacheFirst.
new StaleWhileRevalidate({
cacheName: 'images',
plugins: [
// Ensure that once this runtime cache reaches a maximum size the
// least-recently used images are removed.
new ExpirationPlugin({ maxEntries: 50 }),
],
})
);
// This allows the web app to trigger skipWaiting via
// registration.waiting.postMessage({type: 'SKIP_WAITING'})
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
// Any other custom service worker logic can go here.
const CUSTOM_PRECACHE_NAME = `custom-precache-v${packageJson.version}`;
const CUSTOM_PRECACHE_URLS = [
// ... external resources URLs here
];
self.addEventListener('install', event => {
const now = new Date();
console.log(`PWA Service Worker adding ${CUSTOM_PRECACHE_NAME} - :: ${now} ::`);
event.waitUntil(caches.open(CUSTOM_PRECACHE_NAME)
.then(cache => {
return cache.addAll(CUSTOM_PRECACHE_URLS)
.then(() => {
self.skipWaiting();
});
}));
});
// The fetch handler serves responses for same-origin resources from a cache.
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(resp => {
// @link https://mcmap.net/q/219432/-what-causes-a-failed-to-execute-39-fetch-39-on-39-serviceworkerglobalscope-39-39-only-if-cached-39-can-be-set-only-with-39-same-origin-39-mode-error
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
return;
}
return resp || fetch(event.request)
.then(response => {
return caches.open(CUSTOM_PRECACHE_NAME)
.then(cache => {
cache.put(event.request, response.clone());
return response;
});
});
})
);
});
I hope I made myself clear.
Thanks in advance, Francesco