SlideShare a Scribd company logo
1 of 85
Download to read offline
!
"# $
%&
オフラインファーストとは
' ( ) * +
#
, &
&&
-
- &
&&
var value = localStorage['key'];
var json = JSON.parse(value);
localStorage['key'] = JSON.stringify(json);
' ( ) * +
#
, -
--
--
- -
-
var db;
var objectStore;
var req = window.indexedDB.open('dbname', 3);
req.onsuccess = function(event) {
db = event.target.result;
};
req.onupgradeneeded = function(event) {
db = event.target.result;
objectStore = db.createObjectStore('name', {
keyPath: 'key'
});
};
' ( ) * +
#
, -
--
--
- -
-
<html manifest=app.mf>
CACHE MANIFEST
# Resource to cache
CACHE:
index.html
stylesheet.css
image.png
script.js
# Resource to bypass
NETWORK:
login.php
/api/data
# Fallback URLs
FALLBACK:
/ /offline.html
if (navigator.onLine) {
$.ajax({
url: '/api/data',
method: 'get'
}).done(function (data) {
render(data);
});
} else {
var cacheData = localStorage.getItem('key');
render(JSON.parse(cacheData));
}
var sw = navigator.serviceWorker;
sw.register('sw.js').then(function (res) {
console.log('sw.js is installed!');
}, function (error) {
console.log('sw.js is not installed...');
});
self.addEventListener('fetch', function (e) {
console.log('Request in Browser');
e.respondWith(
new Response('Not Found', {
status: 404
})
);
});
fetch('/url/json').then(function (res) {
return res.json();
}).then(function (json) {
console.log(json);
});
caches.open('cache-key').then(function(cache) {
return cache.addAll([
'js/app.js',
'css/app.css',
'img/image.png'
]);
});
caches.match(e.request).then(function (res) {
return res;
});
var sw = navigator.serviceWorker;
sw.ready.then(function(reg) {
reg.syncManager.register({
id: "periodicSync",
minDelay: 60 * 60 * 1000,
minPeriod: 12 * 60 * 60 * 1000,
allowOnBattery: true
idleRequired: false
}).then(function() {
// Success
});
});
var sw = navigator.serviceWorker;
sw.ready.then(function(reg) {
reg.pushManager.subscribe().then(
function(subscription) {
// Success
console.log(subscription.subscriptionId);
console.log(subscription.endpoint);
}
});
});
' ( ) * +
#
,
-
-
-
&&
&
-
e.respondWith(
caches.open(CACHE_KEY).then(function (cache) {
return cache.match(e.request)
.then(function (response) {
if (response) {
return response;
} else {
return fetch(e.request.clone())
.then(function (res) {
cache.put(e.request, res.clone());
return res;
});
}
});
})
);
.
!
/

More Related Content

What's hot

Java script.trend(spec)
Java script.trend(spec)Java script.trend(spec)
Java script.trend(spec)dynamis
 
Symfony2でMongoDBと仲良くする方法
Symfony2でMongoDBと仲良くする方法Symfony2でMongoDBと仲良くする方法
Symfony2でMongoDBと仲良くする方法Koji Iwazaki
 
Dennis zapana perez
Dennis zapana perezDennis zapana perez
Dennis zapana perezdennis_elvis
 
Php codigos interfaces fredy guzman cusihunca
Php codigos interfaces   fredy guzman cusihuncaPhp codigos interfaces   fredy guzman cusihunca
Php codigos interfaces fredy guzman cusihuncaTigger_Fred
 
Sumahexavector
SumahexavectorSumahexavector
Sumahexavectorjbersosa
 
Modern Mobile Web Apps
Modern Mobile Web AppsModern Mobile Web Apps
Modern Mobile Web Appsdynamis
 
Formulario
FormularioFormulario
Formulariotukisele
 
Func
FuncFunc
FuncHvqr
 
jQuery - Javascript para quem não sabe Javascript
jQuery - Javascript para quem não sabe JavascriptjQuery - Javascript para quem não sabe Javascript
jQuery - Javascript para quem não sabe JavascriptNando Vieira
 
Php & mysql
Php & mysqlPhp & mysql
Php & mysqlola98z
 
Documentacion edderson callpa_ortiz
Documentacion edderson callpa_ortizDocumentacion edderson callpa_ortiz
Documentacion edderson callpa_ortizEdderson J. Ortiz
 

What's hot (20)

