Skip to content

Commit 0eafea4

Browse files
committed
wip
1 parent 4612521 commit 0eafea4

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

packages/signals/signals-integration-tests/src/tests/signals-vanilla/all-segment-events.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ test('Segment events', async ({ page }) => {
6161
expect(trackingApiReqs).toEqual(snapshot)
6262
})
6363

64-
test.only('Should dispatch events from signals that occurred before analytics was instantiated', async ({
64+
test('Should dispatch events from signals that occurred before analytics was instantiated', async ({
6565
page,
6666
}) => {
6767
const indexPage = new IndexPage()

packages/signals/signals/src/core/processor/sandbox.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -257,51 +257,52 @@ export class IframeSandbox implements SignalSandbox {
257257
constructor(edgeFnUrl: string, processSignalFn?: string) {
258258
logger.debug('Initializing iframe sandbox')
259259
this.edgeFnUrl = edgeFnUrl
260-
this.iframe = document.createElement('iframe')
261-
this.iframe.id = 'segment-signals-sandbox'
262-
this.iframe.style.display = 'none'
263-
this.iframe.src = 'about:blank'
264-
document.body.appendChild(this.iframe)
265-
266-
const doc = this.iframe.contentDocument!
260+
const innerHTML = this.buildIframeInnerHTMLWithScriptTag(
261+
Boolean(processSignalFn)
262+
)
263+
const jsScriptContent = this.getIframeScriptJSScriptContent(processSignalFn)
264+
const iframe = document.createElement('iframe')
265+
iframe.id = 'segment-signals-sandbox'
266+
iframe.style.display = 'none'
267+
iframe.src = 'about:blank'
268+
document.body.appendChild(iframe)
269+
const doc = iframe.contentDocument!
267270
doc.open()
268-
doc.write(this.getIframeHTML(processSignalFn))
271+
doc.write(innerHTML)
269272
doc.close()
270-
this.iframeReady = new Promise((resolve) => {
273+
const ready = new Promise<undefined>((resolve) => {
271274
window.addEventListener('message', (e) => {
272-
if (
273-
e.source === this.iframe.contentWindow &&
274-
e.data === 'iframe_ready'
275-
) {
276-
this.iframe.contentWindow!.postMessage({
275+
if (e.source === iframe.contentWindow && e.data === 'iframe_ready') {
276+
iframe.contentWindow!.postMessage({
277277
type: 'init',
278278
})
279279
resolve(undefined)
280280
}
281281
})
282282
})
283-
const runtimeJs = this.getIframeJS(processSignalFn)
284-
const blob = new Blob([runtimeJs], { type: 'application/javascript' })
283+
const blob = new Blob([jsScriptContent], { type: 'application/javascript' })
285284
const runtimeScript = doc.createElement('script')
286285
runtimeScript.src = URL.createObjectURL(blob)
287286
doc.head.appendChild(runtimeScript)
287+
this.iframeReady = ready
288+
this.iframe = iframe
288289
}
289290

290-
private getIframeHTML(processSignalFn?: string): string {
291+
private buildIframeInnerHTMLWithScriptTag(includedEdgeFn = true): string {
291292
return [
292293
`<!DOCTYPE html>`,
293294
`<html>`,
294295
`<head>`,
295-
processSignalFn
296-
? ''
297-
: `<script id="edge-fn" src=${this.edgeFnUrl}></script>`,
296+
includedEdgeFn
297+
? `<script id="edge-fn" src=${this.edgeFnUrl}></script>`
298+
: '',
298299
`</head>`,
299300
`<body></body>
300301
</html>`,
301302
].join(',')
302303
}
303304

304-
private getIframeJS(processSignalFn?: string): string {
305+
private getIframeScriptJSScriptContent(processSignalFn?: string): string {
305306
// External signal processor script
306307
// Inject runtime via Blob (CSP-safe)
307308
return `

0 commit comments

Comments
 (0)