[対象: ä¸ãä¸ç´]
åã®ããã°ã§ã¯ã¦ãæ°ããã¡ã°ãå¤ãã®ã¯ã¦ã§ããã¼ã¸ãé«éåããTIPSã解説ããè¨äºã§ãï¼ã¾ã èªãã§ãªã人ã¯ãã²èªãã§ãã ããï¼ï¼ã
ãã®è¨äºã§ã¯é«éåå ¨è¬ãæ±ã£ã¦ãã¾ããããä»æ¥ã®è¨äºã§ã¯JavaScriptã«çãçµã£ã¦è¡¨ç¤ºé度ãã¹ãã¼ãã¢ããã§ããæ½çã6ã¤ç´¹ä»ãã¾ãã
ãã¨ãã¨ã¯Search Engine Peopleããã°ã®Optimizing JavaScript for Better Web Performanceã§èª¬æããã¦ãããã®ã§ãã
è¨äºä½è ã®Joydeep Debï¼ã¸ã§ã¤ãã£ã¼ãã»ããï¼æ°ã«åã®ããã°ã§ã®è»¢è¼è¨±å¯ããããã¾ãã (Thanks, Joydeep!)ã
éä¸ã®ç¿»è¨³ã§ã¯ãªããæ¸ããã¦ããå 容ããã¨ã«åã®è¨èã§è§£èª¬ãã¦ããã¾ãã
1. HTTPãªã¯ã¨ã¹ãåæ¸ã®ããã«JavaScriptãã¡ã¤ã«ã1ã¤ã«çµ±åãã
ã¦ã§ããã¼ã¸ã®è¡¨ç¤ºãé«éåããç§è¨£ã®1ã¤ã¯ãHTTPãªã¯ã¨ã¹ãã®æ°ãæ¸ãããã¨ã§ãï¼1æã®ã¦ã§ããã¼ã¸ã§ãã£ã¦ããHTMLãã¡ã¤ã«æ¬ä½ãç»åãã¹ã¯ãªãããªã©ããã¦ã³ãã¼ãããããã«ã¦ã§ããµã¼ãã¼ã¨ãã©ã¦ã¶ã¯ããã¯ã°ã©ã¦ã³ãã§ä½åãä½åãããåããã¦ããï¼ã
HTTPãªã¯ã¨ã¹ãã®æ°ãæ¸ããããã«è¤æ°ã®ãã¡ã¤ã«ã«åããã¦ããJavaScriptã1ã¤ã®ãã¡ã¤ã«ã«ã¾ã¨ãã¾ãã
JavaScriptãè¤æ°ãã¡ã¤ã«ã«åããæªãä¾:
<script src="http://www.example.com/menu.js"></script> <script src="http://www.example.com/ajax.js"></script> <script src="http://www.example.com/nav.js"></script> <script src="http://www.example.com/tools.js"></script> <script src="http://www.example.com/footer.js"></script> <script src="http://www.example.com/others.js"></script>
JavaScriptã1ãã¡ã¤ã«ã«ã¾ã¨ããè¯ãä¾:
<script src="http://www.example.com/combined-all.js"><</script>
ä¸ã®ä¾ã§ã¯ããã¡ã¤ã«ã1ã¤ã«çµ±åãããã¨ã«ãã6åçºçãã¦ããHTTPãªã¯ã¨ã¹ãã1åã«æ¸ããã¾ãã
ã¢ã¯ã»ã¹æ°ã®å¤ããµã¤ãã§ã¯ç¹ã«å¹æãæå¾
ã§ãã¾ãã
2. CSSã®åç §ã¿ã°ãJavaScriptã®åç §ã¿ã°ãããå ã«è¨è¿°ãã
HTMLã®headã»ã¯ã·ã§ã³ã§ã¯ãã¹ã¿ã¤ã«ã·ã¼ãã®åç §ãå ã«è¨è¿°ããã®å¾ã«JavaScriptã®åç §ãè¨è¿°ãã¾ãã
JavaScriptã¿ã°ãããå¾ãã«CSSã¿ã°ãè¨è¿°ããæªãä¾
<head> <script src="http://www.example.com/nav.js"></script> <script src="http://www.example.com/tools.js"></script> <script src="http://www.example.com/footer.js"></script> <link rel="stylesheet" type="text/css" href="stylesheet1.css" /> <link rel="stylesheet" type="text/css" href="stylesheet2.css" /> </head>
JavaScriptã¿ã°ã®åã«CSSã¿ã°ãè¨è¿°ããè¯ãä¾
<head> <link rel="stylesheet" type="text/css" href="stylesheet1.css" /> <link rel="stylesheet" type="text/css" href="stylesheet2.css" /> <script src="http://www.example.com/nav.js"></script> <script src="http://www.example.com/tools.js"></script> <script src="http://www.example.com/footer.js"></script> </head>
JavaScriptã¯èªã¿è¾¼ã¿ãå§ã¾ãã¨ãã®èªã¿è¾¼ã¿ãå®äºãã¦å®è¡ãããã¾ã§ã¯æ¬¡ã®ã³ã¼ãã®èªã¿è¾¼ã¿ãå§ã¾ãã¾ããã
å¾
ã¡æéãçºçããããã§ããã
ä¸æ¹ã§ã¹ã¿ã¤ã«ã·ã¼ãã®èªã¿è¾¼ã¿ã¯è¤æ°ãåæã«è¡ãããå®äºãã¦ããªãã¦ãJavaScriptãã¡ã¤ã«ãå«ãä»ã®ãã¡ã¤ã«èªã¿è¾¼ããã¨ãã§ãã¾ãã
å è¨äºã«ã¯æ¸ãã¦ããªããã¨ã®è£è¶³ã¨ãã¦ãç¹ã«è¯ããªãã®ã¯è¤æ°ã®ã¹ã¿ã¤ã«ã·ã¼ãåç §ã®éã«JavaScriptåç §ãæãã§ãã¾ããã¨ã§é度ã大ããä½ä¸ãããå¯è½æ§ãããã¾ãã
CSSåç §ã¿ã°ã®éã«JavaScriptåç §ã¿ã°ãæãã¨ã¦ãæªãä¾
<head> <script src="http://www.example.com/nav.js"></script> <link rel="stylesheet" type="text/css" href="stylesheet1.css" /> <script src="http://www.example.com/tools.js"></script> <link rel="stylesheet" type="text/css" href="stylesheet2.css" /> <script src="http://www.example.com/footer.js"></script> </head>
3. ãããã³ã«ãçç¥ãã
JavaScriptã®å¤é¨ãã¡ã¤ã«ã示ãURLãã http:
ã¾ã㯠https:
ã¨ãã£ããããã³ã«ï¼ã¹ãã¼ã ï¼ãçç¥ãã¾ãã
çç¥ãããã¨ã§ãã¡ã¤ã«ãµã¤ãºãï¼ãããããã ããã©ï¼å°ããã§ãã¾ãã
ãããã³ã«ãçç¥ããªãæªãä¾:
<script src="http://www.example.com/scripts/javascript.js"></script>
ãããã³ã«ãçç¥ããè¯ãä¾:
<script src="//www.example.com/scripts/javascript.js"></script>
ãããã³ã«ãçç¥ããã¦ããæã¯ãã®ãã¼ã¸ã®è¡¨ç¤ºã«ä½¿ããã¦ããï¼ãã©ã¦ã¶ã®ã¢ãã¬ã¹ãã¼ã«è¡¨ç¤ºãããï¼ãããã³ã«ãèªåçã«é©ç¨ããã¾ãã
ã¤ã¾ã https:
ã§è¡¨ç¤ºãã¦ãããªãåç
§å
ã«ãèªåçã« https:
ã使ããã¾ãã
ã¡ãªã¿ã«JavaScriptã ãã§ãªãCSSã§ãç»åã§ãä½ã§ããããã³ã«ã¯URLããçç¥å¯è½ã§ãã
ãã ãçç¥ããå ´åã¯ãhttp:
ãªã http:
ã§ãhttps:
ãªã https:
ã§ãã®å¤é¨ãªã½ã¼ã¹ãåç
§ã§ããªããã°ãªãã¾ããã
4. scriptè¦ç´ ã®typeå±æ§ãçç¥ãã
HTML5ã«éãã¾ãããscriptè¦ç´ ã«ãããtypeå±æ§ããªãã¡ type="text/javascript"
ãçç¥ãã¾ãã
HTML5ã§ã¯ãscriptè¦ç´ ã®typeå±æ§ã®åæå¤ã "text/javascript"
ã«ãªã£ã¦ããããã§ãã
çç¥ãããã¨ã§ï¼ãããããã«ï¼ãã¡ã¤ã«ãµã¤ãºãå°ããã§ãã¾ãã
typeå±æ§ãçç¥ãã¦ããªãæªãä¾:
<script src="//www.example.com/scripts/javascript.js" type="text/javascript"></script>
typeå±æ§ãçç¥ããè¯ãä¾:
<script src="//www.example.com/scripts/javascript.js"></script>
ãªãHTML5ã§ã¯ãstyleè¦ç´ ã®typeå±æ§ã®åæå¤ã¯ "text/css"
ã«ãªã£ã¦ããã®ã§ type="text/css"
ãåæ§ã«çç¥å¯è½ã§ãã
5. Expiresãããã¼ã§ãã£ãã·ã¥æéãé·ããã
ãã©ã¦ã¶ã¯1度ãã¦ã³ãã¼ããããã¼ã¿ããã¼ã«ã«ã«ãã£ãã·ã¥ãã¾ãã
å度åããã¼ã¿ãå¿
è¦ã«ãªã£ãæã¯ãã¼ã«ã«ã«ãã£ãã·ã¥ãããã°ãµã¼ãã¼ã«ãªã¯ã¨ã¹ãããã«ãã£ãã·ã¥ãã¼ã¿ãå©ç¨ãã¾ãã
ãã§ã«èªåãæã£ã¦ãããã¼ã¿ã使ãã®ã§ã¬ã¹ãã³ã¹ãéããªãã¾ãã
ãã£ãã·ã¥ã¯æéãåããã¨åé¤ããã¾ãããExpiresã¨ããHTTPãããã¼ã使ãã¨ãã£ãã·ã¥æéã®å¼·å¶çãªæå®ãå¯è½ã§ãã
ã¦ã§ããµã¼ãã¼ãApacheã®å ´åã.htaccessã¾ãã¯httpd.confã§Expiresãããã¼ãè¨å®ããã¨JavaScriptã®ãã£ãã·ã¥æéãå¶å¾¡ã§ãã¾ãã
ãã¨ãã°ä¸ã®ããã«è¨è¿°ã§ãã¾ãã
<IfModule mod_headers.c> <IfModule mod_expires.c> ExpiresActive On ExpiresByType text/javascript "access plus 4 weeks" ExpiresByType application/x-javascript "access plus 4 weeks" ExpiresByType application/javascript "access plus 4 weeks" </IfModule> </IfModule>
JavaScriptãã¡ã¤ã«ã«æåã«ã¢ã¯ã»ã¹ããæãã4é±éãéããã¨ãã£ãã·ã¥ãæéåããã¦æ¶ãã¾ãã
è¨ãæããã¨4é±éã¯ãã¼ã«ã«ã«ãã£ãã·ã¥ãæ®ãã¾ãã
æéã®æå®æ¹æ³ã«ã¯ç¨®é¡ãããã¾ãã
詳細ã¯Apacheã®ããã¥ã¡ã³ããåç
§ãã¦ãã ããã
ãªããµã¼ãã¼ã«mod_expiresã¢ã¸ã¥ã¼ã«ãã¤ã³ã¹ãã¼ã«ããã¦ããªãã¨æ©è½ãã¾ããã
ã¾ãJavaScriptã ãã§ãªãä»ã®ã³ã³ãã³ãã®ãã£ãã·ã¥æéã®å¶å¾¡ã«ãExpiresãããã¼ã¯ä½¿ãã¾ãã
åã¯ç»åãã¡ã¤ã«ã®ãã£ãã·ã¥æéãè¨å®ãã¦ãã¾ãã
ExpiresByType image/gif "access plus 4 weeks" ExpiresByType image/jpeg "access plus 4 weeks" ExpiresByType image/png "access plus 4 weeks"
ãã¡ã¤ã«ã¿ã¤ãï¼MIMEã¿ã¤ãï¼ã®æå®ãApacheã®ããã¥ã¡ã³ããèªãã§ãã ããã
JavaScriptã®ãã¡ã¤ã«ã¿ã¤ãã¯ããã¥ã¡ã³ãã«ã¯æ¸ããã¦ããªãã¦èª¿ã¹ãã¨ããããã¤ãããããã§ãã
ãµã¼ãã¼ã®ç°å¢ãå©ç¨ãã¦ããCMSã«ãã£ã¦éã£ã¦ãã¦ä¸è¬çã«ä½¿ããã¦ãã3ã¤ã®ç¨®é¡ãåã¯è¨è¿°ãã¦ãã¾ãã
å
·ä½çãªè¨è¿°ã¯å
è¨äºã«æ¸ããã¦ãã¾ããã
åãèªåã§èª¿ã¹ã¦ä»¥åããå©ç¨ãã¦ããè¨è¿°ãç´¹ä»ãã¾ããï¼ãã£ã¨é©åãªè¨è¿°ãç¥ã£ã¦ãããæãã¦ãã ããï¼ã
ãã£ãã·ã¥æéãã©ã®ãããã«è¨å®ããããåé¡ã§ãããããã¯èªåã§èãã¦ãããããããã¾ããã
åãè¨å®ãã¦ããã4é±éãã«åççãªæå³ãããããã§ã¯ããã¾ããã
ããããããã§ããããªãã§æ±ºãã¾ãããï¼ç¬ï¼
â»RFCã«å¾ãã°1年以ä¸ã®æéãæå®ãã¦ã¯ãããªãããã§ãã
ã¾ãhttpd.confã§ã¯ãªãã¦.htaccessã«æ¸ãã®ãªãã4 weeksããããæ°å¤ã®ã»ããããã©ã¼ãã³ã¹ããããªãããããã¾ããã
注æããªããã°ãªããªããã¨ã¯ãã¾ã£ãããããã¯ã»ã¨ãã©æ´æ°ããªãéçãªãã¡ã¤ã«ã«Expiresãããã¼ã使ããã¨ã§ãã
æ´æ°ãã¦ããã£ãã·ã¥ã使ã£ã¦ãã¾ãã®ã§ãã©ã¦ã¶ã«æ®ã£ã¦ããå¤ããã¼ã¿ã使ããã¦ãã¾ãããã§ãã
æ´æ°ããå ´åã¯ãã¡ã¤ã«ã®ååãå¤ããã¨ããã§ãããï¼åãã¦ãªã¯ã¨ã¹ããããã¡ã¤ã«ã«ãã¦ãã¾ãï¼ã
ãã£ãã·ã¥ã®å¶å¾¡ã¨ãã¦ã¯ãããã·ãã£ãã·ã¥ãæ´»ç¨ãããã¨ãã§ãã¾ãã
å è¨äºã«ã¯ç°¡åã«ãã触ãããã¦ããªãã®ã§åèªèº«ã§èª¿ã¹ã解説ã«ãªãã¾ãã
ãã¨ãã°éä¿¡çµè·¯éä¸ã®ISPã§ãããã·ãµã¼ãã¼ã使ããã¦ããã¨ãã¯ããã®ãããã·ãµã¼ãã¼ã«ããã¡ã¤ã«ããã£ãã·ã¥ããã¾ãã
ãã¨ãããªããåãã¦ã¢ã¯ã»ã¹ãããµã¤ãã§ãã£ã¦ãåããããã·ãµã¼ãã¼ãå©ç¨ãã¦ããä»ã®ã¦ã¼ã¶ã¼ãã¢ã¯ã»ã¹ãããã¨ã®ãããã¼ã¸ã§ããã°ãããã·ãµã¼ãã¼ã«ãããã£ãã·ã¥ãã¼ã¿ãè¿ããã¾ãã
ã¦ã§ããµã¼ãã¼ã¾ã§ã®éä¿¡ãçºçããªãã®ã§å¿çã¹ãã¼ããçµæã¨ãã¦éããªãã¾ãã
ãããã·ãã£ãã·ã¥ãã³ã³ããã¼ã«ããã«ã¯Cache-Control
ãããã¼ãè¨å®ãã¾ãã
ãã¨ãã°ãããªãµãã«è¨è¿°ã§ãã¾ãã
<IfModule mod_headers.c> <FilesMatch "\.(jpg|png|gif|swf|ico|css|js)$"> Header set Cache-Control "max-age=2419200, public" </FilesMatch> </IfModule>
JavaScript以å¤ã«ããç»åãCSSãªã©ã®ãã®ä»ã®éçãã¡ã¤ã«ãå«ãã¦ãã¾ãã
ãã£ãã·ã¥ã®ä¿ææéã¯4é±éã«ã»ãããã¦ããã¾ãã
注æç¹ãããã¾ãã
CSSã¨JavaScriptãCache-Control
ãããã¼ã§ãã£ãã·ã¥ãããã¨ãã¯ããã製åã®ãããã·ãµã¼ãã¼ã§ã¯ãã°ã«ãããã¾ãæ©è½ããªããã¨ãããããã§ãã
Vary: Accept-Encoding
ãããã¼ãä½µç¨ãããã¨ã§åé¿ã§ãã¾ãï¼ããã¦ãã®ãµã¼ãã¼ã§ã¯å®è£
ããã¦ããã¯ãã詳細ã¯æå¾ã«è¼ããããã¥ã¡ã³ãã§ç¢ºèªãã¦ãã ãããï¼ã
6. ã³ã¡ã³ããåé¤ãã
JavaScriptãã¡ã¤ã«ã«æ¸ãè¾¼ã¾ãã¦ããä¸è¦ãªã³ã¡ã³ããåé¤ãã¾ãã
ãã£ã¢ã«ãµã¤ãºãå°ãããªããã©ã¦ã¶ã®èªã¿è¾¼ã¿ãéããªãã¾ãã
HTMLãã¡ã¤ã«ãCSSãã¡ã¤ã«ã«ãã³ã¡ã³ãåé¤ã®å¹ç¨ã¯å½ã¦ã¯ã¾ãã¾ãã
ãã£ã¨ãä½æçã«ã©ã®ãããã¹ãã¼ãã¢ãããããã©ããã¯çåã§ããã©ãã
ãããããªããã£ã¦ããã°ãç¨åº¦ã§ããããã
以ä¸ãã¦ã§ããµã¤ãã§ã®JavaScriptã®èªã¿è¾¼ã¿ãæé©åãã¦ã¦ã§ããã¼ã¸ã®è¡¨ç¤ºé度ãæ¹åãã6ã¤ã®æ¹æ³ã§ãã
ãã£ãã·ã¥ã®ã¨ãããã²ãã£ã¨ããã¨é£ããã£ãããããã¾ãããã
ãã£ã¨ãåããç¥ããªãã¦ã.htaccessã«è¿½å ããã°æ®éã¯æ©è½ããã®ã§ä½ãèããã«å®è£ ãããã¨ãã§ããã§ãããï¼ã¨ã¯è¨ãèªå·±è²¬ä»»ã§ãé¡ããã¾ãï¼ã
JavaScriptãã¡ã¤ã«ã®çµ±åã¨åç §ã¿ã°ã®é ç½®ã«ã¤ãã¦ã¯ããªãã®ãµã¤ãã§ã¯ã©ããªã£ã¦ãããã£ããè¦ç´ãã¦ã¿ã¦ãã ããã
ä½åãªè¨è¿°ãçããã¡ã¤ã«ãµã¤ãºã®åæ¸ã«ããèªã¿è¾¼ã¿é度ã®æ¹åã¯ç®ã«è¦ããã»ã©ã®å¤§ããªå¹æã¯ãªãããªæ°ããã¾ããæ°ä¼ãç¨åº¦ã«ãã£ã¦ã¿ãã®ãæªãã¯ãªãã¯ãã§ãã
ä»åãå©ç¨ã許å¯ãã¦ãããJoydeepæ°ã®è¨äºãæ¥æ¬èªè§£èª¬ããéã¯ãç解度ã確ãã«ãã¦ãã¾ã説æã§ããããã«èªåã§ããããã調ã¹ã¾ããã
調ã¹ãéç¨ã§çºè¦ããã®ã§ãããJoydeepæ°ã¯ä»¥ä¸ã®ããã¥ã¡ã³ããããå¤æ°ã®æ
å ±ãå¼ã£å¼µã£ã¦ãã¦ãã¾ãã
ãã¹ã¦è±èªã«ãªãã¾ããããã詳ãããã¨ã«å ãã¦JavaScript以å¤ã®ã¹ãã¼ãã¢ããæ¹æ³ããããã説æããã¦ããã®ã§èªãã§ããã¨çµ¶å¯¾ã«å¾ãã¾ãã