Skip to content

AmitM30/basic-service-worker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Service Worker Demo

license Code Climate

Demo Page

  • Load the page
  • Turn Wi-Fi / Internet off
  • Reload the page

Voila ! Page still loads. src/sw.js is where the Magic is!

Usage

Simply refer to src/sw.js script file in your site and the setup is done. To change what APIs and assets are cached, update variables CACHE_FILE_LIST and CACHE_API_LIST in src/sw.js

<script src="sw.js">

Note: service worker file needs to be served from the root of the domain, as default. This path can be changed using header Service-Worker-Allowed

About

There are multiple strategies one can implelment - network first, cache first, fastest first (race between network and cache). The choice depends upon the type of application and the situation one is trying to handle.

This application demonstrates:

  • Cache First strategy - i.e. serve from service worker cache if available
  • Cached API calls have a max age - we don't want to serve the same thing all life!
  • Version controlled cache - updates on any change to sw.js

I have tried to use cache first for API calls, and cache all the time for static assets. The API calls are cached for 5 minutes.

More

There is a lot more one can do using service worker:

  • Background data synchronization
  • Push messages
  • Responding to resource requests from other origins
  • Hooks for background services

References:

Support

Service Workers are now quite widely supported (except IE). Check caniuse for more detailed info.

Catch

The cache storage used by service workers do not get deleted by browser, ever, unlike browser cache, who have no reliability. So it is important to delete stale objects.

About

Service Worker basic demo, supports timed request caching

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published