Stencil version:
@stencil/[email protected]
I'm submitting a:
[X] bug report
Current behavior:
All SVG files in the ionicons project are being pre-cached by the sw.js
Expected behavior:
Ideally only the icons being used (somehow) would be cached.
Steps to reproduce:
The easiest way to reproduce this is with the ionic-pwa-toolkit project. If you run a build inside that and inspect the generated sw.js file you will see every SVG has been added to the precacheAndRoute list.
I am not directly including ionicons in my app (as neither is the ionic-pwa-toolkit) it is being included as a dependency of @ionic/core.
Other information:
Currently the ionic-pwa-toolkit pre-caches 725 files on first install, which seems excessive to me. In my app (which is based/seeded from ionic-pwa-toolkit) I'm looking to reduce this number. 697 of these files I believe are from ionicons.
Found that adding globIgnores: ['**/svg/*.svg'] to be stencil.config.{tj}s will prevent them from being cached. Now probably just need to manually add each one I'm using back in using a custom sw.js.
The approach used for SVG files also has some other issues:
I think a better approach for icons would be to have the svg in importable js files.
import { checkmarkIcon } from '@ionic/icons';
import { myIcon } from './my-icon';
<ion-icon icon={checkmarkIcon} />
<ion-icon icon={myIcon} />
Some benefits of this approach:
More details: https://github.com/ionic-team/ionicons/issues/536
Hey, I was also already looking into this and think I found a solution that works kind of ok in my case...
First I ignore all the SVGs for pre-caching:
globIgnores: ['build/app/*.es5.*.js', 'build/app/svg/*.svg']
Then in my service worker I enable the cacheFirst workbox strategy for the /build/app/svg route:
self.workbox.routing.registerRoute(
/build\/app\/svg\/.+\.svg/i,
self.workbox.strategies.cacheFirst(),
);
Not sure which strategy is the best to use, maybe staleWhileRevalidate would be better. Anyway, this caches the icons after they were requested so they are available offline from then on. Assuming that the user would have an internet connection when first loading the PWA and signing in, that would work fine until the user goes offline and then hits a route they haven't visited before. I think I could live with that in my use case (I mostly have labels next to icons anyway).
Most helpful comment
Hey, I was also already looking into this and think I found a solution that works kind of ok in my case...
First I ignore all the SVGs for pre-caching:
Then in my service worker I enable the
cacheFirstworkbox strategy for the/build/app/svgroute:Not sure which strategy is the best to use, maybe
staleWhileRevalidatewould be better. Anyway, this caches the icons after they were requested so they are available offline from then on. Assuming that the user would have an internet connection when first loading the PWA and signing in, that would work fine until the user goes offline and then hits a route they haven't visited before. I think I could live with that in my use case (I mostly have labels next to icons anyway).