From 315e2fe5aa764ba9da424e26d14eb5bb088ddb70 Mon Sep 17 00:00:00 2001 From: Thorn Walli Date: Wed, 13 Sep 2023 13:09:42 +0200 Subject: [PATCH] fix(module): improve critters handling If critters are disabled, the font faces are embedded separately in markup (workaround). If no critters are enabled, CSS files are embedded in markup. --- src/module.mjs | 3 ++- src/utils/preload.mjs | 45 ++++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/module.mjs b/src/module.mjs index 46c063ff4d..2c14d53d7e 100644 --- a/src/module.mjs +++ b/src/module.mjs @@ -111,7 +111,8 @@ async function addBuildTemplates(nuxt, options) { addTemplate({ filename: MODULE_NAME + '/fonts.css', - getContents: () => fontConfig.toCSS(), + getContents: () => + `/*! speedkit-font-faces start */${fontConfig.toCSS()}/*! speedkit-font-faces end */`, write: true }); diff --git a/src/utils/preload.mjs b/src/utils/preload.mjs index 60e00fdbba..4244e5dfc7 100644 --- a/src/utils/preload.mjs +++ b/src/utils/preload.mjs @@ -5,13 +5,16 @@ import { load } from 'cheerio'; import { render } from 'dom-serializer'; import { isViteBuild, logger } from '../utils.mjs'; +// eslint-disable-next-line sonarjs/cognitive-complexity export function optimizePreloads(nuxt) { if (isViteBuild(nuxt)) { nuxt.options.vite.build.manifest = false; - nuxt.options.vite.build.cssCodeSplit = false; + if (nuxt.options.speedkit.disableNuxtCritters) { + nuxt.options.vite.build.cssCodeSplit = false; + } } - nuxt.options.experimental.inlineSSRStyles = false; + nuxt.options.experimental.inlineSSRStyles = true; nuxt.hook('nitro:init', nitro => { nitro.hooks.hook('prerender:generate', async route => { @@ -30,7 +33,7 @@ export function optimizePreloads(nuxt) { // embed css files try { - await Promise.all( + const css = await Promise.all( Array.from($('link[rel="stylesheet"]')) .map(el => $(el)) .map(async $el => { @@ -43,19 +46,35 @@ export function optimizePreloads(nuxt) { nuxt.options.vite.build.assetsDir ); const filepath = join(publicDir, basename($el.attr('href'))); + const fileContent = await fsPromises.readFile(filepath, 'utf-8'); - let css = await fsPromises.readFile(filepath, 'utf-8'); - logger.info( - `Embed CSS File \`${basename($el.attr('href'))}\`; Route: \`${ - route.route - }\`` - ); - - css = css.replace(/url\(.\//g, `url(${dir}/`); - $('head').append(``); - $el.remove(); + if (nuxt.options.speedkit.disableNuxtCritters) { + const css = fileContent.replace(/url\(.\//g, `url(${dir}/`); + $el.remove(); + logger.info( + `Embed CSS File \`${basename($el.attr('href'))}\`; Route: \`${ + route.route + }\`` + ); + return css; + } else { + const matches = fileContent.match( + /\/\*! speedkit-font-faces start \*\/(.*)\/\*! speedkit-font-faces end \*\// + ); + if (matches) { + logger.info( + `Embed Font-Faces CSS \`${basename( + $el.attr('href') + )}\`; Route: \`${route.route}\`` + ); + return matches[1].replace(/url\(.\//g, `url(${dir}/`); + } + } }) ); + if (css.length) { + $('head').append(``); + } } catch (error) { logger.error("can't embed css file.", error); }