From fd9331dc86e7f765ef49e6e7624c4878d073556b Mon Sep 17 00:00:00 2001 From: Nathan Chase Date: Sat, 17 Dec 2022 12:15:07 -0500 Subject: [PATCH 1/9] include only compressible mime types --- src/compress.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/compress.ts b/src/compress.ts index 6f74109f59..c690d07e37 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -72,5 +72,24 @@ function isTextMime(mimeType: string) { } function isCompressableMime(mimeType: string) { - return /image|text|font|json|xml|javascript/.test(mimeType); + const compressibleMimeTypes = [ + 'atom', + 'css', + 'eot', + 'htc', + 'html', + 'ico', + 'js', + 'json', + 'mjs', + 'otf', + 'rss', + 'svg', + 'text', + 'ttf', + 'webmanifest', + 'xml', + ]; + + return compressibleMimeTypes.includes(mimeType); } From c3d4f70eb6a2ff46d6c371a7cc0e1f447911d4eb Mon Sep 17 00:00:00 2001 From: Nathan Chase Date: Sat, 17 Dec 2022 12:25:41 -0500 Subject: [PATCH 2/9] docs: include list of compressible mime types --- docs/content/3.config/index.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/content/3.config/index.md b/docs/content/3.config/index.md index 60e7c00e5c..9622510bf4 100644 --- a/docs/content/3.config/index.md +++ b/docs/content/3.config/index.md @@ -92,6 +92,35 @@ 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: + +- text/html - `.html` +- text/plain - `.txt` +- text/css - `.css` +- text/xml - `.xml` +- text/x-component - `.htc` +- text/javascript - `.js` +- application/javascript - `.js` +- application/x-javascript - `.js` +- application/json - `.json` +- application/vnd.api+json - `.json` +- application/manifest+json - `.webmanifest` +- application/xml - `.xml` +- application/xhtml+xml - `.xhtml` +- application/rss+xml - `.rss` +- application/atom+xml - `.atom` +- image/svg+xml - `.svg` +- image/x-icon - `.ico` +- image/vnd.microsoft.icon - `.ico` +- font/ttf - `.ttf` +- application/x-font-ttf - `.ttf` +- application/x-font-truetype - `.ttf` +- font/eot - `.eot` +- application/vnd.ms-fontobject - `.eot` +- font/otf - `.otf` +- font/opentype - `.otf` +- application/x-font-opentype - `.otf` + ## `serverAssets` Assets can be accessed in server logic and bundled in production. From 04c70b48273a849264b6c39f35cdfcd653052699 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 17 Dec 2022 18:31:59 +0000 Subject: [PATCH 3/9] refactor: extract list of types --- src/compress.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compress.ts b/src/compress.ts index c690d07e37..ae23a7122a 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -71,7 +71,6 @@ function isTextMime(mimeType: string) { return /text|javascript|json|xml/.test(mimeType); } -function isCompressableMime(mimeType: string) { const compressibleMimeTypes = [ 'atom', 'css', @@ -91,5 +90,6 @@ function isCompressableMime(mimeType: string) { 'xml', ]; +function isCompressableMime(mimeType: string) { return compressibleMimeTypes.includes(mimeType); } From 4492656d00747ab4b1d92ef676ad2f420936340f Mon Sep 17 00:00:00 2001 From: Nathan Chase Date: Sat, 17 Dec 2022 13:53:05 -0500 Subject: [PATCH 4/9] refactored and simplified to regex --- src/compress.ts | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/src/compress.ts b/src/compress.ts index ae23a7122a..3275d57c0a 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -71,25 +71,8 @@ function isTextMime(mimeType: string) { return /text|javascript|json|xml/.test(mimeType); } - const compressibleMimeTypes = [ - 'atom', - 'css', - 'eot', - 'htc', - 'html', - 'ico', - 'js', - 'json', - 'mjs', - 'otf', - 'rss', - 'svg', - 'text', - 'ttf', - 'webmanifest', - 'xml', - ]; +const compressibleMimeTypesRegex = /atom|css|eot|htc|html|ico|js|json|mjs|otf|rss|svg|text|ttf|webmanifest|xml/ function isCompressableMime(mimeType: string) { - return compressibleMimeTypes.includes(mimeType); + return compressibleMimeTypesRegex.test(mimeType); } From dab382258dbf13f55de202ab043c4f7f625a53a1 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 17 Dec 2022 20:46:05 +0000 Subject: [PATCH 5/9] refactor: rename re --- src/compress.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compress.ts b/src/compress.ts index 3275d57c0a..eab5378819 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -71,8 +71,8 @@ function isTextMime(mimeType: string) { return /text|javascript|json|xml/.test(mimeType); } -const compressibleMimeTypesRegex = /atom|css|eot|htc|html|ico|js|json|mjs|otf|rss|svg|text|ttf|webmanifest|xml/ +const COMPRESSIBLE_MIMES_RE = /atom|css|eot|htc|html|ico|js|json|mjs|otf|rss|svg|text|ttf|webmanifest|xml/ function isCompressableMime(mimeType: string) { - return compressibleMimeTypesRegex.test(mimeType); + return COMPRESSIBLE_MIMES_RE.test(mimeType); } From b8b4fdb44bb8ef7146b870acbad04d0489cf44eb Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 17 Dec 2022 20:47:20 +0000 Subject: [PATCH 6/9] style: add semi --- src/compress.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compress.ts b/src/compress.ts index eab5378819..b8bd900c9c 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -71,7 +71,7 @@ function isTextMime(mimeType: string) { return /text|javascript|json|xml/.test(mimeType); } -const COMPRESSIBLE_MIMES_RE = /atom|css|eot|htc|html|ico|js|json|mjs|otf|rss|svg|text|ttf|webmanifest|xml/ +const COMPRESSIBLE_MIMES_RE = /atom|css|eot|htc|html|ico|js|json|mjs|otf|rss|svg|text|ttf|webmanifest|xml/; function isCompressableMime(mimeType: string) { return COMPRESSIBLE_MIMES_RE.test(mimeType); From 3cb1219fd5af2996f0651fdad0cd2ef0c7026255 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 17 Dec 2022 20:53:45 +0000 Subject: [PATCH 7/9] style: add line break --- src/compress.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compress.ts b/src/compress.ts index b8bd900c9c..67898c2af3 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -71,7 +71,8 @@ function isTextMime(mimeType: string) { return /text|javascript|json|xml/.test(mimeType); } -const COMPRESSIBLE_MIMES_RE = /atom|css|eot|htc|html|ico|js|json|mjs|otf|rss|svg|text|ttf|webmanifest|xml/; +const COMPRESSIBLE_MIMES_RE = + /atom|css|eot|htc|html|ico|js|json|mjs|otf|rss|svg|text|ttf|webmanifest|xml/; function isCompressableMime(mimeType: string) { return COMPRESSIBLE_MIMES_RE.test(mimeType); From 539d8ea3337d7318ff7776d3dd045ec178ca8570 Mon Sep 17 00:00:00 2001 From: Nathan Chase Date: Mon, 19 Dec 2022 09:06:00 -0500 Subject: [PATCH 8/9] replaced with set of AWS MIME types --- src/compress.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/src/compress.ts b/src/compress.ts index 67898c2af3..a4c906fd6e 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -71,9 +71,57 @@ function isTextMime(mimeType: string) { return /text|javascript|json|xml/.test(mimeType); } -const COMPRESSIBLE_MIMES_RE = - /atom|css|eot|htc|html|ico|js|json|mjs|otf|rss|svg|text|ttf|webmanifest|xml/; +// 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 COMPRESSIBLE_MIMES_RE.test(mimeType); + return COMPRESSIBLE_MIMES_RE.has(mimeType); } From d5a9c72d4cbc035dd05627504673b9a324039ea2 Mon Sep 17 00:00:00 2001 From: Nathan Chase Date: Mon, 19 Dec 2022 09:13:30 -0500 Subject: [PATCH 9/9] updated list of compressible MIME types --- docs/content/3.config/index.md | 72 ++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/docs/content/3.config/index.md b/docs/content/3.config/index.md index 9622510bf4..295ecd612c 100644 --- a/docs/content/3.config/index.md +++ b/docs/content/3.config/index.md @@ -94,32 +94,52 @@ larger than 1024 bytes into the public directory. The best compression level is The compressible MIME types are: -- text/html - `.html` -- text/plain - `.txt` -- text/css - `.css` -- text/xml - `.xml` -- text/x-component - `.htc` -- text/javascript - `.js` -- application/javascript - `.js` -- application/x-javascript - `.js` -- application/json - `.json` -- application/vnd.api+json - `.json` -- application/manifest+json - `.webmanifest` -- application/xml - `.xml` -- application/xhtml+xml - `.xhtml` -- application/rss+xml - `.rss` -- application/atom+xml - `.atom` -- image/svg+xml - `.svg` -- image/x-icon - `.ico` -- image/vnd.microsoft.icon - `.ico` -- font/ttf - `.ttf` -- application/x-font-ttf - `.ttf` -- application/x-font-truetype - `.ttf` -- font/eot - `.eot` -- application/vnd.ms-fontobject - `.eot` -- font/otf - `.otf` -- font/opentype - `.otf` -- application/x-font-opentype - `.otf` +- 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`