From 72820de13ebf4caa8842f1f7326fec108d9c637f Mon Sep 17 00:00:00 2001 From: Ashley Date: Sat, 11 Mar 2023 14:34:31 +0000 Subject: [PATCH] add minifiycss --- src/libpoketube/init/pages-static.js | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/libpoketube/init/pages-static.js b/src/libpoketube/init/pages-static.js index aaab21ab..46db43b6 100644 --- a/src/libpoketube/init/pages-static.js +++ b/src/libpoketube/init/pages-static.js @@ -62,8 +62,31 @@ module.exports = function (app, config, renderTemplate) { }); }); - app.get("/css/:id", (req, res) => { - res.sendFile(req.params.id, { root: html_location }); - }); +const path = require("path"); +const fs = require("fs"); +const CleanCSS = require("clean-css"); + +const cssDir = "./css/"; + +app.get("/css/:id", (req, res) => { + const filePath = path.join(cssDir, req.params.id); + if (!fs.existsSync(filePath)) { + res.status(404).send("File not found"); + return; + } + + if (req.params.id.endsWith(".css")) { + // Minimize the CSS file + const css = fs.readFileSync(filePath, "utf8"); + const minimizedCss = new CleanCSS().minify(css).styles; + // Serve the minimized CSS file + res.header("Content-Type", "text/css"); + res.send(minimizedCss); + } else { + // Serve the original file + res.sendFile(req.params.id, { root: html_location }); + } +}); + };