ãŸããã
ä»æ¥ã®ãµãŒãã¹ã¯ãŒã«ãŒïŒService Workerãã容赊ãã ããïŒã¯ããµã€ãã®åºæ¬æ©èœãžã®äŸ¿å©ãªè¿œå æ©èœã§ããããã§ã¯ããªãã©ã€ã³äœæ¥ãããã¯ã°ã©ãŠã³ãããŒã¿ã®åæããã¬ã³ãã£ãªããã·ã¥éç¥ãèŠã€ããããšãã§ããŸãã
ãã ãããµãŒãã¹ã¯ãŒã«ãŒã«é¢ããå€ãã®èšäºã¯ãããªãç°¡æœã§ãç°¡åãªäŸã説æããŠããŸãã ãµãŒãã¹ã¯ãŒã«ãŒã®äœæ¥ã®ããã€ãã®æ©èœã«æ³šæãåããããšãããããåºæ¬çãªç¥èãå¿ èŠã§ãã åºçºç¹ã¯ã ãã®èšäº ïŒ ç¿»èš³ ïŒãŸãã¯ããå°ã詳现ãªèšäºã§ãã
åããã¡ã€ã³äžã®è€æ°ã®ãµãŒãã¹ã¯ãŒã«ãŒ
ç¹å®ã®ãµãŒãã¹ã¯ãŒã«ãŒã®ç»é²ã«ã¯ã ã¹ã³ãŒããªã©ã®æŠå¿µããããŸã ã ç¹å®ã®ãã¡ã€ã³ã®ã©ã®ããŒãžããã®ç®¡çäžã«å ¥ããã決å®ããŸãã ãã®å Žåãåããã¡ã€ã³ã«è€æ°ã®Service Workerãç»é²ã§ããŸãããã¹ã³ãŒãã¯ç°ãªããŸãã ããããå¥ã®ååã§ç»é²ããããšãããšã1ã€ã®ã¹ã³ãŒãã§ããã®åŸã«ã€ã³ã¹ããŒã«ãããã¯ãŒã«ãŒã圌ã®åã®å åŒãã眮ãæãããŸãã
ãšããã§ãäžèšã®ãã¹ã®ãµãŒãã¹ã¯ãŒã«ãŒãšããŠæå®ããããã¹ã«ãã¡ã€ã«ãã€ã³ã¹ããŒã«ããããã«ïŒãã®åäœã¯ããã©ã«ãã§ã¯çŠæ¢ãããŠããŸãããã¹ãå¢ããããããã¹ãæžããããã§ããŸãïŒã Service-Worker-Allowed http-headerã䜿çšã§ããŸãã
ãã©ã¡ãŒã¿ãŒãªãã®getRegistrationïŒïŒã¡ãœããã¯ãçŸåšã®ããŒãžã«é©ãããããããéã¢ã¯ãã£ããªService Workerç»é²ãè¿ããŸãã ãŸãããã¹ãããããã¹ã«æ²¿ã£ãŠãããé©åã§ãªãå Žåã¯åãç»é²ãåãåãããšãæå³ããŸãã è€æ°ã®ãµãŒãã¹ã¯ãŒã«ãŒãåããã¡ã€ã³ã§äœæ¥ãããšäºæ³ãããå Žåãããã¯é©ãã«ã€ãªããå¯èœæ§ããããŸãã
äŸãèŠãŠã¿ãŸãããïŒã¹ã³ãŒã/ãæã€ã€ã³ã¹ããŒã«ãããService WorkerããããŸãã ããããã¥ãŒã¹ãµã€ãã«ããŠããªãã©ã€ã³çã®ããã¹ããæäŸããŸãã ãã¹/ admin /ã«æ²¿ã£ãŠãç¬èªã®ãµãŒãã¹ã¯ãŒã«ãŒãåããã³ã³ãããŒã«ããã«ããããŸãã 2çªç®ã®ãµãŒãã¹ã¯ãŒã«ãŒããŸã ã€ã³ã¹ããŒã«ãããŠããªãå ŽåãgetRegistarationïŒïŒã¯æåã®ãµãŒãã¹ã¯ãŒã«ãŒã®ç»é²ãè¿ããŸããããã«ãããšã©ãŒãçºçããå¯èœæ§ããããŸãïŒããšãã°ã管çããã«ãããµãŒãã¹ã¯ãŒã«ãŒã«éç¥ãéä¿¡ããŸãã
getRegistrationã«ã¯ãªãã·ã§ã³ã®ãã©ã¡ãŒã¿ãŒ-scopeããããŸãã æå®ãããšãã¡ãœããã¯æž¡ãããã¹ã³ãŒãã«æé©ãªïŒå¿ ãããçãããªãïŒç»é²ãè¿ããŸãã ãããã£ãŠããµãããŒãžã§ãµãŒãã¹ã¯ãŒã«ãŒã®ç»é²ã解é€ããããçŸåšã®ãã¡ã€ã³ããç»é²ãåãåãããšãã§ããŸããé©åãªç¯å²ãç¥ãå¿ èŠããããŸãã
ãããŠããã¹ãŠã®ã¹ã³ãŒããã€ãŸãgetRegistrationsïŒïŒã¡ãœãããããããªãå Žåã¯ãçŸåšã®ãã¡ã€ã³ããã®ãã¹ãŠã®ç»é²ãåã«é åãšããŠè¿ããŸãã FirefoxãŸãã¯Chrome 45以éãå¿ èŠã§ãã
ããŒãžãšService Workeréã®ãªã³ã¯
ãµãŒãã¹ã¯ãŒã«ãŒãšãµãããŒãžéã§ããŒã¿ã亀æã§ããå¯èœæ§ããããããå ã®äœæ¥ã¹ããŒã ã«è³ãå¯èœæ§ããããŸãã ããšãã°ããã£ãã·ã¥ããããŒã¿ãããã«éä¿¡ããªãããåæã«æ°ããããŒã¿ãèŠæ±ã§ããŸãã æ°ããããŒã¿ããããšããã«-ãã£ãã·ã¥ã«å ¥ããŠããŒãžã«éä¿¡ããŸãã
serviceworke.rsã®äŸã¯ ããµãŒãã¹ã¯ãŒã«ãŒãšç°¡åã«éä¿¡ããæ¹æ³ã瀺ããŠããŸãã
navigator.serviceWorker.controller.postMessage(message.value);
ããã§ãã³ã³ãããŒã©ãŒã¯ããŒãžãå¶åŸ¡ãããµãŒãã¹ã¯ãŒã«ãŒã§ãã æ°ãããã©ãŠã¶ïŒFirefoxããã³Chrome 51以éã®ãã¹ãŠã®ããŒãžã§ã³ïŒã§ã¯ã次ã®ãªã¯ãšã¹ãã«ç°¡åã«çããããšãã§ããŸãã
self.addEventListener('message', function (event) { event.source.postMessage('response'); });
å€ãããŒãžã§ã³ã§ã¯ããã¹ãŠã®ã¿ãã移åããŠé©åãªã¿ããèŠã€ããããMessageChannelã®æã§äœæããå¿ èŠããããŸããã ãŸãããã§ããã€ãã³ãããã¿ãã«ã¡ãã»ãŒãžãéä¿¡ã§ããããã«ãªããŸããã ããã«ã€ããŠã¯ãææ°ã®APIãæ¢ã«ããããšãé€ããŠã èšäºã§èª¬æãããŠããŸãã
ãã1ã€ã®ãã€ã³ãã¯ãService Workerã§ã®ããŒã¿ã®ä¿åã§ãã ãã§ã«ãµãŒãã¹ã¯ãŒã«ãŒãè©Šãã人ã¯ãLocalStorageãããã«ãªãããšã«æ°ä»ããŠãããããããŸããã ããã¯ããµãŒãã¹ã¯ãŒã«ãŒã®ã³ãŒã¹ãå®å šã«éåæã®APIã§è¡ãããããã§ãïŒ importScriptsã®äŸå€ãé€ããŸã ïŒã ããããå éšã¯ãŸã å©çšå¯èœã§ãïŒ
- ãã£ãã·ã¥
- indexedDB
- ã¯ãŒã«ãŒã®ã³ã³ããã¹ãã§å®£èšãããå€æ°ã®ã¿ïŒãã ãããããã¯çåœã§ããããµãŒãã¹ã¯ãŒã«ãŒãåæ¢ãããšå¿ããããŸãïŒ
ãã£ãã·ã¥ãšindexedDBã¯ã©ã¡ããããŒãžäžã§éåžžã®æ¹æ³ã§ã¢ã¯ã»ã¹ã§ããã¯ãŒã«ãŒãšå®å šã«ããŒã¿ãå ±æããŸãã åã®æ®µèœãåç §ãããšãåããªãªãžã³ã®è€æ°ã®ãµãŒãã¹ã¯ãŒã«ãŒãããŒã¿ãå ±æãããšããçµè«ã«éããããšãã§ããŸãã ãã®å Žåãå¥ã®ãµãŒãã¹ã¯ãŒã«ãŒã®ãã£ãã·ã¥ãããããªãã§ãã ãããããšãã°ã次ã®ããã«ãã¬ãã£ãã¯ã¹ã§ãã§ãã¯ããŸãã
var CACHE_PREFIX = 'my-page-'; var CACHE_VERSION = 1; var CACHE_NAME = CACHE_PREFIX + CACHE_VERSION; self.addEventListener('activate', function(event) { event.waitUntil( caches.keys().then(function (cacheNames) { return Promise.all( cacheNames.map(function (cacheName) { if (cacheName.indexOf(CACHE_PREFIX) === 0) { return caches.delete(cacheName); } }) ); }) ); });
ãŸãã¯ãã®ãããªãã®ã§ããããã®å Žåãå¯èœãªãã£ãã·ã¥ã®å®å šãªãªã¹ãã1ãæã«ä¿æããå¿ èŠããããŸãã
ããããããããã¹ãŠã«ãããã¹ãã¬ãŒãžå ã®ããŒã¿ã®100ïŒ ã®å®å šæ§ãä¿èšŒãã人ã¯ããªãããšãèŠããŠãã䟡å€ããããŸãã ååãªãã£ã¹ã¯å®¹éããªãå Žåããã©ãŠã¶ã¯CacheStorageãšindexedDBãèªåçã«æ¶å»ã§ããŸãããŠãŒã¶ãŒã¯èªåã§å®è¡ã§ããŸãã
ã¯ãã¹ãã¡ã€ã³ãªã¯ãšã¹ãããã³ä»ã®ãã¡ã€ã³ãšã®ãã®ä»ã®ããåã
ãã§ããã®å°å ¥ã«ãããç¶æ³ã¯å°ãæ··ä¹±ããŠããããã«èŠãããããããŸããïŒç°ãªãèŠæ±/å¿çã¢ãŒãããããŸãïŒããµãŒãã¹ã¯ãŒã«ãŒã§ã¯ããã¹ãŠã2åé£ãããªããŸãã
æãç°¡åã«ç解ã§ããã®ã¯ãCORSããããŒããããããããŒãªãã§å¥ã®ãã¡ã€ã³ã®ã³ã³ãã³ãã«ã¢ã¯ã»ã¹ããããšã§ãã 2ã€ã®ã¿ã€ãã®äœ¿çšãåé¢ããããšãéèŠã§ãïŒjavascriptã®ãµã€ãããã®ã¢ã¯ã»ã¹ãããšãªãã ããšãã°ãåé¡ãªã1ã€ã®ç»åãå¥ã®ç»åã«çœ®ãæããããšãã§ããŸãããã§ãããµãŒãã¹ã¯ãŒã«ãŒã¢ãŒãã§ãno-corsããšæå®ããã ãã§ãããããŒãäœã§ãããã¯é¢ä¿ãããŸããã ãno-corsãã䜿çšããªãå Žåããã§ããã¯CORSããããŒãåŸ æ©ããããããŒããªãå Žåããã¹ãŠã倱æããŸãã
ããå³å¯ã«ã¯ãããŒãžããã®ãªã¯ãšã¹ãã«ã¯ã¢ãŒãããããŸãã ããšãã°ãç»åãªã¯ãšã¹ãã¯ãno-corsãã§ãããcrossOriginå±æ§ïŒå¿åãŸãã¯äœ¿çšè³æ Œæ å ±ïŒãæã€ç»åãªã¯ãšã¹ãã¯ãã§ã«ãcorsãã§ãã XMLHttpRequestãä»ãããªã¯ãšã¹ãã¯ãåžžã«ãcorsãã¢ãŒãã§ãã ãŸãããã§ããã䜿çšãããšãã¢ãŒããçŽæ¥èšå®ã§ããŸãã
å¿çã«ã¯typeããããã£ããããŸãã çŸåšã®ãã¡ã€ã³ã«å¯ŸããèŠæ±ã¯ãåºæ¬ãã§ãã ãã以å€ã®å ŽåãèŠæ±ã¢ãŒãããcorsãã®å Žåãå¿ èŠãªããããŒãååšããå Žåãå¿çã¿ã€ãããcorsãã«ãªããŸãã å¿çã¢ãŒããopaqueãã¯ããno-corsãã¢ãŒãã®èŠæ±ã§åä¿¡ã§ããŸããå¿çããŒã¿ã«ã¯ã¢ã¯ã»ã¹ã§ããŸããã
ã¯ãšãªã¢ãŒãã®ãã¹ãŠã®å¯èœãªã¿ã€ããããã§èª¬æãããŠããããã§ã¯ãããŸããããããã¯äžè¬çãªç解ã«ååãªã¯ãã§ãã 詳现ã«ã€ããŠã¯ã fetchã®èšäºãã芧ãã ããã
ããã§ã¯ããã¹ãŠãçµã¿åãããŠã¿ãŸãããã èŠæ±ã¯ããŒãžãé¢ãããµãŒãã¹ã¯ãŒã«ãŒã«ãã£ãŠã€ã³ã¿ãŒã»ããããããã§ãããè¡ããå¿çãåãåããŸãã ãããŸã§ç¶æ³ã¯æŽçãããŠããŸããããä»ã§ã¯åŸ®åŠãªéãããããŸããããŒãžãªã¯ãšã¹ããžã®å¿çã§ãäžéæãã¿ã€ãã®å¿çãéä¿¡ããå Žåã§ãã ãno-corsãã¢ãŒãã§ã¯å®è¡ãããªãã£ãããããšã©ãŒãçºçããŸãã
ãªã¯ãšã¹ãã ãã§ãªããService Workerãå¥ã®ãã¡ã€ã³ã«ã€ã³ã¹ããŒã«ã§ããŸãã ãããããµãŒãã¹ã¯ãŒã«ãŒãéããŠå¥ã®ããŒãžãå¶åŸ¡ããããšã¯ã§ããŸããããµãŒãã¹ã¯ãŒã«ãŒã®æ¡ä»¶ã¯åããŸãŸã§ãïŒã¹ã¯ãªããèªäœã¯ãã¯ãŒã«ãŒãç»é²ãããŠããã®ãšåããã¡ã€ã³ã«ååšããå¿ èŠããããŸãïŒã ãããè¡ãã«ã¯ãç®çã®ãã¡ã€ã³ã®iframeã䜿çšã§ããŸãããŠãŒã¶ãŒæš©éã¯äžèŠã§ãiframeãåçŽã«é衚瀺ã«ããããšãã§ããŸãã
çŸåšã以åã®ããŒãžã§ã³ã«ããå¥ã®èå³æ·±ãæ©èœã¯Foreign Fetchã§ãã éåžžã®ãµãŒãã¹ã¯ãŒã«ãŒãèªåã®ã¹ã³ãŒãå ã®ããŒãžïŒãªã¯ãšã¹ãã§ã¯ãªããã¹ã³ãŒãå ã®ããŒãžïŒããã®ãªã¯ãšã¹ããå¶åŸ¡ããå Žåãå€éšãã§ããã«ããããã¡ã€ã³ãžã®ãªã¯ãšã¹ããå¶åŸ¡ã§ããŸãã CDNäžã®ã©ã€ãã©ãªã«å¯ŸããŠãªã¯ãšã¹ããè¡ããããšéåžžã®ãã§ããã€ãã³ããçºçãããã®ã©ã€ãã©ãªã«å¯Ÿãããã¹ãŠã®ãªã¯ãšã¹ãããµã€ãã§è¡ããããšforeignFetchãçºçãããšããŸãããã ãã®å¥œå¥å¿ã匷ãæ©äŒã¯ãããšãã°åæãµãŒãã¹ã§äœ¿çšã§ããŸãã
ãã¹ãäž
ãµãŒãã¹ã¯ãŒã«ãŒåãã®ãã¹ãã®äœæã«ã¯ããã€ãã®å°é£ããããŸãã ãã¹ãã®äœæã¯ããã»ã©ç°¡åã§ã¯ãããŸããïŒãªãã©ã€ã³ã¢ãŒãã確èªããå Žåã¯ãäœããã®æ¹æ³ã§ãããã¯ãŒã¯ãšã©ãŒããšãã¥ã¬ãŒãããå¿ èŠããããŸããæŽæ°ã確èªããå Žåã¯ããã¡ã€ã«ãæ°ãããã¡ã€ã«ãªã©ã«çœ®ãæããå¿ èŠããããŸãã
è¿œå ã®åé¡ã¯ãçŸæç¹ã§ããããã¬ã¹ããã©ãŠã¶ããµãŒãã¹ã¯ãŒã«ãŒããµããŒãããŠããªããšããäºå®ã«ããããŸããã€ãŸããå®éã®ãµãŒãã¹ã¯ãŒã«ãŒãå¿ èŠã§ãã
ãµãŒãã¹ã¯ãŒã«ãŒã®ãã¹ãã«é¢ãã䟡å€ã®ããèšäºããããŸãã ãŸãã sw-unit-test-sampleããã³platinum-sw ïŒPolymerã®èŠçŽ ã§ãããããã€ãã®ãã¹ãããããŸãïŒã®ããã€ãã®ããŒã«ãžã®ãªã³ã¯ããããŸãã ãŸãããã®èšäºã§ã¯èå³æ·±ãããªãã¯ã«ã€ããŠã説æããŠããŸããiframeãäœæããŠããã¹ãããããµãŒãã¹ã¯ãŒã«ãŒãå¶åŸ¡ã§ããããã«ããŸãã äžè¬çã«ãiframeèŠçŽ ãšãªããžã§ã¯ãèŠçŽ ã«ã¯å¥ã®æ©èœããããŸãããããã«å¯ŸããèŠæ±ãšãã®ã³ã³ãã³ãã¯ãç¬èªã®ãµãŒãã¹ã¯ãŒã«ãŒã䜿çšããŠçŸåšã®ããŒãžãµãŒãã¹ã¯ãŒã«ãŒããã€ãã¹ããŸãã
ããŒãžèªäœã§ãã£ãã·ã¥ãå©çšå¯èœã§ãããšããäºå®ã¯ããã£ãã·ã¥ã®å 容ãã¯ãªã¢ããŠæ€èšŒãããã¹ãã«åœ¹ç«ã¡ãŸãã
èªåãã¹ãã®äœæ¥äžã®éèŠãªãã¥ã¢ã³ã¹ã¯ããµãŒãã¹ã¯ãŒã«ãŒãããŒãžãå¶åŸ¡ããŠãªã¯ãšã¹ããã€ã³ã¿ãŒã»ããã§ããç¬éã決å®ããããšã§ãã åçŽãªnavigator.serviceWorker.readyãåžžã«æ£ãã決å®ãšã¯éããŸãã-ã¯ãŒã«ãŒãµãŒãã¹ãã¢ã¯ãã£ãåããããšããclients.claimïŒïŒãå®äºããåã«æºåå®äºãããªã¬ãŒãããŸãã 解決çã®1ã€ã¯controllerchangeã€ãã³ãããªãã¹ã³ããããšã§ããããã ããã§è©³ãã説æããŸã ã
ãµãŒãã¹ã¯ãŒã«ãŒã®æŽæ°
ãµãŒãã¹ã¯ãŒã«ãŒãæŽæ°ããéã«ã¯ã泚æãæã䟡å€ã®ããããã€ãã®ãã¥ã¢ã³ã¹ããããŸãã
ãµãŒãã¹ã¯ãŒã«ãŒã¹ã¯ãªãããèŠæ±ããéã®ããããŒã®ãã£ãã·ã¥ã®ãµããŒãã«ããããããããã©ãŠã¶ãŒã¯ãã£ãã·ã¥ã®æå¹æéã24æéã«ççž®ããŸãã ããã¯ããŠãŒã¶ãŒã誀ã£ãŠé·æéãµã€ãã匷å¶çµäºç¶æ ã«ããªãããã«è¡ãããŸãã ãã£ãã·ã³ã°ã«é¢ããStackOverflowã®è¯ãçãã¯æ¬¡ã®ãšããã§ãã
å¥ã®ãã¥ã¢ã³ã¹ïŒæŽæ°ã¯ã Service Worker ã¹ã¯ãªããèªäœãæŽæ°ãããå Žåã«ã®ã¿æ©èœããããã¯ãã€ãã§æ±ºå®ãããŸãã importScripts ãä»ããŠæ¥ç¶ãããŠãããã¡ã€ã«ãæŽæ°ããŠããService Workerèªäœã¯æŽæ°ãããŸãã ã
æŽæ°ãããšãå€ãã®å Žåãäžéšã®ãã¡ã€ã«ããããã¯ãŒã¯ãããã£ãã·ã¥ã«è¿œå ãããŸãã ãããããã©ãŠã¶ã®ãã£ãã·ã¥ã¯æ©èœããŠããŸãïŒ ãµãŒãã¹ã¯ãŒã«ãŒå ã®ãã§ããåŒã³åºããšåæ§ã ãã¡ã€ã«ãå€æŽãããŠããªãããšã確èªããïŒãã¡ã€ã«åã«ããŒãžã§ã³/ããã·ã¥ãå«ãããªã©ïŒãããã£ãã·ã¥ããã€ãã¹ããŠãªãœãŒã¹ãããŒãããå¿ èŠããããŸãã ãã£ãã·ã¥ããã€ãã¹ããŠãªãœãŒã¹ãããŒãããã«ã¯ãæåã§ãã§ãããåŒã³åºããŠããããã£ãã·ã¥ã«å¿çãè¿œå ãããïŒããšãã°response.okã確èªããããšãå¿ããã«ïŒããã£ãã·ã¥ã䜿çšããŸãïŒ 'no-cache' Requestãªãã·ã§ã³ïŒãããŸã§ã¯Firefox Nightlyã§ã®ã¿åäœããŸãïŒ ã ã©ã¡ããJake Archibaldã®èšäºã§èª¬æãããŠããŸã ã
æŽæ°äžã®Service Workerã¹ã¯ãªãããžã®ãªã¯ãšã¹ãã¯ãçŸåšã®Service Workerã®ãã§ããã€ãã³ããã³ãã©ãŒããã€ãã¹ããããšã«ãèšåãã䟡å€ããããŸãã
éå€
- serviceworke.rs-ãµãŒãã¹ã¯ãŒã«ãŒã®äœ¿çšäŸã®ãããµã€ã
- ãµãŒãã¹ã¯ãŒã«ãŒã®ãã説æãããã©ã€ãããã»ã¹
- ãããªã®ããªãã§ããã®äŸãšç¯å²ããããŒãžã®å¿ç
- ãµãŒãã¹ã¯ãŒã«ãŒã¯æºåãã§ããŠããŸããïŒ ããŸããŸãªæ©èœã®ãªã¹ãããã©ãŠã¶ãµããŒãã䜿çšäŸãæ²èŒãããJake Archibaldãµã€ã
- pwa.rocksã ããã°ã¬ãã·ãWebã¢ããªã®äŸãå«ãWebãµã€ã
- Googleã®ãµãŒãã¹ã¯ãŒã«ãŒã®äœ¿çšäŸ