service worker.js

/*
 Copyright 2016 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

// Names of the two caches used in this version of the service worker.
// Change to v2, etc. when you update any of the local resources, which will
// in turn trigger the install event again.
const PRECACHE = 'precache-v1';
const RUNTIME = 'runtime';

// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
  'index.html',
  './', // Alias for index.html
  'styles.css',
  '../../styles/main.css',
  'demo.js'
];

// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', event => {
  event.waitUntil(
    caches.open(PRECACHE)
      .then(cache => cache.addAll(PRECACHE_URLS))
      .then(self.skipWaiting())
  );
});

// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', event => {
  const currentCaches = [PRECACHE, RUNTIME];
  event.waitUntil(
    caches.keys().then(cacheNames => {
      return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
    }).then(cachesToDelete => {
      return Promise.all(cachesToDelete.map(cacheToDelete => {
        return caches.delete(cacheToDelete);
      }));
    }).then(() => self.clients.claim())
  );
});

// The fetch handler serves responses for same-origin resources from a cache.
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', event => {
  // Skip cross-origin requests, like those for Google Analytics.
  if (event.request.url.startsWith(self.location.origin)) {
    event.respondWith(
      caches.match(event.request).then(cachedResponse => {
        if (cachedResponse) {
          return cachedResponse;
        }

        return caches.open(RUNTIME).then(cache => {
          return fetch(event.request).then(response => {
            // Put a copy of the response in the runtime cache.
            return cache.put(event.request, response.clone()).then(() => {
              return response;
            });
          });
        });
      })
    );
  }
});

4.1
10
Marc DMarco 105 points

                                    if ('serviceWorker' in navigator) {  window.addEventListener('load', function() {    navigator.serviceWorker.register('/sw.js').then(function(registration) {      // Registration was successful      console.log('ServiceWorker registration successful with scope: ', registration.scope);    }, function(err) {      // registration failed :(      console.log('ServiceWorker registration failed: ', err);    });  });}

4.1 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
javascript get a service worker javbascript get a service worker service worker means jsdom service worker what is service worker and web worker service worker new Worker service-worker.js tutorial can we write service worker tag in js file service worker in an application do what service worker guide what does service worker do in javascript javascript example service worker what is a service worker in js use a service worker sample service-worker.js what service worker can do service worker language What is a service worker? service worker.min.js use service worker in nodejs SERVICE WORKER % what can i do in service worker a service worker javascript service worker library add service worker javascript generate service worker using javascript service worker starter code js use of service worker in web application what is service worker and how its work where is service worker . js service worker in html what is service worker from service worker node.js service workers how to make a service worker service worker in html5 what are worker service what are worker services whare to find service worker js service workers javascript "this" in service worker 'this' in service worker this in service worker service worker this use service worker create a service worker javascript service worker what is service worker node what a service worker can do whats service worker what is use of service worker service worker .js pwa download service worker js] service worker é um web worker can i use service worker how to develop service worker javascript what is a service worker in javascript service worker html5 tutorial GET /service-worker.js services worker service worker hs service worker code example how to write service worker .js service worker and web worker service worker sample code service worker based website service workers nodejs service worker service-worker.js download where to put service worker what is a worker service what is a service worker used for Service worker in node js js code for service worker whats a service worker js basic service worker service worker sw.js how to create service worker create service worker js service worker web api service worker js wiki service worker state java script service worker library do i need a service worker? web worker and service worker what are service worker service worker javascript w3schools use of service worker what is the service worker and web worker explain service worker service worker call javascript function js service worker start service service worker in browser what's a worker service define service-worker.js what can we do with service worker service worker nedir whats a service worker what is a .net worker service get service worker js i don't have the service worker file in react create react app service worker how to generate service worker for my app how to register a service worker javascript make service worker in javascript service worker example javascript service worker example usage service-worker.js example when should we use service worker service worker with phtml service worker nodejs how to write a service worker service worker can i use worker service service worker script what is a service worker service worker javascript tutorial simple example on service worker service worker how to what is a service worker javascript script service worker js service workers can i use the service worker who are considered service worker example what is service worker in javascript how service worker works service worker js pwa create service worker javascript service worker html what is service-worker.js service worker~ service worker web how to use service worker service worker application service worker web worker example service worker js services worker in javsdcript hwy use service worker what is service worker jss service worker service worker js example example service-worker.js serviceWorker file google drive service worker service handler javscripy what is serviceworkers in nodejs serviceworker web how to use service worker on localhost chrome not fetching service worker service worker backgroun process javascript worker service workerr service-worker example service worker reat way to create a service worker neatly js service worker details js push technoly service workers digital scroe service worker js worker servie worker js javascript web worker ~ JavaScript SERVICE WORKERS javascript service worker template write a service worker javascript serviceworker install javascript register service worker how to write a service worker in javascript\ service worker in chrome examples of service workers advanced service worker demos service worker js example file service worker examples service worker to a web application service worker in js what is service worker in js service worker download file calling a get endpoint on the service worker calling an endpoint on the service worker open service worker from other sites (from ServiceWorker) service worker tutorial js service worker example service workers frontend or backend? inistallation code for service worker service worker code to install on android simple service worker for pwa service worker definition what is service worker.js js create service worker server workers a backend service worker worker service example service-worker.js') simple background service worker javascript google search console ServiceWorker sevice worker service worker install service worker Briefly describe the technologies used. basics of service workers how to service worker what is worker service node.js serviceworker scope html service worker what is a service worker code service worker code sample basic service worker tutorial how to check service worker is added ornot localhost:service-worker.js possible to serve js file from service worker when online service worker in online service worker docs REGISTERT SERVICE WORKER service worker chrome red cross link to service worker html what is servive worker Using Service Workers what is servudeWorker.js what is service workers how to add service worker is service worker boilerplate creating js service worker creating service worker simple service worker example mobile service worker pwa service worker service workers chrome download service worker js how can i use service worker service.worker.js service workes what is service worker js service worker template js fetch in service worker what are service workers service worketr service worker cache save credentials service worker cache save password service workerrs user credentials service worker should serveice worker be started in html file full code for an service worker service workers are not enabled chrome servuice worker window.addeventlistener or document.addeventlistener service worker Serviceworker.js add where can i install the servicewroker.js file does start ul in service worker have to have slash infront how to use service workers how to write a script to enable on and off service worker how to detecte mobile device on service worder how service worker works with http # in url in service worker what does service workers mean in development check is service worker in-app browser service worker service worker. Service Workers: an Introduction how to use a service worker connecting serviceworker with server why do i have rportwebvitals instead of service worker how to install service worker in javascript how to install serviceworker java service worker detemine if downloading introduction to service workers sw js service worker see events in ui service worker see events in page service worker events to page check service worker is loading in chrome sw.js javascript add service worker restore serviceworker.js file is report webvitals and service worker.js same online first service wokre when to use service workers does the service worker go into source control serviceworker js hiding code service worker file what is a service worker js worker service database service worker report ststus service worker report cache status service worker enquire status serviceWorker.js or reportwebvital.js javascript api to check if your getting a phonecall for pwa service worker css game solution service worker dom servie worker javascript service worker example service worker code service worker fetch in install server worker file server worker simple file chrome served from serviceworker service worker insttall time cace chrome extension service worker example service worker fetch example service workers implement appcache event model service workers appcache tutorial service worker js file download websites that use service workers websites using service workers service worker api power web application service worker not working when back online where is my service worker service workers tutorial custom-service-worker.js reasons for service worker what can i do with service worker install service worker registerWorker.js service worker .js service worker pass datta to new tab service worker javascript service worker javascript example javascript create service worker google photo service worker _track Installing in service worker install server-worker run serveice worker how to make service-worker.js service worker on server side service worker on server how service worker working on server how to create a service worker in javascript how to set up a service worker on an html page how to host a service worker at the root of your website expressjs how to host a service worker at the root of your website service-worker.js cache manager service worker is register service worker needed open service worker page in chrome website service worker service workers good service worker example good service worker service worker on install service worker for static site service workers in javascript js service worker which statments are true about serviceworker o serviceWorker.js where to find servise-worker.js file service worker.js serviceWorker.js simple service worker service worker serviceworker javascript online example of web site with service worker example of web site with service worker example web site with service worker good service worker code example good service worker code serviceworker js intro node js service worker reinstall service worker how to create a service-worker in node how to install service worker in word javascript service node js service worker example JavaScript Service Workers service worker example file service workers examples nodejs service worker qu 'est ce que service worker in react get installed service worker version jquery service worker service worker node js serviceworkers js how does service worker called in html5 chrome version support service worker serviceworker js #628 services worker javascript local service worker for javascript i deleted service worker.js file service workers js register service worker javascript js service worker tutorial javascript service worker access html from service worker service worker in javascript service worker javascript service worker example service worker js
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source