1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PWAだからServiceWorker大切

Posted at

前回
「PWA初期化」
https://qiita.com/sanoh/items/430a50035d4dfab0de7d
でPWAなのにServiceWorkerが入っていなかったので追加。

■Step1:index.htmlの修正

index.html
<!-- index.html -->
<html>
<head>
    <title>Hello ServiceWorker.</title>
    <link rel="manifest" href="./manifest.json">
    <script>
        if ('serviceWorker' in navigator) {
          navigator.serviceWorker.register('./service-worker.js').then(function() { console.log('Service Worker Registered'); });
       }
    </script>    
</head>
<body>
    <h1>Hello ServiceWorker!</h1>
</body>
</html>

■Step2:ServicWorkerのJSを追加

service-worker.js
// service-worker.js
self.addEventListener('install', function(e) {
  console.log('[ServiceWorker] Install');
});
  
self.addEventListener('activate', function(e) {
  console.log('[ServiceWorker] Activate');
});
  
self.addEventListener('fetch', function(event) {
});

これでServiceWorkerが使えるようになりました。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?