Clase 10 electiva profesional 3 aws rds php y mysql
Clase 10 electiva profesional 3 aws rds php y mysqlClase 10 electiva profesional 3 aws rds php y mysql
Clase 10 electiva profesional 3 aws rds php y mysql
 
Jsoon
JsoonJsoon
Jsoon
 
Java script.trend(spec)
Java script.trend(spec)Java script.trend(spec)
Java script.trend(spec)
 
Sis quiz
Sis quizSis quiz
Sis quiz
 
Symfony2でMongoDBと仲良くする方法
Symfony2でMongoDBと仲良くする方法Symfony2でMongoDBと仲良くする方法
Symfony2でMongoDBと仲良くする方法
 
Dennis zapana perez
Dennis zapana perezDennis zapana perez
Dennis zapana perez
 
Php codigos interfaces fredy guzman cusihunca
Php codigos interfaces   fredy guzman cusihuncaPhp codigos interfaces   fredy guzman cusihunca
Php codigos interfaces fredy guzman cusihunca
 
Sumahexavector
SumahexavectorSumahexavector
Sumahexavector
 
SmartCSS
SmartCSSSmartCSS
SmartCSS
 
Add tag shortcode
Add tag shortcodeAdd tag shortcode
Add tag shortcode
 
Modern Mobile Web Apps
Modern Mobile Web AppsModern Mobile Web Apps
Modern Mobile Web Apps
 
Formulario
FormularioFormulario
Formulario
 
Miniray.php
Miniray.phpMiniray.php
Miniray.php
 
Wek14 mysql 2
Wek14 mysql 2Wek14 mysql 2
Wek14 mysql 2
 
Func
FuncFunc
Func
 
jQuery - Javascript para quem não sabe Javascript
jQuery - Javascript para quem não sabe JavascriptjQuery - Javascript para quem não sabe Javascript
jQuery - Javascript para quem não sabe Javascript
 
Php & mysql
Php & mysqlPhp & mysql
Php & mysql
 
Documentacion edderson callpa_ortiz
Documentacion edderson callpa_ortizDocumentacion edderson callpa_ortiz
Documentacion edderson callpa_ortiz
 
Index2
Index2Index2
Index2
 
Phpex3
Phpex3Phpex3
Phpex3
 

Viewers also liked

Web若手エンジニアの会(仮)
Web若手エンジニアの会(仮)Web若手エンジニアの会(仮)
Web若手エンジニアの会(仮)Naoyuki Kataoka
 
レンタルサーバで今すぐ始めるWEB開発
レンタルサーバで今すぐ始めるWEB開発レンタルサーバで今すぐ始めるWEB開発
レンタルサーバで今すぐ始めるWEB開発Naoyuki Kataoka
 
Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス
 Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス
Objective-CのBlocksの循環参照に関する僕なりのベストプラクティスNaoyuki Kataoka
 
Php入門
Php入門Php入門
Php入門dcubeio
 
GoogleTagManagerを使ってタグ運用を楽にしませんか?
GoogleTagManagerを使ってタグ運用を楽にしませんか?GoogleTagManagerを使ってタグ運用を楽にしませんか?
GoogleTagManagerを使ってタグ運用を楽にしませんか?dcubeio
 
[D3]サーバーレスでサービスを作ってみた話
[D3]サーバーレスでサービスを作ってみた話[D3]サーバーレスでサービスを作ってみた話
[D3]サーバーレスでサービスを作ってみた話dcubeio
 
React NativeでTwitterクライアントを作ってみよう
React NativeでTwitterクライアントを作ってみようReact NativeでTwitterクライアントを作ってみよう
React NativeでTwitterクライアントを作ってみようdcubeio
 
シロク流事業の立ち上げ方の成功と失敗
シロク流事業の立ち上げ方の成功と失敗シロク流事業の立ち上げ方の成功と失敗
シロク流事業の立ち上げ方の成功と失敗Naoyuki Kataoka
 
Web Components 2016 & Polymer v2
Web Components 2016 & Polymer v2Web Components 2016 & Polymer v2
Web Components 2016 & Polymer v2Shogo Sensui
 
覚えて帰ろうJavaデザインパターン
覚えて帰ろうJavaデザインパターン覚えて帰ろうJavaデザインパターン
覚えて帰ろうJavaデザインパターンdcubeio
 
Jenkinsを使った初めての継続的インテグレーション
Jenkinsを使った初めての継続的インテグレーションJenkinsを使った初めての継続的インテグレーション
Jenkinsを使った初めての継続的インテグレーションdcubeio
 
