ð Hello World! Progressive Web-Blog!!
Web Componentsã§PWAãªããã°ãäœã£ãŠã¿ãã[Loadingç·š]
Feb 13, 2018
ð¥ 2022幎ã«ãµã€ãã®äœããå€ããŸããŠãããçŸç¶ã¯ãã®èšäºã®éãã§ã¯ãããŸããããããã¡ãã®èšäºã®å®è£
ãåç
§ãããã®ã§ããã°
ãã¡ãã®ãã©ã³ã ãåç
§ãã ããã
ããã§äœåºŠç®ã®ææŠã§ãããããèšæ¶ããéããïŒå¹Žã¶ãïŒïŒåºŠç®ãããã®ããã°ã§ããããŸããäŒç€Ÿã®ãã€ã»ã³ã®
@ymotongpooããã¯ïŒïŒå¹Žã
ãèªèº«ã®ããã°ãç¶ç¶ãããŠãããæ¥ã
ã³ã³ãã³ããšãã®äœæ¥ã«èãããã ãã®çèãå¢ããç¶ããããŠããã®ã暪ç®ã«ãç§ã¯ãšããã°éå»ã«ïŒå¹Žä»¥äžç¶ããæžããã®ã¯æ¥èšãå«ããŠäžåãªããããã°ã¯ãšãããšæãè¿ããŠã¿ãã°2004幎ããã®ã©ã€ããã¢ããã°ããå§ãŸããèŸããŠã¯ä¹ãæãèŸããŠã¯ä¹ãæããç¹°ãè¿ããŠããæå¥ãæçµçã«ã¯Jekyllããã£ããããã§ãã£ã±ãé³ä¿¡äžéã«ãªã£ãã®ãïŒå¹Žåã䞊ã³ã«ãçèäºæ
ã«é¢ããŠç³ãäžãããšãæåŸã«ãžã ã«è¡ã£ãã®ãæ°ã¥ãã°ïŒå¹Žåãçµã£ãŠãããäŒå¡æš©ãè
ãå§ããŠããå°å€ªãã¯ãœåäž»ã«ãªã£ãŠããŸããŸããã
å®ã¯æžãããããšã¯ããããããã®ã§ãããããã°ãåéãããšãªããšãã©ãããã©ãŒã éžã³ã«å°äžå¹Žã»ã©ãããããã§ãã
ãMediumãã£ããããªãŒããšæã£ãŠããã§ã«"uskay"ãšããIDããšãããŠãããããŠãªããªãåã«é²ã¿ãŸããã ãªããŠæãã§ãŸãã€ããŠããã®ã§ãããæ°ã¥ãã°2018幎ã«ãªã£ãŠãããããã«ã¯æµç³ã«èªåã®å®è¡åãªãã«çµ¶æãæããããšããã£ãŠãä»ãŸã§ã®æ æ°ãææãã¹ãå¿æ©äžè»¢ãä»åã¯ïŒäžçªããã©ããããããïŒãããã°ãèªäœããšããã®ãããŠã¿ãŸããã
ãã£ããç³ãäžãããšãProgressive Web Appsãªããã°ã«ä»ç«ãŠãŠãããŸããããŸæç¹ã¯ã²ãšãŸãããŒã¿ççãªæ±ãã§ããŒã³ãåªå
ã®ãã®ãã®ãã®ã§ãããä»ååœè©²ãã©ãããã©ãŒã ãéçºããã«éçšã§æèããç¹ã以äžã«èšèŒããŸãã®ã§ãäœãã®ãçžã§ãã®ããŒãžãŸã§èŸ¿ãçãããçæ§ã«ããããŸããŠã¯ãåœèšäºãä»åŸã®ãŠã§ãéçºã®ãã³ããšãªã£ãŠæ¶åããŠé ããŸããšå¹žçã«åããŸãïŒèŠããããåºæ¥ã§ããªãã®ã§ãããçŸç¶ã®ãœãŒã¹ã¯
ãã¡ãã«æããŠãããŸãïŒã
ãšããã€ã€æžãããããšãå
šéšæžããšçžåœããªã¥ãŒããŒã«ãªãããšæãããŸãã®ã§ãè€æ°èšäºã«åããããŠé ããŸããååã¯Loadingç·šãšããããšã§
ããŒãžè¡šç€ºã¹ããŒãã®æé©åã«ã€ããŠäž»ã«åãäžããããšæããŸãã
ããããProgressive Web Appsã£ãŠïŒ
ãã¡ãã«ãïŒãããããããšã«ïŒèšäºã«ããŠé ããŠãããŸãããProgressive Web Appsã¯PWAãšããç¥åã§2015幎åŸæããããæµè¡ã£ãŠããWebããã³ããšã³ãã®ãã¹ãã©éããã€ãã®ãã¹ãã©ãããæãã«é©çšããWebã¢ããªã®é称ã§ãã
ãæè¿ã€ã³ã¹ã¿ãPWAåãããããçãªèªæ³ã§äœ¿ãããããšãå€ãã§ããã念ã®ããPWAã®ã³ã³ã»ãããèŠçŽããŸããšã
- ãšã«ããããã©ãŒãã³ã¹ã®ãããµã€ãã«ããŸããããSPAã§ãSPAãããªããŠããã©ããªãã¬ãŒã ã¯ãŒã¯äœ¿ã£ãŠã䜿ããªããŠãããããªãã§ãããã§ãããªãã§ãããããã衚瀺ãéããŠç»é¢é·ç§»ãã¹ã ãŒãºãªãã®ã«ããŠãŠãŒã¶ãŒã«å¥œãããŸãããã
- äžèšããã¥ãŒãã³ã°ããããä»åºŠã¯æè¿äœ¿ããããã«ãªã£ãŠããã€ã±ãŠãWeb APIãç©æ¥µçã«äœ¿ã£ãŠããããäœéšããŠãŒã¶ãŒã«æäŸããŸãããã
- ã€ã±ãŠãWeb APIã䜿ããšäœãã§ããããšãããšãWebã¢ããªãããŒã ç»é¢ã«è¿œå ã§ããããã«ãªã£ããããªãã©ã€ã³ã§ãåäœããããã«ãªã£ãããããã·ã¥éç¥ãã§ããããã«ãªã£ããããããªä»ãŸã§Webã§ã¯å®çŸã§ããªãã£ãã¢ããªã£ãœãããšãã§ããã®ã§ããããªãæã¯ãªãã§ãããïŒ
ãšãã£ããã®ã§ããå°ãäžèšã§ã觊ããŠããŸãããPWAã¯ãããŸã§ãã¹ãã©éãªã®ã§ç¹å®ã®3rd Partyãã¬ãŒã ã¯ãŒã¯ãã©ã€ãã©ãªã«ã¯äŸåããŸãããäžã®äžã«ã¯Reactã䜿ã£ãPWAãããããPolymerãé§äœ¿ããPWAãããããPHPãµãŒããŒãµã€ãããŒãžãªPWAããããŸããåŸã£ãŠæè¡ã¹ã¿ãã¯ãå€ãããšãé©çšå¯èœãªãã¹ãã©ã§ãããŸãããäžåºŠã«å
šéšãé©çšããã®ã§ã¯ãªã段éçã«ïŒ=ProgressivelyïŒæé©åããããšãã§ããŸãã
äžèšã§èšããšããã®ã€ã±ãŠãWeb APIãããå
·äœçã«çŽ¹ä»ãããšã
ServiceWorkerã
WebAppManifestã
Push API/Notification APIïŒ
Payment Request APIã
Credential Management APIãããããããã°ïŒã§ã¹ã¿ã¡ã³ãšãªããŸãããšããã泚ç®åºŠãé«ãæ¡æšè±éã¯ééããªã
ServiceWorkerãšãªããŸãã®ã§ããã¡ãã«ã€ããŠã¯å€ãã®æ¹ãã©ããã§èãèŠãããããã®ãšæããŸãã
ãŠã§ãããŠãºã¯ã©ããªæ§æã§äœã£ãã®ïŒ
ãšããã§ç³ãé
ããŸããããåœããã°åã¯
ããŠã§ãããŠãºãã«ããŸãããç§ã¯ãŠã§ãã奜ãã§ããã€ããŠãºé ã§ããããšã«ç±æ¥ããŠãããŸãããæ°åããããšãã¯ã¹ãã³ãããã«ããŠãããŸãã®ã§ããã¯ãã«ã«ãã²ããšã©ã£ã¡ãããããªãšæ©ãã§ãããšããã劻ãããããã«åŸè
ã®æ¡ã¯ã²ã©ããšè«ããä»ã«è³ããŸããããŠãããŠã§ãããŠãºãã¯ã©ã®ãããªæè¡ã¹ã¿ãã¯ã§æãç«ã£ãŠããããšãããšã
- ããã³ãã®ã¡ã€ã³ã¯Vanilla JavaScript (ES6) + Web Components
- ããã¯ãšã³ãã¯Firebase Hosting + Fireabase Cloud Functionsã§ãµãŒãã¬ã¹çãªã¢ã¬
ã§æ§æãããŠããŸããã¡ããã©çµµã«ãããšä»¥äžã®ãããªæãã§ãã
æ¥ããããªãã
UskayUIãªããã®ããã°å°çšã®Web Component矀ãäœã£ãŠããŸããŸãã.. åŸè¿°ããŸãããèšäºèªäœã¯ããŒã¯ããŠã³ã§æžããããã«ã€ãã£ãŠããŠã
<uskay-article>
ãšããã³ã³ããŒãã³ããFirebase Hostingã«çœ®ããŠãã
.md
ãã¡ã€ã«ã
fetch
ããŠããŠãããã³ããšã³ãã§ããŒã¯ããŠã³ãããŒã¹ã»ã¬ã³ããªã³ã°ããŠãããŸããã€ãŸãã
ããã³ããšã³ãã§JavaScriptãé§äœ¿ããŠåçã«ã³ã³ãã³ããæ§ç¯ããŠããã¿ã€ãã®ããŒãžãšãªããŸãããããã§ãåŠäœã«åå衚瀺ã§ãããã©ãŒãã³ã¹ãããåçš®Botã«ããã¬ã³ããªãŒã«ãããããã€ã³ããšãªã£ãŠããŸããWeb Components ã€ã±ãŠããïŒ
ä»åã¯ãªãã¹ã3rd Partyãã¬ãŒã ã¯ãŒã¯ãã©ã€ãã©ãªã¯äœ¿ããã«ã
Webãã©ãããã©ãŒã ã®çŽ æããã®ãŸãŸæŽ»ããããšãæèããŠããŸãããã ãããã¯ãã£ãŠãæè¿ã®ãã¬ã³ãã§ããWebããŒãžå
ã®åèŠçŽ é¡ïŒããšãã°ããããŒãšãïŒã¯ã³ã³ããŒãã³ãåããŠäžå
管çãããåå©çšãããããããã®ã§ããä»ã§ããReactãVueJsãªã©ã³ã³ããŒãã³ãæåãªäœ¿ããããã©ã€ãã©ãªãæ°å€ããããŸãããå€ãã¯Dojo toolkitã§
declare
ããŠã¿ãããjQueryã®ãã©ã°ã€ã³ãªãã䜿ã£ãŠã¿ããããã®æŽå²ã¯æ±ºããŠçããããŸããããããã£ãã³ã³ããŒãã³ããWebæšæºã§äœãããã®ä»æ§ãWeb ComponentsãšãªããŸããWeb Componentsã¯
Custom Elementsã
Shadow DOMã
HTML TemplatesãšããåWeb APIããæãç«ã£ãŠãããäŸãã°ïŒãã®ãããã·ã³ãã«ãªäŸã§ããïŒä»¥äžã®ããã«ç°¡åã«ã³ã³ããŒãã³ããäœããŸãïŒR.I.P
HTML imports...ïŒã
CODEPENã§è©Šã: Simple Custom Elementäžèšã®éããCustom Elementsã¯ES6 ClassesãšããŠäœãã®ã§ä»ã®ã³ã³ããŒãã³ãã®ç¶æ¿ãã§ããŸãããDOMãå
éšã®ããžãã¯ããã³CSSã¯ã«ãã»ã«åãããŸãã®ã§åå空éã®è¡çªããéæŸãããŸãããã錻è¡ãåºããããJavaScriptãæ§é åãããããªããŸãã®ã§æ¯éãšããè©Šããã ããã
AMPãå®è£
ããããšããæ¹ã¯ãåç¥ããšæããŸããã
<amp-img>
ã
<amp-carousel>
ãªã©ãšãã£ãAMPã®ã³ã³ããŒãã³ãéã¯ãŸãã«Web Componentsã§ããAMPã¯ãAMPHTMLã©ã€ãã©ãªãçšæããŠããCustom Elementsãåå©çšããããšã§ç°¡åã«ããŒãžãã€ããã¡ããããšãããã¬ãŒã ã¯ãŒã¯ãªã®ã§ãããŸããæè¿ã ãš
VueJsã
AngularãWeb ComponentsãåãåºãåããããŸããªãã¹ããã§ã
Webæšæºãã¡ãããšã¬ãã¬ããžããããçæ©éãæããŸããPerformance ããããããã«ã¯ã
ãšããããšã§ãŠã§ãããŠãºã¯
Web Componentsã ãã§ããŒãžãäœã£ãŠããã®ã§ãããWeb Componentsã£ãŠJavaScriptãå®è¡ããŠåããŠDOMãæ§ç¯ããã代ç©ã§ãã®ã§ãããŒãžè¡šç€ºã¹ããŒãã®æµã§ããJavaScriptã®èªã¿èŸŒã¿ãããŸãããšå·¥å€«ããªããšããã©ãŒãã³ã¹è¯ãããŒãžãæç»ãããŸããããããªæé©åãå¿
èŠãšãªãå Žåã¯ãäœãããã®ãã¶ã€ã³ãã¿ãŒã³ãåèã«ããã®ãå®ç³ããšæããŸãããæè¿ã¯
PRPL PatternïŒããŒãã«ãã¿ãŒã³ãšèªã¿ãŸãïŒãšãããã®ãæµè¡ã£ãŠããŸãã®ã§ãããã«åŸã£ãŠãã¥ãŒãã³ã°ãè¡ã£ãŠãããŸããPRPLãã¿ãŒã³ã¯
PushãRenderãPre-cacheãLazy-loadã®é æåãåã£ããã®ãšãªããŸããŠãããããã²ãšã€ã²ãšã€èŠãŠãããããšã¯æãã®ã§ãããæéã®é¢ä¿äžãåœèšäºã§ã¯ããŒãžè¡šç€ºã¹ããŒãæé©åã«çµã£ãŠ
äž»ã«Push/Renderã«ã€ããŠèŠãŠãããããšæããŸããPush/Render
ãã¡ãã¯ãCritical PathïŒïŒåææç»ã«å¿
èŠãªãªãœãŒã¹ãã¡ïŒãæé©åããããšãããã®ã§ãæç»ã«å¿
èŠãªãã®ã¯ãé ã§å
šéšäžæ°ã«ãšã£ãŠããããã®ã§
H/2 Pushããã
<link rel=preload>
ã䜿ããŸãããããšãã£ããã®ã§ããããããHTTP/1.1ã®ãŸãŸã ãšãŠã§ãããŠãºã®Critical Pathé¢é£ãªãœãŒã¹ã¯ä»¥äžã®ããã«äºæéšã«ããŠã³ããŒããããŠããŸãããã§ããå°ãªããšãJsã¯å
šéšå¿
èŠãªãã§ããã©ãæ²ããããªãäºæéšã«ããã¡ãã¯Chrome Developer Toolsã®Performanceã¿ãã§ç°¡åã«ç¢ºèªã§ããŸãã
ãããã©ã®ããã«æé©åããããšãããšã
- ãŸãã¯H/2ãæå¹ã«ããŠäžåºŠã«å€ãã®ãªãœãŒã¹ãååŸã§ããããã«ããŸãããã
- 次ã«ãªãœãŒã¹ååŸã®åªå
床ãå®çŸ©ããŸãããã
ä»åã¯ãæ軜ã«H/2 +
<link rel=preload>
ã䜿çšããŠããŸãããããã ãã§ä»¥äžã®ããã«Critical PathååŸãæé©åãããŸããPreloadããããªãœãŒã¹ã¯ååŸåªå
é äœãé«ãŸããéåæã§ããŠã³ããŒããããŠã¡ã¢ãªãã£ãã·ã¥ã«ä¿åãããŸãã
<link rel=preload>
ã®å®£èšã¯ãããªæããã¡ãã£ãš
crossorigin
å±æ§ã«ã€ããŠã¯ãããããç¹ãããã®ã§ããŸãã®æ©äŒã«èšäºåããããšæããŸãã
ãŸãããŠã§ãããŠãºã§ã¯ã¿ã€ãã«ã«Web Fontã䜿ã£ãŠããã®ã§ããã
FOITãé¿ããããã«CSS @font-faceå±æ§ã®ã²ãšã€ã®
font-display
ã䜿ã£ãŠããŸããçŸç¶
font-display: swap
ãšããŠããŸãã®ã§ãWeb Fontã䜿çšå¯èœã«ãªããŸã§ã¯ã·ã¹ãã ãã©ã³ãã䜿ã£ãŠæåã衚瀺ãããŸãã
ãŠãŒã¶ãŒã«ãèªãããã³ã³ãã³ãããªãã¹ãéãå±ããããã«æ¯éãšãå©çšããŠã¿ãŠäžããã
ããã«ã¯ãWeb Componentsã
ES6 Modulesã䜿ã£ãŠèªã¿èŸŒãã§ããã®ã§ã
<script type=module>
ãªJavaScriptã¯ãã¹ãŠDeferãããŠããŸããDeferããŠããã®ã§JavaScriptãRender BlockããããšãªãHTMLã®ããŒã¹ãè¡ããŠãäœæ³ãšããŠã¯ããããŒãªã®ã§ãããããããHTMLã«ã³ã³ãã³ããäœãçšæããªããšæåã¯èæ¯ã ãã衚瀺ãããŠããã®åŸåŸã
ã«Custom Elementsãæç»ããããšãã£ãã
ã¬ã€ã¢ãŠããåãåãæ®å¿µãªäºè±¡ãçºçããŸããããã§ã¯ã¡ãã£ãšæ¥ãããããããããªãã§ãç¡é²åããããã®ã§ãã¹ã±ã«ãã³HTMLãçšæããŸããããªãã®ããšã¯ãªããHTMLã«ãã ã€ã³ã©ã€ã³ã§ãããŒã®ããããŒãèšäºãå®è£
ããŠããã ãã§ãã
ã£ãŠãªæãã§Push/Renderã®æé©åããåãšåŸãæ¯èŒãããšãããªæãã§ããåãããããããã«NetworkãšCPU ThrottleããããŠããŸããã
Afterã®ã»ããã ããã衚瀺ãæ°æã¡ãã... Pre-cache
次ã«PRPLãã¿ãŒã³ïŒçªç®ã®PãPre-cacheã§ãããããã¯æ¬¡ã«ç»é¢é·ç§»ããã§ãããããŒãžïŒRouteïŒã®ãªãœãŒã¹ãå
èªã¿ããŠããããããšãããã®ã§ããåŸ
ã£ãŠãŸããã䞻圹ïŒ
ServiceWorkerã®ç»å Žã§ãïŒ ...ãã ããã¡ãã«ã€ããŠã¯å¥èšäºã§çŽ¹ä»ãããŠé ããŸãã
Lazy-Load
Lazy-Loadã¯å€ãããäŒããææ³ã§ããããŠã§ãããŠãºã§ã¯ç»åãå
šãŠ
<uskay-img>
ãšããWeb Componentã«WrapããŠãããå
éšçã«
Intersection Observerãå©çšããŠã¹ã¯ããŒã«ã«å¿ããç»åã®Lazy-Loadãå®çŸããŠããŸãããŸããããŒãžäžéšã®æç»ãåªå
ããããã«äžéšã®ããã¹ãã
<uskay-article>
å
ã§Lazy-LoadããŠããŸããããšããã©ããåºãŠããGistãéåæã«Shadow DOMã«å·®ã蟌ããããã«
<uskay-gist>
ãªããã®ãäœããŸããããã¡ãããŸãå¥èšäºã§ã
Lighthouseã§æž¬ã£ãŠã¿ãã
ã§çµå±ã©ããªããã©ãŒãã³ã¹ãªã®ãããšããããšã§
Lighthouseã§æž¬ã£ãŠã¿ããšãããªæãã§ããã¡ãªã¿ã«Lighthouseã¯Chromeã®ããŒã ãéçºããŠããŸãPerformance AuditããŒã«ã§ãä»ãWebãµã€ãã®æé©å床åãã枬ãããŒã«ã®äžã§ã¯ããã¡ã¯ãåããŠãããã®ã§ããChromeã®Developer Toolããç°¡åã«äœ¿ããŸãã®ã§ããŸã è©Šãããããšãç¡ãæ¹ã¯æ¯éãšããè©Šããã ããã
æããããPeformance 99ç¹ã®
é¿éšå¯ã®ãµã€ãã«ã¯è² ããŸãããåºã ããšããŠã¯æªããªãããšãã£ããšããã§ãããããã¡ãªã¿ã«Layoutããã³ã³ãã³ãéãäŸåããJavaScriptããããªãã«ããåœããã°ã«ãããŠã
Perfomanceã90ç¹å°åŸåãŸã§äŒžã°ãããã«ã¯ããããŸããããªãã«é 匵ãå¿
èŠãããŸããããã è©Šè¡é¯èª€ãç¹°ãè¿ããªããç¹æ°ãåŸã
ã«äŒžã³ãŠããã®ã¯èŠãŠããŠæ°æã¡ãããã§ããããã®éçšã§äœ¿ã£ãåœèšäºã®Tipsãçæ§ã«ã»ãã®å°ãã§ãã圹ã«ç«ãŠãããšãããã°å¹žãã§ããããŸãã
ãŸãšã
以äžãLoadingã«ç¹åããéçºã¡ã¢ãšãªããŸãããèŠèŠããç¹ãå€ã
ãã£ãããšåããŸãããä»å玹ä»ã§ããªãã£ã
ServiceWorkerã
WebAppManifestãå§ãããã®ä»Botçšã«ã¯
Rendertronã䜿ã£ãŠãããã
Web Components Polyfillã䜿ã£ãŠIE11察å¿ãããã
Web Share APIãå©çšãããããŠããã®ã§ããŸãã®æ©äŒã«èšäºãšããŠãã°ãããŠé ããã°ãšæããŸãã
Happy Tuesday!!ð