From be660aff988a0370161241fd1dd5e3a8042bf527 Mon Sep 17 00:00:00 2001 From: Danny Spina Date: Tue, 20 Jun 2023 13:51:05 +0200 Subject: [PATCH] Improve performance serving minified html --- index.ts | 30 +++++++++++++++++-- package-lock.json | 73 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index 41c5661..8bdc407 100644 --- a/index.ts +++ b/index.ts @@ -8,10 +8,22 @@ import { parseHTML } from "linkedom"; // @ts-ignore import XHR2 from "xhr2"; const XMLHttpRequest = XHR2.XMLHttpRequest; +import { minify } from "html-minifier"; const app = express(); const port = 8888; +const minifierOptions = { + collapseWhitespace: true, + removeComments: true, + removeOptionalTags: true, + removeRedundantAttributes: true, + removeScriptTypeAttributes: true, + removeTagWhitespace: true, + useShortDoctype: true, + minifyCSS: true, +}; + // @ts-ignore const __filename = fileURLToPath(import.meta.url); @@ -84,7 +96,9 @@ app.get("/", async (req, res) => { `; - res.send(html); + const minifiedSerp = minify(html, minifierOptions); + + res.send(minifiedSerp); }; xhr.send(); } catch (err) { @@ -114,7 +128,19 @@ app.get("/blazed", async (req, res) => { return res.send("Something went wrong"); } - res.send(article.content); + const blazedPage = ` + + + + + + ${article.content} + + `; + + const minifiedBlazedPage = minify(blazedPage, minifierOptions); + + res.send(minifiedBlazedPage); } catch (err) { console.log(err); } diff --git a/package-lock.json b/package-lock.json index db4e1f0..8afeb8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ }, "devDependencies": { "@types/express": "^4.17.17", + "@types/html-minifier": "^4.0.2", "@types/jsdom": "^21.1.1", "@types/node": "^20.3.1", "concurrently": "^8.2.0", @@ -81,6 +82,16 @@ "@types/node": "*" } }, + "node_modules/@types/clean-css": { + "version": "4.2.6", + "resolved": "https://registry.npmjs.org/@types/clean-css/-/clean-css-4.2.6.tgz", + "integrity": "sha512-Ze1tf+LnGPmG6hBFMi0B4TEB0mhF7EiMM5oyjLDNPE9hxrPU0W+5+bHvO+eFPA+bt0iC1zkQMoU/iGdRVjcRbw==", + "dev": true, + "dependencies": { + "@types/node": "*", + "source-map": "^0.6.0" + } + }, "node_modules/@types/connect": { "version": "3.4.35", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz", @@ -114,6 +125,17 @@ "@types/send": "*" } }, + "node_modules/@types/html-minifier": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/html-minifier/-/html-minifier-4.0.2.tgz", + "integrity": "sha512-4IkmkXJP/25R2fZsCHDX2abztXuQRzUAZq39PfCMz2loLFj8vS9y7aF6vDl58koXSTpsF+eL4Lc5Y4Aww/GCTQ==", + "dev": true, + "dependencies": { + "@types/clean-css": "*", + "@types/relateurl": "*", + "@types/uglify-js": "*" + } + }, "node_modules/@types/http-cache-semantics": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", @@ -154,6 +176,12 @@ "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==", "dev": true }, + "node_modules/@types/relateurl": { + "version": "0.2.29", + "resolved": "https://registry.npmjs.org/@types/relateurl/-/relateurl-0.2.29.tgz", + "integrity": "sha512-QSvevZ+IRww2ldtfv1QskYsqVVVwCKQf1XbwtcyyoRvLIQzfyPhj/C+3+PKzSDRdiyejaiLgnq//XTkleorpLg==", + "dev": true + }, "node_modules/@types/send": { "version": "0.17.1", "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.1.tgz", @@ -180,6 +208,15 @@ "integrity": "sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==", "dev": true }, + "node_modules/@types/uglify-js": { + "version": "3.17.1", + "resolved": "https://registry.npmjs.org/@types/uglify-js/-/uglify-js-3.17.1.tgz", + "integrity": "sha512-GkewRA4i5oXacU/n4MA9+bLgt5/L3F1mKrYvFGm7r2ouLXhRKjuWwo9XHNnbx6WF3vlGW21S3fCvgqxvxXXc5g==", + "dev": true, + "dependencies": { + "source-map": "^0.6.1" + } + }, "node_modules/abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -2097,6 +2134,16 @@ "@types/node": "*" } }, + "@types/clean-css": { + "version": "4.2.6", + "resolved": "https://registry.npmjs.org/@types/clean-css/-/clean-css-4.2.6.tgz", + "integrity": "sha512-Ze1tf+LnGPmG6hBFMi0B4TEB0mhF7EiMM5oyjLDNPE9hxrPU0W+5+bHvO+eFPA+bt0iC1zkQMoU/iGdRVjcRbw==", + "dev": true, + "requires": { + "@types/node": "*", + "source-map": "^0.6.0" + } + }, "@types/connect": { "version": "3.4.35", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz", @@ -2130,6 +2177,17 @@ "@types/send": "*" } }, + "@types/html-minifier": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/html-minifier/-/html-minifier-4.0.2.tgz", + "integrity": "sha512-4IkmkXJP/25R2fZsCHDX2abztXuQRzUAZq39PfCMz2loLFj8vS9y7aF6vDl58koXSTpsF+eL4Lc5Y4Aww/GCTQ==", + "dev": true, + "requires": { + "@types/clean-css": "*", + "@types/relateurl": "*", + "@types/uglify-js": "*" + } + }, "@types/http-cache-semantics": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", @@ -2170,6 +2228,12 @@ "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==", "dev": true }, + "@types/relateurl": { + "version": "0.2.29", + "resolved": "https://registry.npmjs.org/@types/relateurl/-/relateurl-0.2.29.tgz", + "integrity": "sha512-QSvevZ+IRww2ldtfv1QskYsqVVVwCKQf1XbwtcyyoRvLIQzfyPhj/C+3+PKzSDRdiyejaiLgnq//XTkleorpLg==", + "dev": true + }, "@types/send": { "version": "0.17.1", "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.1.tgz", @@ -2196,6 +2260,15 @@ "integrity": "sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==", "dev": true }, + "@types/uglify-js": { + "version": "3.17.1", + "resolved": "https://registry.npmjs.org/@types/uglify-js/-/uglify-js-3.17.1.tgz", + "integrity": "sha512-GkewRA4i5oXacU/n4MA9+bLgt5/L3F1mKrYvFGm7r2ouLXhRKjuWwo9XHNnbx6WF3vlGW21S3fCvgqxvxXXc5g==", + "dev": true, + "requires": { + "source-map": "^0.6.1" + } + }, "abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", diff --git a/package.json b/package.json index a5a65b6..f154c40 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "devDependencies": { "@types/express": "^4.17.17", + "@types/html-minifier": "^4.0.2", "@types/jsdom": "^21.1.1", "@types/node": "^20.3.1", "concurrently": "^8.2.0",