【D3 公開用】ドメイン駆動設計とscala 〜既存プロジェクトへの適用〜
【D3 公開用】ドメイン駆動設計とscala 〜既存プロジェクトへの適用〜 【D3 公開用】ドメイン駆動設計とscala 〜既存プロジェクトへの適用〜
【D3 公開用】ドメイン駆動設計とscala 〜既存プロジェクトへの適用〜 dcubeio
 
Introduction to Resource Hints
Introduction to Resource HintsIntroduction to Resource Hints
Introduction to Resource HintsShogo Sensui
 
成長する組織を支えるシロクの自動化
成長する組織を支えるシロクの自動化成長する組織を支えるシロクの自動化
成長する組織を支えるシロクの自動化Naoyuki Kataoka
 
初心者のためのWeb標準技術
初心者のためのWeb標準技術初心者のためのWeb標準技術
初心者のためのWeb標準技術Shogo Sensui
 
React を導入した フロントエンド開発
React を導入したフロントエンド開発React を導入したフロントエンド開発
React を導入した フロントエンド開発 daisuke-a-matsui
 
Flux react現状確認会
Flux react現状確認会Flux react現状確認会
Flux react現状確認会VOYAGE GROUP
 
なぜ人は必死でjQueryを捨てようとしているのか
なぜ人は必死でjQueryを捨てようとしているのかなぜ人は必死でjQueryを捨てようとしているのか
なぜ人は必死でjQueryを捨てようとしているのかYoichi Toyota
 
Web フロントエンドの変遷とこれから
Web フロントエンドの変遷とこれからWeb フロントエンドの変遷とこれから
Web フロントエンドの変遷とこれからShogo Sensui
 

Viewers also liked (20)

Web若手エンジニアの会(仮)
Web若手エンジニアの会(仮)Web若手エンジニアの会(仮)
Web若手エンジニアの会(仮)
 
レンタルサーバで今すぐ始めるWEB開発
レンタルサーバで今すぐ始めるWEB開発レンタルサーバで今すぐ始めるWEB開発
レンタルサーバで今すぐ始めるWEB開発
 
Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス
 Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス
Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス
 
Php入門
Php入門Php入門
Php入門
 
GoogleTagManagerを使ってタグ運用を楽にしませんか?
GoogleTagManagerを使ってタグ運用を楽にしませんか?GoogleTagManagerを使ってタグ運用を楽にしませんか?
GoogleTagManagerを使ってタグ運用を楽にしませんか?
 
[D3]サーバーレスでサービスを作ってみた話
[D3]サーバーレスでサービスを作ってみた話[D3]サーバーレスでサービスを作ってみた話
[D3]サーバーレスでサービスを作ってみた話
 
React NativeでTwitterクライアントを作ってみよう
React NativeでTwitterクライアントを作ってみようReact NativeでTwitterクライアントを作ってみよう
React NativeでTwitterクライアントを作ってみよう
 
シロク流事業の立ち上げ方の成功と失敗
シロク流事業の立ち上げ方の成功と失敗シロク流事業の立ち上げ方の成功と失敗
シロク流事業の立ち上げ方の成功と失敗
 
Web Components 2016 & Polymer v2
Web Components 2016 & Polymer v2Web Components 2016 & Polymer v2
Web Components 2016 & Polymer v2
 
覚えて帰ろうJavaデザインパターン
覚えて帰ろうJavaデザインパターン覚えて帰ろうJavaデザインパターン
覚えて帰ろうJavaデザインパターン
 
Jenkinsを使った初めての継続的インテグレーション
Jenkinsを使った初めての継続的インテグレーションJenkinsを使った初めての継続的インテグレーション
Jenkinsを使った初めての継続的インテグレーション
 
【D3 公開用】ドメイン駆動設計とscala 〜既存プロジェクトへの適用〜
【D3 公開用】ドメイン駆動設計とscala 〜既存プロジェクトへの適用〜 【D3 公開用】ドメイン駆動設計とscala 〜既存プロジェクトへの適用〜
【D3 公開用】ドメイン駆動設計とscala 〜既存プロジェクトへの適用〜
 
Introduction to Resource Hints
Introduction to Resource HintsIntroduction to Resource Hints
Introduction to Resource Hints
 
成長する組織を支えるシロクの自動化
成長する組織を支えるシロクの自動化成長する組織を支えるシロクの自動化
成長する組織を支えるシロクの自動化
 
