kevinmarrec / nuxt-pwa-module

⚠️ DEPRECATED ⚠️ Zero config PWA solution for Nuxt 3
MIT License
339 stars 32 forks source link

is it possible to precache assets with precacheAndRoute ? #78

Closed bhaskarGyan closed 1 year ago

bhaskarGyan commented 1 year ago

Hi,

I am trying to precache all the assets on load using WB precacheAndRoute but I am not able to find this option in the Module.

Similar implementation in vite-plugin-pwa[https://vite-pwa-org.netlify.app/workbox/inject-manifest.html] where it can inject self.__WB_MANIFEST during build time.

I tried using below custom build script after nuxt build is done

const {injectManifest} = require('workbox-build');

// These are some common options, and not all are required.
// Consult the docs for more info.
injectManifest({
    globDirectory:'.output/public/',
    "globPatterns": [
        "**/*.{json,ico,html,png,js,txt,css,svg}"
      ],
  maximumFileSizeToCacheInBytes: 500000000000000,
  swDest: '.output/public/sw.js',
  swSrc: 'public/sw.js',
}).then(({count, size, warnings}) => {
  if (warnings.length > 0) {
    console.warn(
      'Warnings encountered while injecting the manifest:',
      warnings.join('\n')
    );
  }

  console.log(`Injected a manifest which will precache ${count} files, totaling ${size} bytes.`);
});

but nuxt don't allow modifying the file under .output dir after the build

I also tried with build:done hooks, but looks like it's too late by then to modify any file.

It's not possible to run the above script before build, since there won't be any file generated for caching

kevinmarrec commented 1 year ago

The option is not implemented yet, there's no 1:1 features with Nuxt 2 PWA, but this is going to change, I've plans for many things for this module for this start of the year.

Current solution is to implement your own workbox worker (for this module, the worker template is available here : https://github.com/kevinmarrec/nuxt-pwa-module/blob/main/templates/workbox/sw.js

May be a bit related to https://github.com/kevinmarrec/nuxt-pwa-module/issues/63 regarding caching