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);
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;
}