Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Service Worker registration

// Add the script to your 'index.html' and ...
<script>
  (function() {
    if("serviceWorker" in navigator) {
      navigator.serviceWorker.register("/service-worker.js", {scope: '/'})
        .then(() => console.log("Service Worker registered successfully."))
        .catch(error => console.log("Service Worker registration failed:", error));
    }
  })();
  </script>
// don't forget that works only in 'https://' or 'localhost:port' mode so
// if you don't run any server like express.js then install:
// "webpack-dev-server" from npm and add the following line to your package.json
// "scripts: { "start:dev": "webpack-dev-server" }
//          npm run start:dev
// it will run your app in 'localhost:8080'
Source by www.creativebloq.com #
 
PREVIOUS NEXT
Tagged: #Service #Worker #registration
ADD COMMENT
Topic
Name
6+5 =