I'm using angular to make PWA app.
- Angular: 12
What is going on is:
- I go to my app (online mode) -> The web app can display properly
- Turn on developer console and change connectivity to OFFLINE
- Press F5 to reload the application
What happens:
- The below image is what it displays on the screen when I hit reload:
This is my ngsw-config.json
{
"$schema": "../../node_modules/@angular/service-worker/config/schema.json",
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"manifest.json",
"/*.css",
"/*.js"
]
}
},
{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
]
}
}
]
}
- manifest.json
{
"name": "ng-main",
"short_name": "ng-main",
"theme_color": "#1976d2",
"background_color": "#fafafa",
"display": "standalone",
"scope": "./",
"start_url": "./",
"icons": [
{
"src": "assets/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-384x384.png",
"sizes": "384x384",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable any"
}
]
}
After having spent hours searching for solutions I noticed when I built my app using
ng build --configuration production
. There is onengsw.json
file inside mydist
folder.However, the
urls
andpatterns
in the generatedngsw.json
file are all empty, like the one below:When I added the urls to cache in
assetGroups
and tried again, the application worked smoothly. (Please see the content below)I already reported to Angular team about this and it has been confirmed as a bug in Angular 12.
In conclusion, now, I have to workaround by copying the
ngsw.json
file with the cached urls inassetGroups
, after having built the application, I copy thatngsw.json
to thedist
folder.