共计 3748 个字符,预计需要花费 10 分钟才能阅读完成。
1. 开发环境 react18+webpack4
2. 电脑系统 windows11 专业版
3. 在我的项目打包的过程中, 咱们发现打进去的包比拟大, 而且我的项目加载比较慢 [比方首页], 解决办法是应用 compression-webpack-plugin 进行打包压缩 gzip。
4. 所有古代浏览器都反对 gzip 压缩, 启用 gzip 压缩可扩大和扩大传输资源大小, 从而缩短资源下载工夫, 缩小首次白屏工夫, 晋升用户体验。gzip 对基于文本格式文件的压缩成果最好(如:CSS,JavaScript 和 HTML), 在压缩压缩文件时往往可实现高达 70-90%的压缩率, 对曾经压缩过的资源(如:图片)进行 gzip 压缩解决, 成果很不好。
npm i compression-webpack-plugin@5 --save
留神,这里不能间接下载, 须要下载低版本的。间接下载就是最新版的了, 我的项目可能临时不反对最新版的, | |
所以就会报错:TypeError: Cannot read property 'tapPromise' of undefined。 |
const CompressionPlugin = require('compression-webpack-plugin'); | |
plugins: [ | |
new CompressionPlugin({ | |
// gzip 压缩配置 | |
algorithm: 'gzip', | |
test: /\.(js|css)$/, // 匹配文件名 | |
threshold: 10240, // 对超过 10kb 的数据进行压缩 | |
deleteOriginalAssets: true, // 是否删除原文件 | |
}), | |
] |
这里配置完当前,临时还不能应用,还须要后端做一下配置,这里后端以 nginx 为例
5. 次要配置如下图:
#user nobody; | |
worker_processes 1; | |
#error_log logs/error.log; | |
#error_log logs/error.log notice; | |
#error_log logs/error.log info; | |
#pid logs/nginx.pid; | |
events {worker_connections 1024;} | |
http { | |
include mime.types; | |
default_type application/octet-stream; | |
#log_format main '$remote_addr - $remote_user [$time_local]"$request" ' | |
# '$status $body_bytes_sent"$http_referer" ' | |
# '"$http_user_agent" "$http_x_forwarded_for"'; | |
#access_log logs/access.log main; | |
sendfile on; | |
#tcp_nopush on; | |
#keepalive_timeout 0; | |
keepalive_timeout 65; | |
#gzip on; | |
server { | |
listen 9000; | |
server_name localhost; | |
#charset koi8-r; | |
#access_log logs/host.access.log main; | |
location / { | |
root D:/dist; | |
index index.html index.htm; | |
} | |
location /api {proxy_pass http://localhost:3000;} | |
# 次要是下方的 gizp 配置哦,间接复制粘贴就能够应用啦,亲测无效哦 | |
gzip on; # 开启 gzip 压缩 | |
gzip_min_length 4k; # 小于 4k 的文件不会被压缩,大于 4k 的文件才会去压缩 | |
gzip_buffers 16 8k; # 解决申请压缩的缓冲区数量和大小,比方 8k 为单位申请 16 倍内存空间;应用默认即可,不必批改 | |
gzip_http_version 1.1; # 晚期版本 http 不反对,指定默认兼容,不必批改 | |
gzip_comp_level 2; # gzip 压缩级别,1-9,实践上数字越大压缩的越好,也越占用 CPU 工夫。实际上超过 2 的再压缩,只能压缩一点点了,然而 cpu 确是有点节约。因为 2 就够用了 | |
# 压缩的文件类型 MIME 类型,百度一下,一大把 # css # xml # 辨认 php # 图片 | |
gzip_types text/plain application/x-javascript application/javascript text/javascript text/css application/xml application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/x-woff font/ttf; | |
# text # 晚期 js # js # js 的另一种写法 # .eot 字体 # woff 字体 # ttf 字体 | |
gzip_vary on; # 是否在 http header 中增加 Vary: Accept-Encoding,个别状况下倡议开启 | |
gzip_static on; | |
#error_page 404 /404.html; | |
# redirect server error pages to the static page /50x.html | |
# | |
error_page 500 502 503 504 /50x.html; | |
location = /50x.html {root html;} | |
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 | |
# | |
#location ~ \.php$ { | |
# proxy_pass http://127.0.0.1; | |
#} | |
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 | |
# | |
#location ~ \.php$ { | |
# root html; | |
# fastcgi_pass 127.0.0.1:9000; | |
# fastcgi_index index.php; | |
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; | |
# include fastcgi_params; | |
#} | |
# deny access to .htaccess files, if Apache's document root | |
# concurs with nginx's one | |
# | |
#location ~ /\.ht { | |
# deny all; | |
#} | |
} | |
# another virtual host using mix of IP-, name-, and port-based configuration | |
# | |
#server { | |
# listen 8000; | |
# listen somename:8080; | |
# server_name somename alias another.alias; | |
# location / { | |
# root html; | |
# index index.html index.htm; | |
# } | |
#} | |
# HTTPS server | |
# | |
#server { | |
# listen 443 ssl; | |
# server_name localhost; | |
# ssl_certificate cert.pem; | |
# ssl_certificate_key cert.key; | |
# ssl_session_cache shared:SSL:1m; | |
# ssl_session_timeout 5m; | |
# ssl_ciphers HIGH:!aNULL:!MD5; | |
# ssl_prefer_server_ciphers on; | |
# location / { | |
# root html; | |
# index index.html index.htm; | |
# } | |
#} | |
} |
// 要害 配置 | |
# 次要是下方的 gizp 配置哦, 间接复制粘贴就能够应用啦, 亲测无效哦 | |
gzip on; # 开启 gzip 压缩 | |
gzip_min_length 4k; # 小于 4k 的文件不会被压缩, 大于 4k 的文件才会去压缩 | |
gzip_buffers 16 8k; # 解决申请压缩的缓冲区数量和大小, 比方 8k 为单位申请 16 倍内存空间; 应用默认即可, 不必批改 | |
gzip_http_version 1.1; # 晚期版本 http 不反对, 指定默认兼容,不必批改 | |
gzip_comp_level 2; # gzip 压缩级别,1-9, 实践上数字越大压缩的越好, 也越占用 CPU 工夫。实际上超过 2 的再压缩, 只能压缩一点点了, 然而 cpu 确是有点节约。因为 2 就够用了 | |
# 压缩的文件类型 MIME 类型, 百度一下,一大把 # css # xml # 辨认 php # 图片 | |
gzip_types text/plain application/x-javascript application/javascript text/javascript text/css application/xml application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/x-woff font/ttf; | |
# text # 晚期 js # js # js 的另一种写法 # .eot 字体 # woff 字体 # ttf 字体 | |
gzip_vary on; # 是否在 http header 中增加 Vary: Accept-Encoding,个别状况下倡议开启 | |
gzip_static on; |
6. 本期的分享到了这里就完结啦, 心愿对你有所帮忙, 让咱们一起致力走向巅峰。
正文完