Force Angular Service Worker to Ignore External Images

19 views
Skip to first unread message

Zach Becker

unread,
Aug 16, 2020, 2:44:52 AM8/16/20
to Angular and AngularJS discussion
I am currently in the final phases of creating a PWA using Angular's Service Worker package. The web site I am building currently has a home page with 50, dynamic-template images from an external API (eg: http://blah.com/v2/{{id}}/icon.png). The current service worker set up caches these images and uses up the browsers storage. Every day these images change and so it does not pay to cache them plus, there are 50 so it takes up A LOT of space in the browser. Is there a way to prevent images from the external source from being cached?

The following is the current set up of my ngsw-config.json file:

{
   
"$schema": "./node_modules/@angular/service-worker/config/schema.json",
   
"index": "/index.html",
   
"dataGroups": [
       
{
           
"name": "api",
           
"urls": ["/api", "/v2"],
           
"cacheConfig": {
               
"maxAge": "0u",
               
"maxSize": 0,
               
"strategy": "freshness"
           
}
       
}
   
],
   
"assetGroups": [
       
{
           
"name": "app",
           
"installMode": "prefetch",
           
"resources": {
               
"files": [
                   
"/favicon.ico",
                   
"/index.html",
                   
"/manifest.webmanifest",
                   
"/*.css",
                   
"/*.js"
               
]
           
}
       
},
       
{
           
"name": "assets",
           
"installMode": "lazy",
           
"updateMode": "prefetch",
           
"resources": {
               
"files": [
                   
"/assets/**",
                   
"/*.(eot|svg|cur|webp|gif|otf|ttf|woff|woff2|ani)"
               
],
               
"urls": [
                   
"https://fonts.googleapis.com/**",
                   
"https://fonts.gstatic.com/**"
               
]
           
}
       
}
   
]
}



The random /v2/ in the dataGroups section was my last of many failed attempts to fix this problem.

I even removed all image file endings from the assetGroups section, but that did not work either.
Reply all
Reply to author
Forward
0 new messages