Gzip static file

使用node生成靜態文檔的時候, 部分項目會出現 index-a1adc171.jsindex-a1adc171.js.gz。這樣是方便支持 gzip_static 的情況直接調用 index-a1adc171.js.gz 而不用 server 再次壓縮。

例如使用 插件 vite-plugin-compression:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import compression from "vite-plugin-compression"

export default defineConfig({
  plugins: [
    react(),
    compression(),
  ],
})

要實現以上功能,還需Http Server 設定。

Nginx設定:

gzip_static  on;
gzip_proxied expired no-cache no-store private auth;

Apache設定:

# AddEncoding allows you to have certain browsers uncompress information on the fly.
AddEncoding gzip .gz

#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]

# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]

# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]