Search code examples
workbox

Support bundling for non-webpack users in workbox-cli


I am running the below command on my dist directory.

workbox copyLibraries dist/en-in/; workbox generateSW workbox-config-prod.js;

The service worker file calls

importScripts("/workbox-v4.3.1/workbox-sw.js");

workbox-sw.js downloads below files:

   workbox-core.prod.js
   workbox-precaching.prod.js
   workbox-routing.prod.js
   workbox-strategies.prod.js
   workbox-expiration.prod.js
   workbox-cacheable-response.prod.js"

Question 1:

I have set long expiry cache headers on all files served from /workbox-v4.3.1/ path since it is versioned. Is there any downside to this approach?

Question 2:

Can Workbox provide support for teams which don't use any bundlers?

If workbox-cli can output a single file which has 1. Only those Workbox modules which as referenced in the workbox-config file. 2. Code generated from the workbox-config file. 3. Minified This was how sw-precache used to generate service worker file.


Solution

  • I have set long expiry cache headers on all files served from /workbox-v4.3.1/ path since it is versioned. Is there any downside to this approach?

    There's no downside to that approach because, as you mention, the /v4.3.1/ path segment is used in your URL, so the content should never change. There is some nuance around whether or not URLs loaded via importScripts() (which happens for all of the Workbox URLs) are actually checked for updates, and that's detailed more in this article. But what you're doing with Cache-Control headers should be fine.

    Can Workbox provide support for teams which don't use any bundlers?

    That's straightforward with Workbox v5 (as of Nov. 2019, in pre-release), using generateSW with the following configuration:

    {
      inlineWorkboxRuntime: true,
      mode: 'production',
      sourcemap: false,
      // ...other options...
    }
    

    That will generate a single service worker file that includes the Workbox runtime inline, minified, without a sourcemap, which is about as straightforward as the output gets. Under the hood, Workbox will use Rollup for you to create a custom bundle that only contains the parts of Workbox that you actually need, so you don't have to worry about doing bundling yourself.