diff --git a/docusaurus.config.ts b/docusaurus.config.ts index 6b6185213..295267dca 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -151,6 +151,7 @@ const config: Config = { // buttonPosition: "center-right", // }, // ], + "./src/plugins/preload-css", async function tailwindcss(context, options) { return { name: "docusaurus-tailwindcss", diff --git a/src/plugins/preload-css.js b/src/plugins/preload-css.js new file mode 100644 index 000000000..40cada0f7 --- /dev/null +++ b/src/plugins/preload-css.js @@ -0,0 +1,44 @@ +// Copyright © 2026 Ory Corp +// SPDX-License-Identifier: Apache-2.0 + +const fs = require("fs") +const path = require("path") + +module.exports = function preloadCssPlugin() { + return { + name: "preload-css", + async postBuild({ outDir, siteConfig }) { + const cssDir = path.join(outDir, "assets", "css") + if (!fs.existsSync(cssDir)) return + + const cssFile = fs.readdirSync(cssDir).find( + (f) => f.startsWith("styles.") && f.endsWith(".css"), + ) + if (!cssFile) return + + const baseUrl = siteConfig.baseUrl.replace(/\/$/, "") + const href = `${baseUrl}/assets/css/${cssFile}` + const preloadTag = `` + + function walk(dir) { + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { + const full = path.join(dir, entry.name) + if (entry.isDirectory()) { + walk(full) + } else if (entry.name.endsWith(".html")) { + inject(full) + } + } + } + + function inject(filePath) { + let html = fs.readFileSync(filePath, "utf8") + if (html.includes(preloadTag)) return + html = html.replace("", `${preloadTag}`) + fs.writeFileSync(filePath, html) + } + + walk(outDir) + }, + } +}