From ed6035ad9c6c3d6856a892cab24b7412a2e98762 Mon Sep 17 00:00:00 2001 From: Debian Dev4 Date: Mon, 22 Sep 2025 10:54:16 +0000 Subject: [PATCH] ci: docker_tag=ext fix(front): SSR-safe IframeReference (no throw on server, env fallbacks) --- src/sdk/IframeReference.ts | 48 ++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/sdk/IframeReference.ts b/src/sdk/IframeReference.ts index db9fbe2d..d69a3d1e 100644 --- a/src/sdk/IframeReference.ts +++ b/src/sdk/IframeReference.ts @@ -15,15 +15,27 @@ export default class IframeReference { new URL(targetOrigin); this.targetOrigin = targetOrigin; } catch { - throw new Error(`Invalid targetOrigin: ${targetOrigin}`); + // Ne pas throw côté SSR; enregistrer une valeur sûre (vide) + this.targetOrigin = ''; + console.warn(`Invalid targetOrigin provided, using empty string`); } } public static getTargetOrigin(): string { - if (!this.targetOrigin) { - throw new Error("targetOrigin is not set"); + if (this.targetOrigin && this.targetOrigin.length > 0) { + return this.targetOrigin; } - return this.targetOrigin; + // Fallback sur env côté Next.js si disponible + const fromEnv = (process as any)?.env?.NEXT_PUBLIC_TARGET_ORIGIN || (process as any)?.env?.NEXT_PUBLIC_4NK_URL || ''; + try { + if (fromEnv) { + const origin = new URL(fromEnv).origin; + this.targetOrigin = origin; + return origin; + } + } catch { /* ignore */ } + // SSR-safe: retourner chaîne vide plutôt que throw + return ''; } public static setIframeUrl(iframeUrl: string): void { @@ -33,24 +45,40 @@ export default class IframeReference { } try { - // Validate and normalize URL const url = new URL(iframeUrl); this.iframeUrl = url.toString(); } catch { - throw new Error(`Invalid iframeUrl: ${iframeUrl}`); + // Ne pas throw côté SSR; on dérive depuis targetOrigin si possible + const origin = this.getTargetOrigin(); + this.iframeUrl = origin || ''; + console.warn(`Invalid iframeUrl provided, using '${this.iframeUrl}'`); } } public static getIframeUrl(): string { - if (!this.iframeUrl) { - throw new Error("iframeUrl is not set"); + if (this.iframeUrl && this.iframeUrl.length > 0) { + return this.iframeUrl; } - return this.iframeUrl; + const fromEnv = (process as any)?.env?.NEXT_PUBLIC_4NK_IFRAME_URL || (process as any)?.env?.NEXT_PUBLIC_4NK_URL || ''; + try { + if (fromEnv) { + const url = new URL(fromEnv).toString(); + this.iframeUrl = url; + return url; + } + } catch { /* ignore */ } + // SSR-safe: retourner chaîne vide plutôt que throw + return ''; } public static setIframe(iframe: HTMLIFrameElement | null): void { + if (typeof window === 'undefined') { + // SSR: ignorer + return; + } if (iframe !== null && !(iframe instanceof HTMLIFrameElement)) { - throw new Error("setIframe expects an HTMLIFrameElement or null"); + console.warn("setIframe expects an HTMLIFrameElement or null"); + return; } this.iframe = iframe; }