SlideShare a Scribd company logo
1 of 25
Download to read offline
ServiceWorker と
それを取り巻くAPI
駆け足紹介
HTML5オールスターズ 06/13/2015
Kinuko Yasuda (@kinu)
kinuko@chromium.org
今日のお話
最近話題のServiceWorker を中心に
新しいAPIについて(偏った視点で)
駆け足で紹介します
* 発表時点の確認バージョンは Chrome 43
(Canary 45), Opera 30, FireFox Nightly です
* 個人的な意見を含みます。雇用者とは無関係
去年までのWeb
Cache
今年のWeb
Cache
Service
Worker
♪
Cache
API
Fetch
API
HTTPS
HTTP/2
Push
API
!
Background
Sync API
Permission
API
Streams
API
①ServiceWorker
ページの裏側で動くイベント駆動型Yet
another Javascript 環境
■ ページのセット (= scope) に対してインストー
ルされ、バックグラウンドで動作
■ 対象ページからのロードリクエストを proxy の
ように横取りして操作可能
①ServiceWorker
♪
Service
Worker
♪
‘fetch’
イベント
register
①ServiceWorker
navigator.serviceWorker.register(
‘sw.js’, {scope: ‘scope/’})
.then(function(registration) {
/* 成功! */
});
self.onfetch = function(event) {
event.respondWith(
new Response(‘from SW!’));
};
■ 最初のロードからServiceWorkerでコントロー
ルしたい:Clients.claim() (M42から)
■ スクリプトのあるディレクトリ外のページをコント
ロールしたい:
Service-Worker-Allowed ヘッダ (M42から)
■ ServiceWorkerがコントロールしているページ
の一覧が取りたい:Clients.matchAll()
(M43でgetAllから変更)
ServiceWorker細かい話
■ HTTPS だけなのでテストが面倒:
--unsafely-treat-insecure-origin-as-secure=http://url/
--user-data-dir=/tmp/dir (M44から)
■ ServiceWorker からも Performance API 使
いたい:resource timing, user timing で
workerStart が使えるように (M45から)
■ ServiceWorker をスクリプトから更新したい:
registration.update() (M45から…多分)
ServiceWorker細かい話
②Cache API
HTTPリクエスト・レスポンスのための
オフラインストレージ
■ Request オブジェクトをキーに、Response オ
ブジェクトを値に取る
Cache
Cache
API
♪
// Cache に ‘foo.png’ のレスポンスを入れる
caches.open(‘cache-name’).then(
function(cache) {
cache.add(‘foo.png’);
});
// Cache から ‘foo.png’ のレスポンスを取り出す
caches.match(‘foo.png’).then(
function(res) {
console.log(res);
});
②Cache API
Chrome, Opera ではまだ一部に polyfill 必要
global scope の仕様はまだちょっと議論中
self.oninstall = function(event) {
event.waitUntil(caches.open(‘cache’)
.then(function(cache) {
return cache.addAll([‘index.html’,...]);
}));
};
self.onfetch = function(event) {
event.respondWith(
caches.match(event.request));
};
②Cache API
ServiceWorker でのサンプル
③Fetch API
ネットワークからリソースを ‘fetch’ して
Response オブジェクトを返す
■ XHR より CORS, cache control などを
きめ細かく制御可能
Fetch
API
♪
③Fetch API
fetch('foo.txt').then(function(response) {
response.text().then(function(data) {
console.log(data);
});
});
余談:cancel をどうするかなどで揉めてる
③Fetch API
// Cache になかったものだけネットワークから取ってくる例
self.onfetch = function(ev) {
ev.respondWith(
caches.match(ev.request).then(
function(response) {
return response || fetch(ev.request);
}));
};
ServiceWorker でのサンプル
④ReadableStream
まとめてじゃなくてデータを受け取るた
びに処理したい
■ 絶賛仕様策定中: Streams API
■ バイナリ・ストリームをちゃんと扱える
■ fetch と組み合わせて response.body を
ストリームとして読み出せる
* まだまだ仕様について議論中
④ReadableStream
// レスポンスを少しずつ読み出す (あまり意味ない例)
fetch(url).then(function(res) {
var reader = res.body.getReader();
reader.read().then(
function(r) {
if (!r.done) { console.log(r.value); }
});
});
⑤Push API
ServiceWorker を使ってサーバから
のプッシュ通知を受け取る
■ GCM などを使う (実装依存)
■ タブ閉じてても受けられる
■ Chrome for Android では
Chrome 起動してなくてもOK
!
* まだ仕様少し変わったりしている
⑤Push API
navigator.serviceWorker.ready.then(
function(sw) {
sw.pushManager.subscribe().then(
function(sub) {
// endpoint をサーバに登録
});
}
);
self.onpush = function(event) {
event.waitUntil(
self.registration.showNotification(...))
};
今ネットにつながってなくても後で
つながったときに通信して欲しい
■ ツイートやメッセージをオフラインで書いて次に
つながったとき送る
■ 定期的にバックグラウンドで通信する
‘periodic sync’ の仕様も策定中
* Chrome でも --enable-service-worker-sync
--enable-experimental-web-platform-features フラグが必要
⑥Background Sync
⑥Background Sync
navigator.serviceWorker.ready.then(
function(sw) {
sw.sync.register({ tag: ‘send-later’ })
.then(function(reg) {
// 登録成功!
});
}
);
self.onsync = function(event) {
if (event.registration.tag == ‘send-later’)
event.waitUntil(sendEverythingNow());
};
その他今後来そうなもの
■ Geofencing API
■ Storage API
■ Web Bluetooth API
■ . . .
いろいろ出るのはいいけど使
うの大変 (怒)
詳しい人が作ってくれたライブラリとか
使いましょう
■ platinum-sw: ServiceWorker in Polymer
■ sw-precache: オフラインキャッシュのSWコー
ドを生成してくれるNodeモジュール
デモ的サイトなど
■ www.locchat.com - ServiceWorker +
Push API チャットサイト
■ https://jakearchibald-gcm.appspot.
com/chat/ - ServiceWorker + Push API +
Background Sync
■ ソース: https://github.
com/jakearchibald/push-api-appengine-demo
Resources
■ ServiceWorker
■ Cache API
■ Fetch API
■ Streams API
■ Fetch + Streams API
■ Push API
■ Background Sync

More Related Content

Recently uploaded

論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Danieldanielhu54
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略Ryo Sasaki
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムsugiuralab
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 

Recently uploaded (9)

論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Daniel
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Service Workerとその周辺API 駆け足紹介