初心者のためのWeb標準技術
初心者のためのWeb標準技術初心者のためのWeb標準技術
初心者のためのWeb標準技術
 
Effective ES6
Effective ES6Effective ES6
Effective ES6
 
React を導入した フロントエンド開発
React を導入したフロントエンド開発React を導入したフロントエンド開発
React を導入した フロントエンド開発
 
Flux react現状確認会
Flux react現状確認会Flux react現状確認会
Flux react現状確認会
 
なぜ人は必死でjQueryを捨てようとしているのか
なぜ人は必死でjQueryを捨てようとしているのかなぜ人は必死でjQueryを捨てようとしているのか
なぜ人は必死でjQueryを捨てようとしているのか
 
Web フロントエンドの変遷とこれから
Web フロントエンドの変遷とこれからWeb フロントエンドの変遷とこれから
Web フロントエンドの変遷とこれから
 

More from Shogo Sensui

Web Standards Interop 2022
Web Standards Interop 2022Web Standards Interop 2022
Web Standards Interop 2022Shogo Sensui
 
Introduction to Performance APIs
Introduction to Performance APIsIntroduction to Performance APIs
Introduction to Performance APIsShogo Sensui
 
Web Standards 2018
Web Standards 2018Web Standards 2018
Web Standards 2018Shogo Sensui
 
The State of Web Components
The State of Web ComponentsThe State of Web Components
The State of Web ComponentsShogo Sensui
 
Component of Web Frontend
Component of Web FrontendComponent of Web Frontend
Component of Web FrontendShogo Sensui
 
これからのJavaScriptの話
これからのJavaScriptの話これからのJavaScriptの話
これからのJavaScriptの話Shogo Sensui
 
We should optimize images
We should optimize imagesWe should optimize images
We should optimize imagesShogo Sensui
 
Web Components changes Web Development
Web Components changes Web DevelopmentWeb Components changes Web Development
Web Components changes Web DevelopmentShogo Sensui
 
Re-think about Web Performance
Re-think about Web PerformanceRe-think about Web Performance
Re-think about Web PerformanceShogo Sensui
 
Browser Computing Structure
Browser Computing StructureBrowser Computing Structure
Browser Computing StructureShogo Sensui
 
Brush up your Coding! 2013 winter
Brush up your Coding! 2013 winterBrush up your Coding! 2013 winter
Brush up your Coding! 2013 winterShogo Sensui
 
Brush up your Coding!
Brush up your Coding!Brush up your Coding!
Brush up your Coding!Shogo Sensui
 
Functional JavaScript with Lo-Dash.js
Functional JavaScript with Lo-Dash.jsFunctional JavaScript with Lo-Dash.js
Functional JavaScript with Lo-Dash.jsShogo Sensui
 

More from Shogo Sensui (13)

Web Standards Interop 2022
Web Standards Interop 2022Web Standards Interop 2022
Web Standards Interop 2022
 
Introduction to Performance APIs
Introduction to Performance APIsIntroduction to Performance APIs
Introduction to Performance APIs
 
Web Standards 2018
Web Standards 2018Web Standards 2018
Web Standards 2018
 
The State of Web Components
The State of Web ComponentsThe State of Web Components
The State of Web Components
 
Component of Web Frontend
Component of Web FrontendComponent of Web Frontend
Component of Web Frontend
 
これからのJavaScriptの話
これからのJavaScriptの話これからのJavaScriptの話
これからのJavaScriptの話
 
We should optimize images
We should optimize imagesWe should optimize images
We should optimize images
 
Web Components changes Web Development
Web Components changes Web DevelopmentWeb Components changes Web Development
Web Components changes Web Development
 
Re-think about Web Performance
Re-think about Web PerformanceRe-think about Web Performance
Re-think about Web Performance
 
Browser Computing Structure
Browser Computing StructureBrowser Computing Structure
Browser Computing Structure
 
Brush up your Coding! 2013 winter
Brush up your Coding! 2013 winterBrush up your Coding! 2013 winter
Brush up your Coding! 2013 winter
 
Brush up your Coding!
Brush up your Coding!Brush up your Coding!
Brush up your Coding!
 
Functional JavaScript with Lo-Dash.js
Functional JavaScript with Lo-Dash.jsFunctional JavaScript with Lo-Dash.js
Functional JavaScript with Lo-Dash.js
 

Introduction to Service Worker