From 378b8f9b831f932f425c5041170eb262bf4b52f1 Mon Sep 17 00:00:00 2001 From: Nathan Chase Date: Tue, 20 Dec 2022 10:57:56 -0500 Subject: [PATCH] fix: include only compressible mime types (#761) --- docs/content/3.config/index.md | 49 +++++++++++++++++++++++++++++++ src/compress.ts | 53 +++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/docs/content/3.config/index.md b/docs/content/3.config/index.md index 60e7c00e5c..295ecd612c 100644 --- a/docs/content/3.config/index.md +++ b/docs/content/3.config/index.md @@ -92,6 +92,55 @@ If a `public/` directory is detected, it will be added by default, but you can a 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. +The compressible MIME types are: + +- application/dash+xml +- application/eot +- application/font +- application/font-sfnt +- application/javascript +- application/json +- application/opentype +- application/otf +- application/pkcs7-mime +- application/protobuf +- application/rss+xml +- application/truetype +- application/ttf +- application/vnd.apple.mpegurl +- application/vnd.mapbox-vector-tile +- application/vnd.ms-fontobject +- application/xhtml+xml +- application/xml +- application/x-font-opentype +- application/x-font-truetype +- application/x-font-ttf +- application/x-httpd-cgi +- application/x-javascript +- application/x-mpegurl +- application/x-opentype +- application/x-otf +- application/x-perl +- application/x-ttf +- font/eot +- font/opentype +- font/otf +- font/ttf +- image/svg+xml +- text/css +- text/csv +- text/html +- text/javascript +- text/js +- text/plain +- text/richtext +- text/tab-separated-values +- text/xml +- text/x-component +- text/x-java-source +- text/x-script +- vnd.apple.mpegurl + ## `serverAssets` Assets can be accessed in server logic and bundled in production. diff --git a/src/compress.ts b/src/compress.ts index 6f74109f59..a4c906fd6e 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -71,6 +71,57 @@ function isTextMime(mimeType: string) { return /text|javascript|json|xml/.test(mimeType); } +// Reference list of compressible MIME types from AWS +// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html#compressed-content-cloudfront-file-types +const COMPRESSIBLE_MIMES_RE = new Set([ + "application/dash+xml", + "application/eot", + "application/font", + "application/font-sfnt", + "application/javascript", + "application/json", + "application/opentype", + "application/otf", + "application/pkcs7-mime", + "application/protobuf", + "application/rss+xml", + "application/truetype", + "application/ttf", + "application/vnd.apple.mpegurl", + "application/vnd.mapbox-vector-tile", + "application/vnd.ms-fontobject", + "application/xhtml+xml", + "application/xml", + "application/x-font-opentype", + "application/x-font-truetype", + "application/x-font-ttf", + "application/x-httpd-cgi", + "application/x-javascript", + "application/x-mpegurl", + "application/x-opentype", + "application/x-otf", + "application/x-perl", + "application/x-ttf", + "font/eot", + "font/opentype", + "font/otf", + "font/ttf", + "image/svg+xml", + "text/css", + "text/csv", + "text/html", + "text/javascript", + "text/js", + "text/plain", + "text/richtext", + "text/tab-separated-values", + "text/xml", + "text/x-component", + "text/x-java-source", + "text/x-script", + "vnd.apple.mpegurl" +]); + function isCompressableMime(mimeType: string) { - return /image|text|font|json|xml|javascript/.test(mimeType); + return COMPRESSIBLE_MIMES_RE.has(mimeType); }