A gzip, brotli and zstd compressor for Astro
npm install astro-compressor
A gzip, brotli and zstd compressor for Astro
- Simple: Set it and forget it
- Optimal: By compressing ahead of time, a more performant compression can be performed
- Configurable: Allows full configuration for those that require it
- All the compression: brotli, zstd, gzip, oh my
Table of Contents
- Quickstart
- NOTE
- Usage
- Configuration
- License
> [!NOTE]
> This only works for static exports, SSR does not export assets that can be compressed ahead of time so you need to solve it with middleware. See this for more context and a partial solution.
Install via your tool of choice:
``sh`Using NPM
npx astro add astro-compressorUsing Yarn
yarn astro add astro-compressorUsing PNPM
pnpm astro add astro-compressor
To compress your files, simply run pnpm build and look for the compression messages in the build log.
> [!IMPORTANT]
> It is important that this is the last integration in the integrations property to ensure all the generated files are compressed.
First, install the package with your favorite package manager: pnpm add --dev astro-compressor,astro.config.*
then configure it in your file in the integrations property:
`js
import { defineConfig } from "astro/config";
import compressor from "astro-compressor";
export default defineConfig({
// ...
integrations: [..., compressor()],
});
`
You can also optionally enable and/or disable either the gzip or brotli compression by
passing an options object to the compressor:
`js
import { defineConfig } from "astro/config";
import compressor from "astro-compressor";
export default defineConfig({
// ...
integrations: [..., compressor({ gzip: true, brotli: false })],
});
`
If the default settings are not to your liking you can also configure the various
options for each compressor directly instead:
`js
import { defineConfig } from "astro/config";
import compressor from "astro-compressor";
export default defineConfig({
// ...
integrations: [..., compressor({ gzip: { level: 6 }, brotli: { chunkSize: 16 * 512 } })],
});
`
Or customize the file formats that will be compressed:
`js
import { defineConfig } from "astro/config";
import compressor from "astro-compressor";
export default defineConfig({
// ...
integrations: [..., compressor({
fileExtensions: [".html"] // only compress HTML files
})],
});
`
By default, the fileExtensions array is [".css", ".js", ".html", ".xml", ".cjs", ".mjs", ".svg", ".txt"]`.
MIT.