MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / loadReplayModule

Method loadReplayModule

packages/sdks/web/src/index.ts:151–189  ·  view source on GitHub ↗

* Load the replay recorder module. * * - **IIFE build (op1.js)**: `__OPENPANEL_REPLAY_URL__` is replaced at * build time with a CDN URL (e.g. `https://openpanel.dev/op1-replay.js`). * The user can also override it via `sessionReplay.scriptUrl`. * We load the IIFE replay script v

()

Source from the content-addressed store, hash-verified

149 * bundler resolves and code-splits from the package source.
150 */
151 private async loadReplayModule(): Promise<typeof import('./replay') | null> {
152 try {
153 // typeof check avoids a ReferenceError when the constant is not
154 // defined (library build). tsup replaces the constant with a
155 // string literal only in the IIFE build, so this branch is
156 // dead-code-eliminated in the library build.
157 if (typeof __OPENPANEL_REPLAY_URL__ !== 'undefined') {
158 const scriptEl = _replayScriptRef;
159 const url =
160 this.options.sessionReplay?.scriptUrl ||
161 scriptEl?.src?.replace('.js', '-replay.js') ||
162 'https://openpanel.dev/op1-replay.js';
163
164 // Already loaded (e.g. user included the script manually)
165 if ((window as any).__openpanel_replay) {
166 return (window as any).__openpanel_replay;
167 }
168
169 // Load via classic <script> tag — no CORS restrictions
170 return new Promise((resolve) => {
171 const script = document.createElement('script');
172 script.src = url;
173 script.onload = () => {
174 resolve((window as any).__openpanel_replay ?? null);
175 };
176 script.onerror = () => {
177 console.warn('[OpenPanel] Failed to load replay script from', url);
178 resolve(null);
179 };
180 document.head.appendChild(script);
181 });
182 }
183 // Library / bundler context — resolved by the bundler
184 return await import('./replay');
185 } catch (e) {
186 console.warn('[OpenPanel] Failed to load replay module', e);
187 return null;
188 }
189 }
190
191 private debounce(func: () => void, delay: number) {
192 clearTimeout(this.debounceTimer);

Callers 1

constructorMethod · 0.95

Calls 3

replaceMethod · 0.80
appendChildMethod · 0.80
warnMethod · 0.45

Tested by

no test coverage detected