Boost Web Site with Service Worker – Part 2

Kapil Gupta
Latest posts by Kapil Gupta (see all)

<< Part 1      >> Part 3       >> Part 4       >> Azure        >> O365      Download code

In previous part we saw what service worker is and why should we use it. lets take it forward and see how service worker works.

How service worker works

You will first need to register it to your website by registering it in JavaScript on page, this allows browser to start the worker installation step in background.

During the installation step, you can cache some static assets, if there is any missing file during the cache, the installation step fails and also does not activate the service worker. However it does try to install it again the next time.

After installation step is activation step is called, and service worker takes care of all the web pages that are in its scope. when page is loaded after refresh, and if it is registered with service worker then it is served by it for response.

A service worker can be in two states one is active and other is terminated. In active state it handles messages and network requests through fetch and message events and in terminated state it is not in use to save memory.

 

Service Worker Uses JavaScript Promises

A promise is an abstraction for asynchronous programming. It’s an object that proxies for the return value or the exception thrown by a function that has to do some asynchronous processing.

A promise can only succeed or fail once. It cannot succeed or fail twice, neither can it switch from success to failure or vice versa.

Service Worker Uses Fetch API

Fetch provides a generic definition of Request and Response objects and other things involved with network requests.

This will allow them to be used wherever they are needed in the future, whether it’s for service workers, cache API and other similar things that handle or modify requests and responses, or any kind of use case that might require you to generate your own responses programmatically.

Uses Cache API

Cache API allow authors to fully and conveniently manage their content caches for offline use.

Uses App Cache

The Application Cache or App Cache allows a developer to specify which files the browser should cache and make available to offline users.

Your app will load and work correctly, even if the user presses the refresh button while they’re offline. following are few properties of app cache

Files always come from app cache, even if you’re online.
The app cache only updates if the content of the manifest itself has changed.
The app cache is an additional cache, not at alternative one.
Never ever ever far-future cache the manifest.

Browser Support for Service Worker

Following is a short summary of browser support for service worker.

 

<< Part 1      >> Part 3      >> Part 4       >> Azure        >> O365      Download code


Check Articles From Categories      Health and Parenting      Inspiring Stories      Technology      Microsoft Azure      SharePoint O365

Leave a Reply

Your email address will not be published. Required fields are marked *