Repository navigation
fix(sdk): window.op keeps working when compiled to window.op.call #645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Script tag | OpenPanel SDK</title> | ||
| </head> | ||
| <body> | ||
| <script type="module" src="/src/script-tag.ts"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { getInitSnippet } from '@openpanel/web'; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| loadTracker: () => Promise<unknown>; | ||
| } | ||
| } | ||
|
|
||
| // A customer site's two script tags, taken from source: public/op1.js is the | ||
| // last released build, not the code under test. The spec decides when the | ||
| // second one "arrives" so it can call window.op on both sides of it. | ||
| const snippet = document.createElement('script'); | ||
| snippet.textContent = getInitSnippet(); | ||
| document.head.append(snippet); | ||
|
|
||
| window.op('init', { | ||
| clientId: import.meta.env.VITE_OPENPANEL_CLIENT_ID ?? 'testbed-client', | ||
| apiUrl: import.meta.env.VITE_OPENPANEL_API_URL ?? 'http://localhost:3333', | ||
| }); | ||
|
|
||
| window.loadTracker = () => import('@openpanel/web/src/tracker'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; | ||
| import type { TrackHandlerPayload } from '@openpanel/sdk'; | ||
| import { JSDOM } from 'jsdom'; | ||
| import { getInitSnippet } from './init-snippet'; | ||
|
|
||
| const PAGE_URL = 'https://example.com/pricing'; | ||
| const DOM_GLOBALS = [ | ||
| 'window', | ||
| 'document', | ||
| 'history', | ||
| 'sessionStorage', | ||
| 'HTMLElement', | ||
| ] as const; | ||
|
|
||
| let dom: JSDOM; | ||
| let sent: TrackHandlerPayload[]; | ||
| let trackerLoads = 0; | ||
|
|
||
| function installSnippet() { | ||
| dom.window.eval(getInitSnippet()); | ||
| dom.window.op('init', { | ||
| clientId: 'test-client', | ||
| // Captures every payload before it reaches the network. | ||
| filter: (payload: TrackHandlerPayload) => { | ||
| sent.push(payload); | ||
| return false; | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| // tracker.ts runs on import, so each load needs its own module instance. | ||
| async function loadTracker() { | ||
| trackerLoads += 1; | ||
| await import(`./tracker?load=${trackerLoads}`); | ||
| } | ||
|
|
||
| function trackedNames() { | ||
| return sent.flatMap((item) => | ||
| item.type === 'track' ? [item.payload.name] : [] | ||
| ); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| dom = new JSDOM('<!doctype html><html><body></body></html>', { | ||
| url: PAGE_URL, | ||
| runScripts: 'outside-only', | ||
| }); | ||
| for (const name of DOM_GLOBALS) { | ||
| (globalThis as Record<string, unknown>)[name] = dom.window[name]; | ||
| } | ||
| sent = []; | ||
| installSnippet(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| for (const name of DOM_GLOBALS) { | ||
| delete (globalThis as Record<string, unknown>)[name]; | ||
| } | ||
| dom.window.close(); | ||
| }); | ||
|
|
||
| describe('window.op before the SDK loads', () => { | ||
| test('call queues the method it was given', () => { | ||
| const { op } = dom.window; | ||
|
|
||
| op.call(dom.window, 'track', 'user_login', { source: 'nextjs' }); | ||
|
|
||
| expect(op.q?.at(-1)).toEqual(['track', 'user_login', { source: 'nextjs' }]); | ||
| }); | ||
|
|
||
| test('events sent through call, apply and bind survive the SDK loading', async () => { | ||
| const { op } = dom.window; | ||
|
|
||
| op.call(dom.window, 'track', 'through_call'); | ||
| op.apply(dom.window, ['track', 'through_apply']); | ||
| op.bind(dom.window)('track', 'through_bind'); | ||
| op.track('through_method'); | ||
| await loadTracker(); | ||
|
|
||
| expect(trackedNames()).toEqual([ | ||
| 'through_call', | ||
| 'through_apply', | ||
| 'through_bind', | ||
| 'through_method', | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('window.op after the SDK loads', () => { | ||
| beforeEach(loadTracker); | ||
|
|
||
| test('call reaches the SDK method, the way Turbopack compiles window.op?.()', () => { | ||
| dom.window.op.call(dom.window, 'track', 'user_login'); | ||
|
|
||
| expect(trackedNames()).toEqual(['user_login']); | ||
| }); | ||
|
|
||
| test('apply and bind reach the SDK method', () => { | ||
| const { op } = dom.window; | ||
|
|
||
| op.apply(dom.window, ['track', 'through_apply']); | ||
| op.bind(dom.window)('track', 'through_bind'); | ||
|
|
||
| expect(trackedNames()).toEqual(['through_apply', 'through_bind']); | ||
| }); | ||
|
|
||
| test('the callable and method styles keep working', () => { | ||
| const { op } = dom.window; | ||
|
|
||
| op('track', 'callable'); | ||
| op.track('method'); | ||
|
|
||
| expect(trackedNames()).toEqual(['callable', 'method']); | ||
| expect(op.q).toBeUndefined(); | ||
| expect(dom.window.openpanel?.options.clientId).toBe('test-client'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
rg -n 'queue|opCallable|window\.op|\.bind\(' packages/sdks/web/src/tracker.ts packages/sdks/web/src/init-snippet.ts packages/sdks/web/src/tracker.test.tsRepository: Openpanel-dev/openpanel
Length of output: 2326
Script executed:
Repository: Openpanel-dev/openpanel
Length of output: 8193
Forward calls from pre-load stubs after the SDK loads.
A bound reference created before load, such as
window.op.bind(window, 'track'), keeps pushing into the stub queue.tracker.tsdrains that queue once at load, then replaceswindow.op. Calls through the stale reference after load are silently dropped. Forward later queue pushes to the loaded SDK, for example by overridingqueue.pushintracker.tsafter the drain loop, and add a test that calls the bound function after load.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid, but out of scope for this PR. Any reference taken before load already behaves this way:
const track = window.op.trackorconst op = window.opkeeps writing to the drained queue. Compiledwindow.op?.()readswindow.opon every call, so it never holds one, which is the case this PR fixes. Forwarding late pushes would change how the queue works for all of those references, so it should be its own change. Leaving this open.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lindesvard Agreed. Retained pre-load references are a separate queue-lifecycle issue, not the compiled-call case this PR fixes. Forwarding late queue pushes belongs in a separate change. This finding can stay open as a follow-up.
Would you like me to open a GitHub issue for that change?
You are interacting with an AI system.