The Service-worker configuration file is the Angular Service worker itself. Just like other service workers, it gets delivered via its separate HTTP request so that the browser can track if it has changed or updated and can apply it to the Service Worker Lifecycle. Note that the Angular Service worker file ngsw-worker.js will always be the same for each build since it is get copied from the node modules directly. This file will remain unchanged until we do not perform any changes into the Angular version which may contain the new version of the Angular Service Worker.
ngsw.json file is the runtime configuration file, which is mainly used by the Angular Service Worker. This file is mainly built based on the ngsw-config.json file and it contains all the information needed by the Angular Service Worker to identify at runtime which files need to be cached and when.
How to Enable Service Worker
To enable or setup the Angular Service worker in any angular application-based project, we need to perform the following steps which take care the configuring our application to use the service workers by including the service-worker package into the application.
Step 1
Add the @angular/service-worker package to our application by using npm install command
Step 2
We need to enable the service worker to build support in the CLI.
Step 3
Imports and register the service worker in the app module.
Step 4
Create the service worker configuration file called ngsw-config.json which mainly specify all caching behaviors and other setting related to the service worker.
- {
- "$schema": "./node_modules/@angular/service-worker/config/schema.json",
- "index": "/index.html",
- "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|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
- ]
- }
- }
- ]
- }
Step 5
In the app.module.ts file, we already added the ServiceWorkerModule and enabled it for production only.
The module also refers to a file called ngsw-worker.js. This file is built by the CLI in production mode. Angular uses the configuration file for this process. In development mode, however, this file is not served so we should not enable the service worker for the development mode.
Now, we need to build the application by using the command –
Now, the Angular Application project is ready to use the Angular Service Worker.
Because ng serve does not work with service workers, we must need to use a separate HTTP server to test our application locally. We can use any HTTP server. For example, we can use the http-server package from npm. To reduce the possibility of conflicts and avoid serving stale content, test on a dedicated port, and disable caching.
To serve the directory containing your web files with http-server, run the following command:
- http-server -p 8080 -c-1 dist/<project-name>
With the server running, we can point your browser at http://localhost:8080/. Our application should load normally.
Now the If you want to block the network connection so that the chrome browser does not have any network access.
For applications that do not use the Angular service worker, refreshing now would display Chrome's Internet disconnected page that says "There is no Internet connection". With the addition of an Angular service worker, the application behavior changes. On a refresh, the page loads normally.
If you look at the Network tab, you can verify that the service worker is active.
Notice that under the "Size" column, the requests state is (from Service-Worker). This means that the resources are not being loaded from the network. Instead, they are being loaded from the service worker's cache.
Conclusion
In this article, we discussed how to implement the service-worker using the Angular Framework in any web application. Also, we discussed the basic concept of service workers along with configuration, how to enable, etc. I hope, this article will help you. Any feedback or query related to this article is most welcome.