gzipå§ç¸®ã«ã¤ãã¦èª¿ã¹ãã®ã§ã¾ã¨ããã
æ¦è¦
gzipï¼ã¸ã¼ã»ã¸ããï¼ã¯ããã¼ã¿å§ç¸®ããã°ã©ã ã®ã²ã¨ã¤ãããã³ãã®å§ç¸®ãã¼ã¿ã®ãã©ã¼ãããã§ããã
圧縮を有効にする | PageSpeed Insights | Google Developers
ææ°ã®ãã©ã¦ã¶ã¯ãã¹ã¦ gzip å§ç¸®ã«å¯¾å¿ãã¦ããããã¹ã¦ã® HTTP ãªã¯ã¨ã¹ãã§èªåçã«å§ç¸®ã®ãã´ã·ã¨ã¼ã·ã§ã³ãå®æ½ãã¾ããgzip å§ç¸®ãæå¹ã«ããã¨ã転éãããã¬ã¹ãã³ã¹ã®ãµã¤ãºãæ大㧠90% åæ¸ãããããããªã½ã¼ã¹ã®ãã¦ã³ãã¼ãæéã®å¤§å¹ ãªç縮ãã¯ã©ã¤ã¢ã³ãã®ãã¼ã¿ä½¿ç¨éã®åæ¸ãæåã®ãã¼ã¸ ã¬ã³ããªã³ã°æéã®æ¹åã¨ãã£ãå¹æãããã¾ã
æ¦è¦ã¯ä¸è¨ã®èª¬æã«ä»»ããã
ä»çµã¿
gzipå§ç¸®ã®æ©æµãåããããã«ã¯ãµã¼ãã¼ã¨ã¯ã©ã¤ã¢ã³ãã®ä¸¡æ¹ãå§ç¸®ã¢ã«ã´ãªãºã ã«å¯¾å¿ãã¦ããå¿ è¦ãããã
ã¯ã©ã¤ã¢ã³ããgzipå§ç¸®ãè¦æ±ããå ´åã¯ããªã¯ã¨ã¹ããããã¼ã§ Accept-Encoding: gzip
ãéãã
ããã¦ããµã¼ãã¼ãã¬ã¹ãã³ã¹ãgzipå§ç¸®ããã¬ã¹ãã³ã¹ãããã¼ã§ Content-Encoding: gzip
ãè¿ãã
å®è£
ãµã¼ãã¼ãµã¤ã
Node.jsã§ãµã¼ãã¼ãµã¤ãã®å®è£ ãããå ´åãcompressionã¨ããããã«ã¦ã§ã¢ããã使ãããã
const compression = require('compression'); const express = require('express'); const app = express(); app.use(compression());
ä¸è¨ã®ããã«ãapp.use
ããã ãã§gzipå§ç¸®ãããã®ã§ç°¡åããã¡ããç´°ããè¨å®ãã§ããã
ã¯ã©ã¤ã¢ã³ã
https://github.com/sindresorhus/got#comparison ã«ããã¨ãæåãªHTTPã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªã¯ã©ããgzipå§ç¸®ã®ãã³ããªã³ã°ã«å¯¾å¿ãã¦ããã
axios
axiosã¯ãã©ã¦ã¶ããã®ãªã¯ã¨ã¹ãã«ã¯XMLHttpRequestãå©ç¨ãã¦ãããXHRã§ã¯Accept-Encodingããªã¯ã¨ã¹ããããã¼ã«è¨å®ãããã¨ãç¦æ¢ããã¦ããã
https://fetch.spec.whatwg.org/#forbidden-header-name
axiosã§ä»¥ä¸ã®ãããªã³ã¼ããæ¸ããå ´åã
axios.get("/users", { headers: { "Accept-Encoding": "gzip" } })
ãã©ã¦ã¶ã§ã¯ã³ã³ã½ã¼ã«ã«ã¨ã©ã¼ãåºåãããã
Refused to set unsafe header "Accept-Encoding"
XHRã§ã®Accept-Encodingãããã¼ã¯ã¦ã¼ã¶ã¼ã¨ã¼ã¸ã§ã³ãã«å§ãããã¦ããã
ãã£ã¦ãaxiosã§gzipå§ç¸®ãããã¬ã¹ãã³ã¹ãåãåãããå ´åã¯ãNode.jsã§åãç®æã«ã ã Accept-Encoding: gzip
ãè¨å®ããå¿
è¦ããããã¾ããdecompressãªãã·ã§ã³ãtrueã«ãããã¨ã§ã¬ã¹ãã³ã¹ããã£ã解åããContent-Encodingãããã¼ãåé¤ãããContent-Encodingãããã¼ãåé¤ããã®ã¯axiosã§è§£åããã¬ã¹ãã³ã¹ãããã«è§£åãã¦ãã¾ãã®ãé¿ããããã ã¨èããããã
got
gotã§gzipå§ç¸®ãå©ç¨ãããå ´åã¯decompressãªãã·ã§ã³ã«trueãè¨å®ããã
https://github.com/sindresorhus/got/blob/main/documentation/2-options.md#decompress
decompressãtrueã®å ´åã¯Accept-Encodingã«gzipãè¨å®ãã¦ããã
ã¾ããContent-Encodingãgzipã®å ´åã¯ã¬ã¹ãã³ã¹ã®ããã£ããã¼ã¹ãã¦ããã
superagent
https://github.com/visionmedia/superagent/blob/master/docs/index.md#compression
Nodeã®å ´åã¯ä½ãããªãã¦ãå§ç¸®ãããã¬ã¹ãã³ã¹ããµãã¼ããã¦ããã¨ããã
HEADã¡ã½ããã§ãªãå ´åã¯ãAccept-Encodingã«gzipãè¨å®ãã¦ããã
Content-Encodingãgzipã®å ´åã¯shouldUnzipã§trueãè¿ããunzipã§è§£åãã¦ããã
éæ
- gzipå§ç¸®ã®ä»çµã¿ã¨å®è£ æ¹æ³ã調ã¹ã
- ãµã¼ãã¼ãµã¤ãã®å®è£ æ¹æ³ã¯ç¥ã£ã¦ããã®ã§ãã¯ã©ã¤ã¢ã³ãå´ã®å®è£ æ¹æ³ã«ã¤ãã¦éç¹çã«èª¿ã¹ã
- 3ã¤ã®ã©ã¤ãã©ãªã調ã¹ãããããããgzipå§ç¸®ã®å¯¾å¿æ¹æ³ãç°ãªãé¢ç½ãã£ã
- axiosã¯å©ç¨è ãAccept-Encodingã渡ãå¿ è¦ããããdecompressãªãã·ã§ã³ã§è§£åãããã©ãããå¶å¾¡ãã¦ãã
- gotã¯decompressãªãã·ã§ã³ã§Accept-Encodingã®è¨å®ã¨è§£åã®ä¸¡æ¹ãå¶å¾¡ãã¦ããï¼headersã§Accept-Encodingã渡ããã¨ãã§ãããï¼
- superagentã¯ããã©ã«ãã§Accept-Encodingã®è¨å®ã¨è§£åããµãã¼ããã¦ãã