Preact PWA, configure maximumFileSizeToCacheInBytes to change this limit

Viewed 1296

I'm creating a PWA using the Preact Cli.

When compiling I get the warning:

/bundle.js is 2.15 MB, and won't be precached. Configure maximumFileSizeToCacheInBytes to change this limit.

Since Preact CLI generates a service worker when building, to customize the service worker, I've created a custom "sw.js" that uses the Preact CLI high-level API.

The question is how to use this API to change the "maximumFileSizeToCacheInBytes" (if possible).

1 Answers

This is definitely a situation where you need to be careful that you're not precaching large items accidentally, but if you need to up the limit, you can use your preact.config.js file to do so. All of this is done with WorkBox's InjectManifest plugin, so editing the service worker itself would do nothing.

preact.config.js

export default {
    webpack(config, env, helpers, options) {
        const manifestPlugin = helpers.getPluginsByName(config, 'InjectManifest')[0];
        if (manifestPlugin) {
            manifestPlugin.plugin.config.maximumFileSizeToCacheInBytes = 5 * 1024 * 1024; // or however much you need. This is 5mb.
        } 
    }
}
Related