From 73df88c90dee72e50228f62a5f0284eec9b922e6 Mon Sep 17 00:00:00 2001 From: Hash6232 Date: Mon, 22 Dec 2025 12:15:57 +0100 Subject: [PATCH] fix(runtime): add fallback for missing head element --- runtime/inject-css.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/runtime/inject-css.js b/runtime/inject-css.js index 2cad19f9..e397bda4 100644 --- a/runtime/inject-css.js +++ b/runtime/inject-css.js @@ -12,7 +12,7 @@ var styleTags = []; * @param {Record} [options.attributes] * @returns {void} */ -export default function (css, options) { +var styleInjector = function (css, options) { if (!css || typeof document === "undefined") return; var position = options.prepend === true ? "prepend" : "append"; @@ -23,6 +23,14 @@ export default function (css, options) { ? document.querySelector(options.container) : document.getElementsByTagName("head")[0]; + // Fallback if container is not available yet + if (!container && document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", function () { + styleInjector(css, options); + }); + return; + } + function createStyleTag() { var styleTag = document.createElement("style"); styleTag.setAttribute("type", "text/css"); @@ -65,4 +73,6 @@ export default function (css, options) { } else { styleTag.appendChild(document.createTextNode(css)); } -} +}; + +export default styleInjector;