From 25ff248b92e3243ad03261d6db4b5673cf61da86 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Thu, 29 Oct 2026 21:53:08 -0700 Subject: [PATCH] main, begin to minify HTML. --- main.js | 25 +++++++++++++++++++++---- package.json | 3 ++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index a0cf85c..e35f4ae 100755 --- a/main.js +++ b/main.js @@ -14,6 +14,8 @@ const execSync = require('child_process').execSync; const fs = require('fs'); +const { minify } = require('html-minifier-terser'); + //// Global Variables //// const DEBUG = false; @@ -44,6 +46,18 @@ const stringsToRemove = [ //// Functions //// +async function mini(html) { + //console.log(`html=${html}`); + const minified = await minify(html, { + collapseWhitespace: true, + removeComments: true, + minifyJS: true, + minifyCSS: true + }); + //console.log(`minified=${minified}`); + return minified.toString(); +} + /* Code exists inside a main function so that we may use async/await. */ async function main() { @@ -149,12 +163,13 @@ async function main() { // TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE" for (let key in pages) { console.log(" * Creating router for", key); - router.get("/" + key, function (req,res) { + router.get("/" + key, async function (req,res) { console.log(key, "fulfilling request to", req.socket.remoteAddress, "asking for", req.url); let html = "" + execSync(pages[key]); stringsToRemove.forEach(string => { html = html.replace(string, ""); }); + html = await mini(html); res.send(html); }); @@ -199,9 +214,10 @@ async function main() { // Provide human-usable sitemap links. // TBD/TODO would be great to log this as "DATE: ADDRESS hit ROUTER requesting RESOURCE" console.log(" * Creating router for sitemap*"); - router.get('/sitemap*', function (req, res) { + router.get('/sitemap*', async function (req, res) { console.log("sitemap.html being provided to", req.socket.remoteAddress) - res.send(sitemap_html); + html = await mini(sitemap_html); + res.send(mini(html)); }); // Provide a robots.txt for where search engines should find things. @@ -264,7 +280,7 @@ async function main() { // Originally a test, now a catch-all redirection to Home! console.log(" * Creating router for redirection"); - router.get('/*', function (req, res) { + router.get('/*', async function (req, res) { // WARNING: These are huge so only look when you need to. //console.log(req); //console.log(res); @@ -273,6 +289,7 @@ async function main() { stringsToRemove.forEach(string => { html = html.replace(string, ""); }); + html = await mini(html); res.send(html); }); diff --git a/package.json b/package.json index 3dc2f20..5b6a95e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "dependencies": { - "express": ">=4.18.1 < 5.0.0" + "express": ">=4.18.1 < 5.0.0", + "html-minifier-terser": "^7.2.0" }, "scripts": { "start": "./run.sh"