0
0

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.

ServiceWorcker試したいときはこうするというメモ

0
Last updated at Posted at 2015-04-09

Chrome V41以上

/index.html
<script type="text/javascript">
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/sw.js', {scope: '/'}).then(function(registration) {
        // 登録成功
        console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }).catch(function(err) {
        // 登録失敗 :(
        console.log('ServiceWorker registration failed: ', err);
    });
}
</script>
/sw.js
self.addEventListener('fetch', function(event) {
	event.respondWith(
		caches.match(event.request).then(function(response) {
			// キャッシュがあったのでそのレスポンスを返す
			if (response) {
			  return response;
			}

			return fetch(event.request);
		})
	);
});
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?