From 5fbf4b5212d5a26fd47b5ef9b88bd1b83e486daa Mon Sep 17 00:00:00 2001 From: zeim839 Date: Sat, 10 Feb 2024 13:15:42 -0500 Subject: [PATCH] feat: render critical styles inline --- utils/callbacks.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/utils/callbacks.js b/utils/callbacks.js index 9bb482f..ca86269 100644 --- a/utils/callbacks.js +++ b/utils/callbacks.js @@ -1,10 +1,16 @@ const ejs = require('ejs') const config = require('./config') +const fs = require('node:fs') const { BlogModel } = require('../model/blog') const cacheIndexPage = async () => { const blog = await BlogModel.find({ status: 'published', id: -1 }, null, { limit: 3 }).sort({ date: -1 }) - const data = { version: config.VERSION, blog } + + // Load critical styles. + let styles = fs.readFileSync('./public/css/common.css', 'utf8') + styles += fs.readFileSync('./public/css/index.css', 'utf8') + + const data = { version: config.VERSION, blog, styles } const indexPageData = { indexPage: '' } ejs.renderFile('./views/index.ejs', data, { async: false }, (err, str) => { if (err) {