From 5272bbc023cdeebcd35c3f2b74bd318a99220826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matilda=20Landstr=C3=B6m?= Date: Wed, 17 Dec 2025 13:35:02 +0000 Subject: [PATCH] Merged in fix/Sj-widget-error (pull request #3332) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor loading of SJ widget to avoid undefined window * refactor loading of widget to avoid undefined window Approved-by: Bianca Widstam Approved-by: Erik Tiekstra Approved-by: Joakim Jäderberg Approved-by: Anton Gunnarsson --- .../scandic-web/components/SJWidget/index.tsx | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/apps/scandic-web/components/SJWidget/index.tsx b/apps/scandic-web/components/SJWidget/index.tsx index da0806ce9..322497c55 100644 --- a/apps/scandic-web/components/SJWidget/index.tsx +++ b/apps/scandic-web/components/SJWidget/index.tsx @@ -10,24 +10,22 @@ const SJSupportedLangs = ["en", "sv"] as const export function SJWidget() { const lang = useLang() const [scriptInitialized, setScriptInitialized] = useState( - !!window.SJ?.widget?.init + () => typeof window !== "undefined" && !!window.SJ?.widget?.init ) const componentInitialized = useRef(false) useEffect(() => { - const initWidget = () => { - if (componentInitialized.current === true) return - if (!window.SJ?.widget?.init) return + if (!scriptInitialized) return + if (componentInitialized.current) return - window.SJ.widget.init({ - micrositeId: "12952d0f-c70f-452c-9598-6586a64c7b60", - language: isSJSupportedLang(lang) ? lang : "en", - }) + if (!window.SJ?.widget?.init) return - componentInitialized.current = true - } + window.SJ.widget.init({ + micrositeId: "12952d0f-c70f-452c-9598-6586a64c7b60", + language: isSJSupportedLang(lang) ? lang : "en", + }) - initWidget() + componentInitialized.current = true }, [lang, scriptInitialized]) return (