ci: docker_tag=ext fix(front): SSR-safe IframeReference (no throw on server, env fallbacks)

This commit is contained in:
Debian Dev4 2025-09-22 10:54:16 +00:00
parent 5284d9be04
commit ed6035ad9c

View File

@ -15,15 +15,27 @@ export default class IframeReference {
new URL(targetOrigin); new URL(targetOrigin);
this.targetOrigin = targetOrigin; this.targetOrigin = targetOrigin;
} catch { } 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 { public static getTargetOrigin(): string {
if (!this.targetOrigin) { if (this.targetOrigin && this.targetOrigin.length > 0) {
throw new Error("targetOrigin is not set"); 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 { public static setIframeUrl(iframeUrl: string): void {
@ -33,24 +45,40 @@ export default class IframeReference {
} }
try { try {
// Validate and normalize URL
const url = new URL(iframeUrl); const url = new URL(iframeUrl);
this.iframeUrl = url.toString(); this.iframeUrl = url.toString();
} catch { } 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 { public static getIframeUrl(): string {
if (!this.iframeUrl) { if (this.iframeUrl && this.iframeUrl.length > 0) {
throw new Error("iframeUrl is not set"); 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 { public static setIframe(iframe: HTMLIFrameElement | null): void {
if (typeof window === 'undefined') {
// SSR: ignorer
return;
}
if (iframe !== null && !(iframe instanceof HTMLIFrameElement)) { 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; this.iframe = iframe;
} }