Turbolinksåè
èªåã§ãªãã¤ã¬ã¯ããã¾ãã
ãã®è¨äºã¯ããSpeee Advent Calendar 2016ãã®4æ¥ç®ã§ãã
3æ¥ç®ã¯ã@Kosaku_HidaããããGoogle Spread Sheetã«è¨è¼ããè¤æ°URLã®Page Speed Insightsã®ç¹æ°ãGoogle Apps Scriptã§åå¾ããæ¹æ³*1ãã§ãã
ä»æ¥ã¯ãTurbolinksãèãç´ãã¦ã¿ã¾ãã
ã¿ãªãããTurbolinks好ãã§ããï¼
Rails使ã£ã¦ã人ã«ã¯ãçµæ§å«ããã¦ãå°è±¡ã§ããããããã§ãããï¼
Railsã¨ã³ã¸ãã¢ã«å«ãããTurbolinksã®å³
ãªãã§å«ããããã¨ããã¨çç±ã¯ããã£ã¦ã·ã³ãã«ã§ãæ¢åã®è³ç£ãåããªããªã£ãããã§ãã
ã¨ããããªããªãæ²ããæ´å²ãæã£ãTurbolinksã§ãããæ¹ãã¦èãç´ãã¦ã¿ããã¨ãããã¨ã§ãã¾ãã¯Turbolinksã¨ã¯ä½ããç°¡åã«ãããããããªãæ¢åã®è³ç£ãåããªããªã£ãããããã¦ãä»ã©ãã®ããã³ãã¨ã³ããªãã©ããªã®ããèãã¦ã¿ã¾ãã
Turbolinksãããã
Railsã®æèã§èªããããã¨ãå¤ãTurbolinksã§ãããTurbolinksèªä½ã¯Railsã¨ã¯åãé¢ãããJSã®ã©ã¤ãã©ãªã§ãã
var Turbolinks = require("turbolinks"); Turbolinks.start();
è¶ ãã£ããã¨èª¬æããã¨ãä¸è¨ã®ãããªå¼ã³åºãã«å¯¾ãã¦ã以ä¸ã®ãããªå¦çããã¾ãã
a
ã¿ã°ã®click
ã¤ãã³ããä¹ã£åãã以ä¸ã®ãããªå¦çã§ä¸æ¸ãããhref
ã«æå®ãããURLãèªãµã¤ãå ãã©ããå¤å®- èªãµã¤ãå ã§ããã°ããã¼ã¸é·ç§»ã¯ããã«ãXHRã§ãªã³ã¯å ã®ãã¼ã¸ãåå¾
- åå¾ããé·ç§»å
ã®HTMLã解æãã
title
ã¿ã°ãbody
ã¿ã°ãªã©ãæ½åº - æ½åºããã¿ã°ã®ä¸èº«ã§ãå®éã«ä»è¡¨ç¤ºãã¦ããDOMãä¸æ¸ããã
- HistoryAPIã使ã£ã¦ãã¾ãã§ãã¼ã¸é·ç§»ãããã®ããã«è¦ãã
ãã£ãã·ã¥ãªã©ã®ç´°ããè¨å®ãããã¼ã¸é·ç§»ã¨ãã§ã¯ããªã©ã®è¿½å æ©è½ãããã¾ãããã³ã¢ã«ãªãæ©è½ã¯ãããªã¨ãã§ããã¤ã¾ããJavaScriptã§ã¯ã©ã¤ã¢ã³ãå´ãããããã¨ã«ãããã¾ãã§SPAã®ãããªè¦ãç®ãå®ç¾ãããã¨ãã§ãã¾ãã
ä½ãåé¡ã ã£ãã
jQuery + jQueryã¨ã³ã·ã¹ãã ã¨ã®ç¸æ§ãæªãã£ããã§ãã
jQueryãjQueryãã©ã°ã¤ã³ãjQueryã使ã£ãåå©ç¨å¯è½ãªã³ã³ãã¼ãã³ãã®ã»ã¨ãã©ã¯ã
- ç¹å®ã®ã¯ã©ã¹åãä»ããã«ã¼ã«ã§HTMLãã¼ã¯ã¢ãããã
$(document).ready
ã§ç¹å®ã®ã¯ã©ã¹ãä»ä¸ãããDOMãæ¢ããã»ããã¢ãããã
ã¨ããåããæé»çã«ãã¾ããããããã¯ãjQueryã¨ã¯ãããããã®ã ãã¨ããä¸çã§ããã¨ããããTurbolinksã使ãã¨ããã¼ã¸é·ç§»ããªããªã£ã¦ãã¾ãã¾ããã¤ã¾ããä¸è¨ã§èª¬æããã4ãã®ãã§ã¼ãºã§æ°ãããã¼ã¸ã®DOMãå½¢æããã¾ããããã®å¾ã«$(document).ready
ã¯çºç«ããªãã®ã§ãã
ã¨ã¯è¨ããå®éã«ã¯ãã®åé¡ãåé¿ããæ段ãç¨æã¯ããã¦ããã®ã§ãããTurbolinksèªä½ã®åãããã£ããç解ãã¦ããªãã¨æ£ãããã¥ã¼ãã³ã°ã¯ã§ãã¾ãããã¾ããjQuery以å¤ã¯èªåã§ã³ã¼ããæ¸ãã¦ãããããªããã³ãã¨ã³ããªãã¨ããããjQueryãã©ã°ã¤ã³ãOSSã®jQueryã³ã³ãã¼ãã³ããçµã¿åããã¦ããã³ããæ§ç¯ãã¦ããå ´åã«ã¯ãæã«è² ããªããªã£ã¦ãä»æ¹ãªãã£ããã¨ã§ãããã
ãããã2017å¹´ã«ãªããã¨ãã¦ããä»
ãã¦ãããã¾ã§ã¯éå»ã®è©±ã§ãããä»ã¯ã©ããã
ãããã®ã¢ããã³ãã«ã¬ã³ãã¼ãã2å¹´çµã¤ãã§ãããæãæµããã®ã¯æ©ãã§ããããããããããã¤ãã®AltJSãæ·æ±°ããã¦æ£çµ±ESã復å¤ããããjQueryã使ãã®ã¯ãã¯ãæªã ã¨ãããããã®è¨ããããã«ãªã£ããããã«ããã¼ã«ãä¸å¨åã£ã¦package.jsonã«æ»ã£ã¦ããããããããªãã¨ãããã¾ããã
ããã³ãã¯æµããæ©ãã¨ãè¨ããã¾ãããã¨ã«ãããjQueryã使ããã«Reactã§ããã³ãæ§ç¯ããã®ãããã£ã¬ã³ã¸ãã ã£ãæ代ã¯çµãã£ãã®ã§ãã
ã§ã¯ä»ã©ãã®ãReactã®å ´åã¯ã©ããªããã¨ããã¨ãReactã³ã³ãã¼ãã³ãå´ã§window
ã®load
ã¤ãã³ããdocument
ã®DOMContentLoaded
ã¤ãã³ãããã³ãã«ãããããªã³ã¼ãã¯æ¸ãã¾ãããã¤ã¾ããä»®ã«OSSã®Reactã³ã³ãã¼ãã³ãã©ã¤ãã©ãªãçµã¿åããããããªããã³ãæ§ç¯ãããã¨ãã¦ããã©ã¤ãã©ãªã®å
é¨ã§ãããã®ã¤ãã³ãã«ä¾åãã¦ãã¦ãTurbolinksã¨ãã¾ãé£æºããªãã¨ããå¿é
ã¯ã¾ããªãããã§ãã
ä¸æ¹ãRailsã¯ãã£ã¨ããã©ã«ãã§jQueryãä¾åã«å ãããã¦ãã¾ãããã5.1.0ã§ã¯ã¤ãã«jQueryãä¾åããåãé¤ãããããã§ããããã§Railså´ãTurbolinksã使ãåå£ãæ´ã£ã¦ããã§ãããã
SPAã¨çä¼¼SPA*2
ä¸æ¹ã§ãããããSPAãå®ç¾ããããªããAngularãReactRouterã使ãã°ãããããªããã¨ãã声ããããã¨æãã¾ããããã¯ã¾ãã«ãã®éãã§ãTurbolinksãæä¾ãããããªçä¼¼SPAãªããã«é ¼ããã«æ¬ç©ã®SPAãå®è£ ããä½åãããã®ã§ããã°ãããã«è¶ãããã¨ã¯ããã¾ããããã®æ¹ãããã¼ã¸é·ç§»ãã¾ããã ãã¼ã¿ãã£ãã·ã¥ãèªç±ã«ã³ã³ããã¼ã«ã§ãããããããé«ãã¦ã¼ã¶ããªãã£ãå®ç¾ã§ããããã§ãã
ãããTurbolinksã®æ大ã®ã¡ãªããã¯ããµã¼ããµã¤ãã«ä¸åã®å¤æ´ãå¿ è¦ãªãã¨ããç¹ã«ããã¾ãã極è«ãè¨ãã°ããµã¼ããµã¤ããåçã¢ããªã±ã¼ã·ã§ã³ã§ããå¿ è¦ããç¡ãã®ã§ãããã®ãµã¤ã*3ã¯ãAWS S3ã®éçã¦ã§ããµã¤ããã¹ãã£ã³ã°+CloudFrontã§æ§ç¯ãããå®å ¨ã«éçãªãµã¤ãã§ãæ®éã®HTMLãã¡ã¤ã«ãç½®ããã¦ããã ãã§ãããTurbolinksã§çä¼¼SPAãå®ç¾ãã¦ãã¾ããå½ç¶ããµã¼ããµã¤ãã¬ã³ããªã³ã°ã«ã¤ãã¦ããããèããå¿ è¦ãããã¾ããã
ç¹ã«ã¹ãã¼ãéè¦ã®æ°è¦éçºã®å ´é¢ã«ããã¦ã¯ãAPIã¨Viewãå¥ã ã«ä½ãã®ã¯ã©ããã¦ãç ©ããããã®ã§ããã·ã³ãã«ãªãµã¼ããµã¤ãController-Viewãä½ãéãã¨çä¼¼SPAã«ããã¦ã¼ã¶ããªãã£ã®ä¸¡ç«ã«ã¯ä¸å®ã®ä¾¡å¤ãããã®ã§ã¯ãªãã§ããããã
ã¾ã¨ã
Turbolinksãæå¤ã¨ãããã
ç¡æ¡ä»¶ã«rails new --skip-turbolinks
ããã«ãTurbolinksã®çä¼¼SPAãå³ãã£ã¦ã¿ã¦ã¯ï¼
ææ¥ã¯@takanamitoããããã¡ããã£ã¨ãµã¼ãã¼ãç«ã¦ããã¨ãã®Terraformã®æ§æãèãããã§ããã楽ãã¿ã«ã