Improve performance serving minified html

This commit is contained in:
Danny Spina 2023-06-20 13:51:05 +02:00
parent 145a39045f
commit be660aff98
3 changed files with 102 additions and 2 deletions

View File

@ -8,10 +8,22 @@ import { parseHTML } from "linkedom";
// @ts-ignore // @ts-ignore
import XHR2 from "xhr2"; import XHR2 from "xhr2";
const XMLHttpRequest = XHR2.XMLHttpRequest; const XMLHttpRequest = XHR2.XMLHttpRequest;
import { minify } from "html-minifier";
const app = express(); const app = express();
const port = 8888; const port = 8888;
const minifierOptions = {
collapseWhitespace: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeTagWhitespace: true,
useShortDoctype: true,
minifyCSS: true,
};
// @ts-ignore // @ts-ignore
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
@ -84,7 +96,9 @@ app.get("/", async (req, res) => {
</html> </html>
`; `;
res.send(html); const minifiedSerp = minify(html, minifierOptions);
res.send(minifiedSerp);
}; };
xhr.send(); xhr.send();
} catch (err) { } catch (err) {
@ -114,7 +128,19 @@ app.get("/blazed", async (req, res) => {
return res.send("Something went wrong"); return res.send("Something went wrong");
} }
res.send(article.content); const blazedPage = `<html><head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<style>body {font-family: sans-serif}</style>
</head>
<body>
${article.content}
</body></html>
`;
const minifiedBlazedPage = minify(blazedPage, minifierOptions);
res.send(minifiedBlazedPage);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }

73
package-lock.json generated
View File

@ -22,6 +22,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
"@types/html-minifier": "^4.0.2",
"@types/jsdom": "^21.1.1", "@types/jsdom": "^21.1.1",
"@types/node": "^20.3.1", "@types/node": "^20.3.1",
"concurrently": "^8.2.0", "concurrently": "^8.2.0",
@ -81,6 +82,16 @@
"@types/node": "*" "@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": { "node_modules/@types/connect": {
"version": "3.4.35", "version": "3.4.35",
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz",
@ -114,6 +125,17 @@
"@types/send": "*" "@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": { "node_modules/@types/http-cache-semantics": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz",
@ -154,6 +176,12 @@
"integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==", "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==",
"dev": true "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": { "node_modules/@types/send": {
"version": "0.17.1", "version": "0.17.1",
"resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.1.tgz", "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.1.tgz",
@ -180,6 +208,15 @@
"integrity": "sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==", "integrity": "sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==",
"dev": true "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": { "node_modules/abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@ -2097,6 +2134,16 @@
"@types/node": "*" "@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": { "@types/connect": {
"version": "3.4.35", "version": "3.4.35",
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz",
@ -2130,6 +2177,17 @@
"@types/send": "*" "@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": { "@types/http-cache-semantics": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz",
@ -2170,6 +2228,12 @@
"integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==", "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==",
"dev": true "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": { "@types/send": {
"version": "0.17.1", "version": "0.17.1",
"resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.1.tgz", "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.1.tgz",
@ -2196,6 +2260,15 @@
"integrity": "sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==", "integrity": "sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==",
"dev": true "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": { "abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",

View File

@ -26,6 +26,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
"@types/html-minifier": "^4.0.2",
"@types/jsdom": "^21.1.1", "@types/jsdom": "^21.1.1",
"@types/node": "^20.3.1", "@types/node": "^20.3.1",
"concurrently": "^8.2.0", "concurrently": "^8.2.0",