Skip to content

Commit

Permalink
feat: improve public asset compression (#466)
Browse files Browse the repository at this point in the history
* Improve compressPublicAssets

* Change sync to async compression

* small refactors

* refactor callback handling

Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
KubesDavid and pi0 authored Sep 5, 2022
1 parent 28e0977 commit 81eef13
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
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

0 comments on commit 81eef13

Please sign in to comment.