From 61191f7403f2c9726abe37ded79985f8a257014e Mon Sep 17 00:00:00 2001
From: "github-actions[bot]"
<41898282+github-actions[bot]@users.noreply.github.com>
Date: Fri, 2 Oct 2026 09:27:59 +0000
Subject: [PATCH] chore: sync storefront from monorepo
Source: coded-letter/coded-letter-monorepo@4224efddad9a73afb0b87455199868b333f3a297
---
.../scripts/bootstrap-performance.test.mjs | 5 +-
apps/storefront/scripts/prerender.mjs | 37 ++++-------
.../storefront/scripts/static-font-assets.mjs | 8 ++-
.../scripts/static-font-assets.test.mjs | 31 ++++++++++
.../scripts/static-wordpress-styles.mjs | 62 +++++++++++++++++++
.../scripts/static-wordpress-styles.test.mjs | 51 +++++++++++++++
6 files changed, 167 insertions(+), 27 deletions(-)
create mode 100644 apps/storefront/scripts/static-wordpress-styles.mjs
create mode 100644 apps/storefront/scripts/static-wordpress-styles.test.mjs
diff --git a/apps/storefront/scripts/bootstrap-performance.test.mjs b/apps/storefront/scripts/bootstrap-performance.test.mjs
index a229866..a44b9fb 100644
--- a/apps/storefront/scripts/bootstrap-performance.test.mjs
+++ b/apps/storefront/scripts/bootstrap-performance.test.mjs
@@ -329,7 +329,7 @@ test("intent prefetch warms documents while navigations prefer current deploys",
assert.match(prerenderSource, /stampServiceWorkerVersion\(generatedAt\)/);
});
-test("generated static HTML omits the loader and keeps styles render-blocking", () => {
+test("generated static HTML keeps critical styles blocking and defers below-the-fold styles", () => {
const html = '
Loading
static
';
const stripped = stripBootstrapOverlay(html);
assert.doesNotMatch(stripped, /storefront-bootstrap/);
@@ -339,6 +339,9 @@ test("generated static HTML omits the loader and keeps styles render-blocking",
assert.match(prerenderSource, /` : "",
``,
routeChromeConfig.paymentGatewayCache.gateways.length
@@ -2862,37 +2865,23 @@ async function buildStaticStyleAsset(styles) {
stylesheets,
customCss: styles.customCss,
});
- const sections = [
+ const prefixSections = [
sanitizeWordPressFontFaces(styles.fontFaceStyles),
sanitizeWordPressGlobalStyles(styles.globalStyles),
];
for (const stylesheetUrl of stylesheets) {
- sections.push(await fetchStaticStylesheet(stylesheetUrl));
+ prefixSections.push(await fetchStaticStylesheet(stylesheetUrl));
}
- sections.push(
- styles.customCss,
+ return writeStaticWordPressStyleAssets({
+ prefixSections,
+ suffixSections: [
WORDPRESS_BLOCK_COMPATIBILITY_CSS,
createWordPressElementTypographyCss(styles.globalStyles),
- );
- const sourceCss = sections.filter((section) => section.trim()).join("\n");
- if (!sourceCss) return null;
- if (Buffer.byteLength(sourceCss, "utf8") > 1_500_000) {
- throw new Error("Combined WordPress static CSS exceeds the 1.5 MB build limit");
- }
- const localized = await localizeStaticFontAssets(sourceCss, { outputDirectory });
- const css = localized.css;
-
- const contentHash = createHash("sha256").update(css).digest("hex").slice(0, 16);
- const filename = `wordpress-static-${contentHash}.css`;
- await mkdir(resolve(outputDirectory, "assets"), { recursive: true });
- await writeFile(resolve(outputDirectory, "assets", filename), `${css}\n`);
- return {
- href: `/assets/${filename}`,
+ ],
+ customCss: styles.customCss,
+ outputDirectory,
sourceHash,
- criticalCss: splitCustomCss(styles.customCss).critical,
- fontAssets: localized.fontAssets,
- preloadAssets: localized.preloadAssets,
- };
+ });
}
async function writeStaticHydrationAsset(label, entries, generatedAt) {
diff --git a/apps/storefront/scripts/static-font-assets.mjs b/apps/storefront/scripts/static-font-assets.mjs
index a22a09f..a084e72 100644
--- a/apps/storefront/scripts/static-font-assets.mjs
+++ b/apps/storefront/scripts/static-font-assets.mjs
@@ -49,7 +49,7 @@ function defaultFacePreloads(css, fontAssets) {
export async function localizeStaticFontAssets(
css,
- { outputDirectory, fetchImpl = fetch } = {},
+ { outputDirectory, fetchImpl = fetch, preloadCss = css } = {},
) {
if (!outputDirectory) throw new Error("A static font output directory is required");
const fontFaceBlocks = [...css.matchAll(FONT_FACE_PATTERN)].map((match) => match[1]);
@@ -96,9 +96,13 @@ export async function localizeStaticFontAssets(
const href = replacements.get(sourceUrl);
return href ? `url(${quote}${href}${quote})` : match;
});
+ const localizedPreloadCss = preloadCss.replace(FONT_URL_PATTERN, (match, quote, sourceUrl) => {
+ const href = replacements.get(sourceUrl);
+ return href ? `url(${quote}${href}${quote})` : match;
+ });
return {
css: localizedCss,
fontAssets,
- preloadAssets: defaultFacePreloads(localizedCss, fontAssets),
+ preloadAssets: defaultFacePreloads(localizedPreloadCss, fontAssets),
};
}
diff --git a/apps/storefront/scripts/static-font-assets.test.mjs b/apps/storefront/scripts/static-font-assets.test.mjs
index fccea84..c8ed79f 100644
--- a/apps/storefront/scripts/static-font-assets.test.mjs
+++ b/apps/storefront/scripts/static-font-assets.test.mjs
@@ -61,6 +61,37 @@ test("deduplicates preloads when different font faces share one localized binary
assert.equal(result.preloadAssets.length, 1);
});
+test("preloads critical fonts without pulling deferred-only fonts into the initial page", async () => {
+ const outputDirectory = await mkdtemp(join(tmpdir(), "storefront-fonts-"));
+ temporaryDirectories.push(outputDirectory);
+ const css = `
+ @font-face{font-family:"Critical";font-style:normal;font-weight:400;src:url("https://cms.test/critical.woff2") format("woff2")}
+ @font-face{font-family:"Deferred";font-style:normal;font-weight:400;src:url("https://cms.test/deferred.woff2") format("woff2")}
+ `;
+ const criticalCss = css.split('@font-face{font-family:"Deferred"')[0];
+ const result = await localizeStaticFontAssets(css, {
+ outputDirectory,
+ preloadCss: criticalCss,
+ fetchImpl: async (url) => new Response(
+ Buffer.concat([Buffer.from("wOF2"), Buffer.alloc(28, url.endsWith("deferred.woff2") ? 8 : 7)]),
+ { headers: { "content-type": "font/woff2" } },
+ ),
+ });
+
+ assert.equal(result.fontAssets.length, 2);
+ assert.equal(result.preloadAssets.length, 1);
+ const [localizedCriticalHref] = result.css
+ .split('@font-face{font-family:"Deferred"')[0]
+ .match(/url\("([^"]+)"/)
+ .slice(1);
+ const [localizedDeferredHref] = result.css
+ .split('@font-face{font-family:"Deferred"')[1]
+ .match(/url\("([^"]+)"/)
+ .slice(1);
+ assert.equal(result.preloadAssets[0].href, localizedCriticalHref);
+ assert.notEqual(result.preloadAssets[0].href, localizedDeferredHref);
+});
+
test("rejects non-font responses instead of publishing untrusted bytes", async () => {
const outputDirectory = await mkdtemp(join(tmpdir(), "storefront-fonts-"));
temporaryDirectories.push(outputDirectory);
diff --git a/apps/storefront/scripts/static-wordpress-styles.mjs b/apps/storefront/scripts/static-wordpress-styles.mjs
new file mode 100644
index 0000000..8d371b8
--- /dev/null
+++ b/apps/storefront/scripts/static-wordpress-styles.mjs
@@ -0,0 +1,62 @@
+import { createHash } from "node:crypto";
+import { mkdir, writeFile } from "node:fs/promises";
+import { resolve } from "node:path";
+import { splitCustomCss } from "../src/lib/customCssLayers.mjs";
+import { localizeStaticFontAssets } from "./static-font-assets.mjs";
+
+export async function writeStaticWordPressStyleAssets({
+ prefixSections,
+ suffixSections,
+ customCss,
+ outputDirectory,
+ sourceHash,
+}) {
+ const layers = splitCustomCss(customCss);
+ const criticalSections = [
+ ...prefixSections,
+ layers.critical,
+ ...suffixSections,
+ ].filter((section) => section.trim());
+ const criticalSourceCss = criticalSections.join("\n");
+ const deferredSourceCss = layers.deferred.trim()
+ ? `/* storefront:deferred */\n${layers.deferred}`
+ : "";
+ const sourceCss = [criticalSourceCss, deferredSourceCss].filter(Boolean).join("\n");
+ if (!sourceCss) return null;
+ if (Buffer.byteLength(sourceCss, "utf8") > 1_500_000) {
+ throw new Error("Combined WordPress static CSS exceeds the 1.5 MB build limit");
+ }
+
+ const localized = await localizeStaticFontAssets(sourceCss, {
+ outputDirectory,
+ preloadCss: criticalSourceCss,
+ });
+ const localizedLayers = splitCustomCss(localized.css);
+ const criticalHash = createHash("sha256").update(localizedLayers.critical).digest("hex").slice(0, 16);
+ const criticalFilename = `wordpress-static-${criticalHash}.css`;
+ await mkdir(resolve(outputDirectory, "assets"), { recursive: true });
+ await writeFile(
+ resolve(outputDirectory, "assets", criticalFilename),
+ `${localizedLayers.critical}\n`,
+ );
+
+ let deferredHref = "";
+ if (localizedLayers.deferred.trim()) {
+ const deferredHash = createHash("sha256").update(localizedLayers.deferred).digest("hex").slice(0, 16);
+ const deferredFilename = `wordpress-deferred-${deferredHash}.css`;
+ await writeFile(
+ resolve(outputDirectory, "assets", deferredFilename),
+ `${localizedLayers.deferred}\n`,
+ );
+ deferredHref = `/assets/${deferredFilename}`;
+ }
+
+ return {
+ href: `/assets/${criticalFilename}`,
+ deferredHref,
+ sourceHash,
+ criticalCss: layers.critical,
+ fontAssets: localized.fontAssets,
+ preloadAssets: localized.preloadAssets,
+ };
+}
diff --git a/apps/storefront/scripts/static-wordpress-styles.test.mjs b/apps/storefront/scripts/static-wordpress-styles.test.mjs
new file mode 100644
index 0000000..9d83aa9
--- /dev/null
+++ b/apps/storefront/scripts/static-wordpress-styles.test.mjs
@@ -0,0 +1,51 @@
+import assert from "node:assert/strict";
+import { mkdtemp, readFile, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, test } from "node:test";
+import { writeStaticWordPressStyleAssets } from "./static-wordpress-styles.mjs";
+
+const temporaryDirectories = [];
+
+afterEach(async () => {
+ await Promise.all(temporaryDirectories.splice(0).map((directory) => rm(directory, { recursive: true })));
+});
+
+test("writes deferred custom CSS separately from the critical static stylesheet", async () => {
+ const outputDirectory = await mkdtemp(join(tmpdir(), "storefront-static-css-"));
+ temporaryDirectories.push(outputDirectory);
+ const result = await writeStaticWordPressStyleAssets({
+ prefixSections: [".global { color: black; }"],
+ suffixSections: [".wp-block { display: block; }"],
+ customCss: ".hero { color: red; }\n/* storefront:deferred */\n.below-fold { color: blue; }",
+ outputDirectory,
+ sourceHash: "source-hash",
+ });
+
+ assert.ok(result);
+ assert.equal(result.sourceHash, "source-hash");
+ assert.match(result.deferredHref, /^\/assets\/wordpress-deferred-[a-f0-9]{16}\.css$/);
+ const criticalCss = await readFile(join(outputDirectory, result.href), "utf8");
+ const deferredCss = await readFile(join(outputDirectory, result.deferredHref), "utf8");
+ assert.match(criticalCss, /\.hero/);
+ assert.match(criticalCss, /\.wp-block/);
+ assert.doesNotMatch(criticalCss, /\.below-fold/);
+ assert.match(deferredCss, /\.below-fold/);
+ assert.doesNotMatch(deferredCss, /\.hero/);
+});
+
+test("keeps all custom CSS critical when no deferred marker is present", async () => {
+ const outputDirectory = await mkdtemp(join(tmpdir(), "storefront-static-css-"));
+ temporaryDirectories.push(outputDirectory);
+ const result = await writeStaticWordPressStyleAssets({
+ prefixSections: [],
+ suffixSections: [],
+ customCss: ".page { color: red; }",
+ outputDirectory,
+ sourceHash: "source-hash",
+ });
+
+ assert.ok(result);
+ assert.equal(result.deferredHref, "");
+ assert.match(await readFile(join(outputDirectory, result.href), "utf8"), /\.page/);
+});