
Security News
GitHub Actions Pricing Whiplash: Self-Hosted Actions Billing Change Postponed
GitHub postponed a new billing model for self-hosted Actions after developer pushback, but moved forward with hosted runner price cuts on January 1.
vite-plugin-compression2
Advanced tools
$ yarn add vite-plugin-compression2 -D
# or
$ npm install vite-plugin-compression2 -D
# or
$ pnpm add vite-plugin-compression2 -D
import { defineConfig } from 'vite'
import { compression } from 'vite-plugin-compression2'
export default defineConfig({
plugins: [
// ...your plugins
compression()
]
})
import { compression, defineAlgorithm } from 'vite-plugin-compression2'
export default defineConfig({
plugins: [
compression({
algorithms: [
'gzip',
'brotliCompress',
defineAlgorithm('deflate', { level: 9 })
]
})
]
})
import { compression, defineAlgorithm } from 'vite-plugin-compression2'
export default defineConfig({
plugins: [
compression({
algorithms: [
defineAlgorithm(
async (buffer, options) => {
// Your custom compression logic
return compressedBuffer
},
{ customOption: true }
)
]
})
]
})
import { compression, tarball } from 'vite-plugin-compression2'
export default defineConfig({
plugins: [
compression(),
// If you want to create a tarball archive, use tarball plugin after compression
tarball({ dest: './dist/archive' })
]
})
| params | type | default | description |
|---|---|---|---|
include | string | RegExp | Array<string | RegExp> | /\.(html|xml|css|json|js|mjs|svg|yaml|yml|toml)$/ | Include all assets matching any of these conditions. |
exclude | string | RegExp | Array<string | RegExp> | - | Exclude all assets matching any of these conditions. |
threshold | number | 0 | Only assets bigger than this size are processed (in bytes) |
algorithms | Algorithms | ['gzip', 'brotliCompress'] | Array of compression algorithms or defineAlgorithm results |
filename | string | function | [path][base].gz or [path][base]. br If algorithm is zstandard be [path][base].zst | The target asset filename pattern |
deleteOriginalAssets | boolean | false | Whether to delete the original assets or not |
skipIfLargerOrEqual | boolean | true | Whether to skip the compression if the result is larger than or equal to the original file |
logLevel | string | info | Control sdout info |
artifacts | function | undefined | Sometimes you need to copy something to the final output. This option may help you. |
| params | type | default | description |
|---|---|---|---|
dest | string | - | Destination directory for tarball |
defineAlgorithm(algorithm, options?)Define a compression algorithm with options.
Parameters:
algorithm: Algorithm name ('gzip' | 'brotliCompress' | 'deflate' | 'deflateRaw' | 'zstandard' | 'gz' | 'br' | 'brotli' | 'zstd') or custom functionoptions: Compression options for the algorithmReturns: [algorithm, options] tuple
Examples:
// Built-in algorithm with default options
defineAlgorithm('gzip')
// Built-in algorithm with custom options
defineAlgorithm('gzip', { level: 9 })
// Brotli with custom quality
defineAlgorithm('brotliCompress', {
params: {
[require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
}
})
// Custom algorithm function
defineAlgorithm(
async (buffer, options) => {
// Your compression implementation
return compressedBuffer
},
{ customOption: 'value' }
)
| Algorithm | Aliases | Extension | Node.js Support | Description |
|---|---|---|---|---|
gzip | gz | .gz | All versions | Standard gzip compression with good balance of speed and ratio |
brotliCompress | brotli, br | .br | All versions | Brotli compression with better compression ratio than gzip |
deflate | - | .gz | All versions | Deflate compression algorithm |
deflateRaw | - | .gz | All versions | Raw deflate compression without headers |
zstandard | zstd | .zst | >= 22.15.0 or >= 23.8.0 | Zstandard compression with excellent speed/ratio balance |
| Custom Function | - | Custom | All versions | Your own compression algorithm implementation |
The algorithms option accepts:
type Algorithms =
| Algorithm[] // ['gzip', 'brotliCompress']
| DefineAlgorithmResult[] // [defineAlgorithm('gzip'), ...]
| (Algorithm | DefineAlgorithmResult)[] // Mixed array
If you're upgrading from v1.x, please check the Migration Guide.
compression({
algorithms: ['gzip']
})
compression({
algorithms: [
defineAlgorithm('gzip', { level: 9 }),
defineAlgorithm('brotliCompress', {
params: {
[require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
}
})
]
})
compression({
algorithms: ['gzip'],
filename: '[path][base].[hash].gz'
})
compression({
algorithms: ['gzip'],
deleteOriginalAssets: true
})
compression({
algorithms: ['gzip'],
threshold: 1000 // Only compress files larger than 1KB
})
After building your project with compressed assets, you need to configure your web server to serve these pre-compressed files.
http {
# Enable gzip_static module to serve pre-compressed .gz files
gzip_static on;
# Enable brotli_static to serve pre-compressed .br files
# Requires ngx_brotli module: https://github.com/google/ngx_brotli
brotli_static on;
# Fallback to dynamic compression if static file not found
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
server {
listen 80;
server_name example.com;
root /var/www/html;
location / {
try_files $uri $uri/ /index.html;
}
}
}
# Enable mod_deflate for fallback dynamic compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>
# Serve pre-compressed files
<IfModule mod_rewrite.c>
RewriteEngine On
# Serve .br file if it exists and client supports brotli
RewriteCond %{HTTP:Accept-Encoding} br
RewriteCond %{REQUEST_FILENAME}.br -f
RewriteRule ^(.*)$ $1.br [L]
# Serve .gz file if it exists and client supports gzip
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [L]
</IfModule>
# Set correct content-type and encoding headers
<FilesMatch "\.js\.gz$">
Header set Content-Type "application/javascript"
Header set Content-Encoding "gzip"
</FilesMatch>
<FilesMatch "\.css\.gz$">
Header set Content-Type "text/css"
Header set Content-Encoding "gzip"
</FilesMatch>
<FilesMatch "\.js\.br$">
Header set Content-Type "application/javascript"
Header set Content-Encoding "br"
</FilesMatch>
<FilesMatch "\.css\.br$">
Header set Content-Type "text/css"
Header set Content-Encoding "br"
</FilesMatch>
For Next.js projects, add the plugin to your Vite configuration if using the App Router with custom server:
// vite.config.js (for custom Next.js setups)
import { defineConfig } from 'vite'
import { compression } from 'vite-plugin-compression2'
export default defineConfig({
plugins: [
compression({
algorithms: ['gzip', 'brotliCompress'],
threshold: 1024
})
]
})
For standard Next.js builds, configure in next.config.js:
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
// Next.js handles compression differently
// Use this plugin with custom server or static export
output: 'export', // For static export
compress: false // Disable Next.js built-in compression to use pre-compressed files
}
module.exports = nextConfig
// nuxt.config.ts
import { compression } from 'vite-plugin-compression2'
export default defineNuxtConfig({
vite: {
plugins: [
compression({
algorithms: ['gzip', 'brotliCompress'],
threshold: 1024,
exclude: [/\.map$/, /stats\.html$/]
})
]
}
})
// vite.config.js
import { sveltekit } from '@sveltejs/kit/vite'
import { compression } from 'vite-plugin-compression2'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
sveltekit(),
compression({
algorithms: ['gzip', 'brotliCompress'],
threshold: 1024
})
]
})
// astro.config.mjs
import { defineConfig } from 'astro/config'
import { compression } from 'vite-plugin-compression2'
export default defineConfig({
vite: {
plugins: [
compression({
algorithms: ['gzip', 'brotliCompress']
})
]
}
})
Choose compression levels based on your deployment strategy:
compression({
algorithms: [
// Development: faster builds, lower compression
defineAlgorithm('gzip', { level: 6 }), // Default level
// Production: slower builds, better compression
defineAlgorithm('gzip', { level: 9 }), // Maximum compression
// Brotli: quality 10-11 recommended for static assets
defineAlgorithm('brotliCompress', {
params: {
[require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
}
})
]
})
Recommendations:
Only compress files that benefit from compression:
compression({
threshold: 1024, // 1KB minimum - recommended
algorithms: ['gzip', 'brotliCompress']
})
Why use a threshold?
Use both gzip and brotli for maximum compatibility and performance:
compression({
algorithms: [
defineAlgorithm('gzip', { level: 9 }), // Wide browser support (all browsers)
defineAlgorithm('brotliCompress', { // Better compression (modern browsers)
params: {
[require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
}
})
]
})
Benefits:
Accept-Encoding headerCompress only specific file types for optimal results:
compression({
include: [/\.(js|mjs|json|css|html|svg)$/], // Text-based files
exclude: [/\.(png|jpg|jpeg|gif|webp|woff|woff2)$/], // Already compressed formats
threshold: 1024
})
File types that compress well:
.js, .mjs, .ts).css).html).json).svg).xml)File types to skip:
.png, .jpg, .webp) - already compressed.woff, .woff2) - already compressed.mp4, .webm) - already compressedtarball option dest means to generate a tarball somewheretarball is based on the ustar format. It should be compatible with all popular tar distributions (gnutar, bsdtar etc)Note: If you try to use zstd compression on an unsupported Node.js version, the plugin will throw a helpful error message indicating the required version.
FAQs
a fast vite compression plugin
The npm package vite-plugin-compression2 receives a total of 61,477 weekly downloads. As such, vite-plugin-compression2 popularity was classified as popular.
We found that vite-plugin-compression2 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub postponed a new billing model for self-hosted Actions after developer pushback, but moved forward with hosted runner price cuts on January 1.

Research
Destructive malware is rising across open source registries, using delays and kill switches to wipe code, break builds, and disrupt CI/CD.

Security News
Socket CTO Ahmad Nassri shares practical AI coding techniques, tools, and team workflows, plus what still feels noisy and why shipping remains human-led.