1- import { EXTERNAL_FORM_API } from "../constants" ;
21import { SurfaceEmbed } from "../embed/embed" ;
32import { openTriggerOverlay } from "./open-trigger-overlay" ;
43import { OpenTriggerEntry , OpenTriggersMap , pickOpenTrigger } from "./resolve" ;
4+ import {
5+ getSurfaceRuntimeConfig ,
6+ type SurfaceRuntimeConfig ,
7+ } from "../runtime-config" ;
58
69const SESSION_PREFIX = "surface_open_triggers:" ;
710// Self-healing cache: re-fetch the map after this long so a slug retargeted/disabled
@@ -30,12 +33,15 @@ interface OverridableWindow {
3033 * present as `?<slug>=true`. Opens a form even if it isn't already embedded on the
3134 * page. No params → zero network. Always fails safe (never breaks the host page).
3235 */
33- export async function resolveOpenTriggersOnLoad ( environmentId : string | null ) : Promise < void > {
36+ export async function resolveOpenTriggersOnLoad (
37+ environmentId : string | null ,
38+ config : SurfaceRuntimeConfig = getSurfaceRuntimeConfig ( )
39+ ) : Promise < void > {
3440 try {
3541 if ( ! environmentId ) return ;
3642 if ( ! window . location . search ) return ;
3743
38- const map = await fetchOpenTriggersMap ( environmentId ) ;
44+ const map = await fetchOpenTriggersMap ( environmentId , config ) ;
3945 const entry = pickOpenTrigger ( window . location . search , map ) ;
4046 if ( ! entry ) return ;
4147
@@ -45,13 +51,16 @@ export async function resolveOpenTriggersOnLoad(environmentId: string | null): P
4551 }
4652}
4753
48- async function fetchOpenTriggersMap ( environmentId : string ) : Promise < OpenTriggersMap | null > {
54+ async function fetchOpenTriggersMap (
55+ environmentId : string ,
56+ config : SurfaceRuntimeConfig
57+ ) : Promise < OpenTriggersMap | null > {
4958 const w = window as unknown as OverridableWindow ;
5059
5160 // Test/escape hatch: a directly-injected map bypasses the network entirely.
5261 if ( w . __SURFACE_OPEN_TRIGGERS_MAP ) return w . __SURFACE_OPEN_TRIGGERS_MAP ;
5362
54- const sessionKey = SESSION_PREFIX + environmentId ;
63+ const sessionKey = ` ${ SESSION_PREFIX } ${ config . apiBaseUrl } : ${ environmentId } ` ;
5564 try {
5665 const cached = sessionStorage . getItem ( sessionKey ) ;
5766 if ( cached ) {
@@ -64,7 +73,7 @@ async function fetchOpenTriggersMap(environmentId: string): Promise<OpenTriggers
6473 // sessionStorage unavailable / malformed (e.g. privacy mode) — fall through to a live fetch.
6574 }
6675
67- const base = w . __SURFACE_OPEN_TRIGGERS_BASE || EXTERNAL_FORM_API ;
76+ const base = w . __SURFACE_OPEN_TRIGGERS_BASE || config . apiBaseUrl ;
6877 const response = await fetch ( `${ base } /environments/${ encodeURIComponent ( environmentId ) } /open-triggers` ) ;
6978 if ( ! response . ok ) return null ;
7079
0 commit comments