Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve public asset compression #466

Merged
merged 5 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/content/3.config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ If a `public/` directory is detected, it will be added by default, but you can a

## `compressPublicAssets`

If enabled, Nitro will generate a precompressed (gzip and brotli) verison of all public assets and prerendered routes
bigger than 1024 bytes into public directory. Using this option you can support zero overhead asset compression without using a CDN.
- Default: `{ gzip: false, brotli: false }`

If enabled, Nitro will generate a pre-compressed (gzip and/or brotli) version of supported types of public assets and prerendered routes
larger than 1024 bytes into the public directory. The best compression level is used. Using this option you can support zero overhead asset compression without using a CDN.

## `serverAssets`

Expand Down
51 changes: 46 additions & 5 deletions src/rollup/plugins/public-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,26 @@ export function publicAssets (nitro: Nitro): Plugin {
path: relative(nitro.options.output.serverDir, fullPath)
}

if (nitro.options.compressPublicAssets && assetData.length > 1024 && !assetId.endsWith('.map')) {
for (const encoding of ['gzip', 'br']) {
if (nitro.options.compressPublicAssets && assetData.length > 1024 && !assetId.endsWith('.map') && isTypeCompressible(type)) {
const { gzip, brotli } = nitro.options.compressPublicAssets || {} as any
const encodings = [gzip !== false && 'gzip', brotli !== false && 'br'].filter(Boolean)
for (const encoding of encodings) {
const suffix = '.' + (encoding === 'gzip' ? 'gz' : 'br')
const compressedPath = fullPath + suffix
const gzipOptions = { level: zlib.constants.Z_BEST_COMPRESSION }
const isTextType = type.startsWith('text')
const brotliOptions = {
[zlib.constants.BROTLI_PARAM_MODE]: isTextType ? zlib.constants.BROTLI_MODE_TEXT : zlib.constants.BROTLI_MODE_GENERIC,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: assetData.length
}
const compressedBuff: Buffer = await new Promise((resolve, reject) => {
zlib[encoding === 'gzip' ? 'gzip' : 'brotliCompress'](assetData,
(error, result) => error ? reject(error) : resolve(result)
)
const cb = (error, result: Buffer) => error ? reject(error) : resolve(result)
if (encoding === 'gzip') {
zlib.gzip(assetData, gzipOptions, cb)
} else {
zlib.brotliCompress(assetData, brotliOptions, cb)
}
})
await fsp.writeFile(compressedPath, compressedBuff)
assets[assetId + suffix] = {
Expand Down Expand Up @@ -107,3 +119,32 @@ export function getAsset (id) {
}
}, nitro.vfs)
}

const isTypeCompressible = (type: string): boolean => {
return [
'application/javascript',
'application/json',
'application/manifest+json',
'application/rss+xml',
'application/vnd.ms-fontobject',
'application/x-font-opentype',
'application/x-font-truetype',
'application/x-font-ttf',
'application/x-javascript',
'application/xml',
'application/xhtml+xml',
'font/eot',
'font/opentype',
'font/otf',
'font/truetype',
'image/svg+xml',
'image/vnd.microsoft.icon',
'image/x-icon',
'image/x-win-bitmap',
'text/css',
'text/javascript',
'text/plain',
'text/xml',
'text/x-component'
].includes(type)
}
7 changes: 6 additions & 1 deletion src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export interface DevServerOptions {
watch: string[]
}

export interface CompressOptions {
gzip?: boolean
brotli?: boolean
}

export interface NitroOptions {
// Internal
_config: NitroConfig
Expand Down Expand Up @@ -135,7 +140,7 @@ export interface NitroOptions {
imports: UnimportPluginOptions | false
plugins: string[]
virtual: Record<string, string | (() => string | Promise<string>)>
compressPublicAssets: boolean
compressPublicAssets: boolean | CompressOptions

// Dev
dev: boolean
Expand Down