-
Notifications
You must be signed in to change notification settings - Fork 192
🐛 [Shopify] Defer plugin init until a checkout page view, fixing double session IDs #4981
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
lierniel
merged 13 commits into
main
from
stephan.koshcheev/RUM-18173/shopify-fix-double-session-ids
Sep 2, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f4b3817
♻️ Replace Shopify Custom Pixel init-patching with a RumPlugin-based …
lierniel 3b31f07
✨ Support async and abortable onInit in RUM plugins
lierniel 4d662d6
✨ Defer Shopify plugin init until the first checkout page view
lierniel 15aec3b
🚨 Silence floating-promise lint errors from the async onInit signature
lierniel f58b3de
♻️ Replace callPluginsMethod with runOnInitPlugins and callPluginsOnR…
lierniel d6fd85d
📝 Explain the isBindingsInstalled guard in shopifyPlugin
lierniel 6685c88
✨ Add embeddedPlugins option to expose plugin factories on the public…
lierniel 7b60dac
♻️ Address review feedback on plugin init lifecycle
lierniel bf1c424
♻️ Remove the generic onInit timeout from callPluginsOnInit
lierniel 5402c17
♻️ Move the onInit timeout from the generic mechanism to shopifyPlugin
lierniel 2a7501f
🐛 Fix unhandled timeout rejection leaking from shopifyPlugin spec
lierniel 856a44a
♻️ Address review feedback on plugin init and Shopify plugin tests
lierniel 9cfaccb
🐛 Move mockShopifyAnalytics test helper out of src to fix package build
lierniel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { isThenable, waitForThenable, TIMEOUT_ERROR_MESSAGE } from './thenable' | ||
| import { noop } from './utils/functionUtils' | ||
|
|
||
| describe('isThenable', () => { | ||
| it('returns true for a Promise', () => { | ||
| expect(isThenable(Promise.resolve())).toBe(true) | ||
| }) | ||
|
|
||
| it('returns true for a plain object with a then function', () => { | ||
| expect(isThenable({ then: noop })).toBe(true) | ||
| }) | ||
|
|
||
| it('returns false for a plain object without a then function', () => { | ||
| expect(isThenable({})).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false for null and undefined', () => { | ||
| expect(isThenable(null)).toBe(false) | ||
| expect(isThenable(undefined)).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false for primitives', () => { | ||
| expect(isThenable(42)).toBe(false) | ||
| expect(isThenable('foo')).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('waitForThenable', () => { | ||
| it('resolves with the thenable value when it settles before the timeout', async () => { | ||
| const result = await waitForThenable(Promise.resolve('value'), 1000) | ||
| expect(result).toBe('value') | ||
| }) | ||
|
|
||
| it('rejects with the thenable rejection reason when it settles before the timeout', async () => { | ||
| await expectAsync(waitForThenable(Promise.reject(new Error('boom')), 1000)).toBeRejectedWithError('boom') | ||
| }) | ||
|
|
||
| it('rejects with a timeout error when the thenable does not settle in time', async () => { | ||
| const neverSettles = new Promise(noop) | ||
| await expectAsync(waitForThenable(neverSettles, 0)).toBeRejectedWithError(TIMEOUT_ERROR_MESSAGE) | ||
| }) | ||
| }) |
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,23 @@ | ||
| import type { TimeoutId } from './timer' | ||
| import { setTimeout, clearTimeout } from './timer' | ||
|
|
||
| export function isThenable<T>(value: unknown): value is PromiseLike<T> { | ||
| return !!value && typeof (value as { then?: unknown }).then === 'function' | ||
| } | ||
|
|
||
| export const TIMEOUT_ERROR_MESSAGE = 'Timeout' | ||
| export function isTimeoutError(error: unknown): error is Error { | ||
| return error instanceof Error && error.message === TIMEOUT_ERROR_MESSAGE | ||
| } | ||
|
|
||
| /** | ||
| * Resolves or rejects with `thenable`, or rejects with a `TIMEOUT_ERROR_MESSAGE` error if it | ||
| * doesn't settle within `timeout` ms. | ||
| */ | ||
| export function waitForThenable<T>(thenable: PromiseLike<T>, timeout = 3000): Promise<T> { | ||
| let timeoutId: TimeoutId | ||
| const timeoutPromise = new Promise<never>((_, reject) => { | ||
| timeoutId = setTimeout(() => reject(new Error(TIMEOUT_ERROR_MESSAGE)), timeout) | ||
| }) | ||
| return Promise.race([thenable, timeoutPromise]).finally(() => clearTimeout(timeoutId)) | ||
| } |
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
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 |
|---|---|---|
| @@ -1,23 +1,122 @@ | ||
| import type { RumPublicApi } from '../boot/rumPublicApi' | ||
| import type { RumInitConfiguration } from './configuration' | ||
| import type { RumPlugin } from './plugins' | ||
| import { callPluginsMethod } from './plugins' | ||
| import { callPluginsOnInit, callPluginsOnRumStart } from './plugins' | ||
|
|
||
| describe('callPluginsMethod', () => { | ||
| it('calls the method on each plugin', () => { | ||
| describe('callPluginsOnInit', () => { | ||
| const PARAMETER = { initConfiguration: {} as RumInitConfiguration, publicApi: {} as RumPublicApi } | ||
|
|
||
| it('calls onInit on each plugin', () => { | ||
| const plugin1 = { name: 'a', onInit: jasmine.createSpy() } satisfies RumPlugin | ||
| const plugin2 = { name: 'b', onInit: jasmine.createSpy() } satisfies RumPlugin | ||
| const parameter = { initConfiguration: {} as RumInitConfiguration, publicApi: {} as RumPublicApi } | ||
| callPluginsMethod([plugin1, plugin2], 'onInit', parameter) | ||
| expect(plugin1.onInit).toHaveBeenCalledWith(parameter) | ||
| expect(plugin2.onInit).toHaveBeenCalledWith(parameter) | ||
|
|
||
| void callPluginsOnInit([plugin1, plugin2], PARAMETER) | ||
|
|
||
| expect(plugin1.onInit).toHaveBeenCalledWith(PARAMETER) | ||
| expect(plugin2.onInit).toHaveBeenCalledWith(PARAMETER) | ||
| }) | ||
|
|
||
| it('does not call the method if the plugin does not have it', () => { | ||
| it('does not call onInit if the plugin does not have it', () => { | ||
| const plugin1 = { name: 'a', onInit: jasmine.createSpy() } satisfies RumPlugin | ||
| const plugin2 = { name: 'b' } satisfies RumPlugin | ||
| const parameter = { initConfiguration: {} as RumInitConfiguration, publicApi: {} as RumPublicApi } | ||
| callPluginsMethod([plugin1, plugin2], 'onInit', parameter) | ||
| expect(plugin1.onInit).toHaveBeenCalledWith(parameter) | ||
|
|
||
| expect(() => callPluginsOnInit([plugin1, plugin2], PARAMETER)).not.toThrow() | ||
| expect(plugin1.onInit).toHaveBeenCalledWith(PARAMETER) | ||
| }) | ||
|
|
||
| it('returns true synchronously when there are no plugins', () => { | ||
| expect(callPluginsOnInit(undefined, PARAMETER)).toBe(true) | ||
| }) | ||
|
|
||
| it('returns true synchronously when every plugin returns void or true', () => { | ||
| const plugin1 = { name: 'a', onInit: jasmine.createSpy().and.returnValue(undefined) } satisfies RumPlugin | ||
| const plugin2 = { name: 'b', onInit: jasmine.createSpy().and.returnValue(true) } satisfies RumPlugin | ||
|
|
||
| const result = callPluginsOnInit([plugin1, plugin2], PARAMETER) | ||
|
|
||
| expect(result).toBe(true) | ||
| expect(plugin1.onInit).toHaveBeenCalledWith(PARAMETER) | ||
| expect(plugin2.onInit).toHaveBeenCalledWith(PARAMETER) | ||
| }) | ||
|
|
||
| it('returns false synchronously as soon as a sync plugin returns false, but still calls the other plugins', () => { | ||
| const plugin1 = { name: 'a', onInit: jasmine.createSpy().and.returnValue(false) } satisfies RumPlugin | ||
| const plugin2 = { name: 'b', onInit: jasmine.createSpy() } satisfies RumPlugin | ||
|
|
||
| const result = callPluginsOnInit([plugin1, plugin2], PARAMETER) | ||
|
|
||
| expect(result).toBe(false) | ||
| expect(plugin2.onInit).toHaveBeenCalledWith(PARAMETER) | ||
| }) | ||
|
|
||
| it('returns a Promise once a plugin returns a thenable, and resolves to true if nothing aborts', async () => { | ||
| const plugin1 = { name: 'a', onInit: () => Promise.resolve() } satisfies RumPlugin | ||
| const plugin2 = { name: 'b', onInit: jasmine.createSpy().and.returnValue(true) } satisfies RumPlugin | ||
|
|
||
| const result = callPluginsOnInit([plugin1, plugin2], PARAMETER) | ||
|
|
||
| expect(result).not.toBe(true) | ||
| expect(await result).toBe(true) | ||
| expect(plugin2.onInit).toHaveBeenCalledWith(PARAMETER) | ||
| }) | ||
|
|
||
| it('resolves to false once an async plugin resolves to false, without waiting for it before calling other plugins', async () => { | ||
| const plugin1 = { name: 'a', onInit: () => Promise.resolve(false) } satisfies RumPlugin | ||
| const plugin2 = { name: 'b', onInit: jasmine.createSpy() } satisfies RumPlugin | ||
|
|
||
| const result = callPluginsOnInit([plugin1, plugin2], PARAMETER) | ||
|
|
||
| expect(plugin2.onInit).toHaveBeenCalledWith(PARAMETER) | ||
| expect(await result).toBe(false) | ||
| }) | ||
|
|
||
| it('does not wait for an earlier plugin to resolve before calling the next plugin', async () => { | ||
| let clientTokenSeenByPlugin2: string | undefined | ||
| const plugin1 = { | ||
| name: 'a', | ||
| onInit: ({ initConfiguration }: { initConfiguration: RumInitConfiguration }) => | ||
| Promise.resolve().then(() => { | ||
| initConfiguration.clientToken = 'from-async-plugin' | ||
| }), | ||
| } satisfies RumPlugin | ||
| const plugin2 = { | ||
| name: 'b', | ||
| onInit: ({ initConfiguration }: { initConfiguration: RumInitConfiguration }) => { | ||
| clientTokenSeenByPlugin2 = initConfiguration.clientToken | ||
| }, | ||
| } satisfies RumPlugin | ||
| const initConfiguration = {} as RumInitConfiguration | ||
|
|
||
| await callPluginsOnInit([plugin1, plugin2], { initConfiguration, publicApi: {} as RumPublicApi }) | ||
|
|
||
| expect(clientTokenSeenByPlugin2).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('callPluginsOnRumStart', () => { | ||
| it('calls onRumStart on each plugin', () => { | ||
| const plugin1 = { name: 'a', onRumStart: jasmine.createSpy() } satisfies RumPlugin | ||
| const plugin2 = { name: 'b', onRumStart: jasmine.createSpy() } satisfies RumPlugin | ||
| const options = { addEvent: jasmine.createSpy(), addError: jasmine.createSpy() } | ||
|
|
||
| callPluginsOnRumStart([plugin1, plugin2], options) | ||
|
|
||
| expect(plugin1.onRumStart).toHaveBeenCalledWith(options) | ||
| expect(plugin2.onRumStart).toHaveBeenCalledWith(options) | ||
| }) | ||
|
|
||
| it('does not call onRumStart if the plugin does not have it', () => { | ||
| const plugin1 = { name: 'a', onRumStart: jasmine.createSpy() } satisfies RumPlugin | ||
| const plugin2 = { name: 'b' } satisfies RumPlugin | ||
| const options = { addEvent: jasmine.createSpy(), addError: jasmine.createSpy() } | ||
|
|
||
| expect(() => callPluginsOnRumStart([plugin1, plugin2], options)).not.toThrow() | ||
| expect(plugin1.onRumStart).toHaveBeenCalledWith(options) | ||
| }) | ||
|
|
||
| it('does nothing when there are no plugins', () => { | ||
| expect(() => | ||
| callPluginsOnRumStart(undefined, { addEvent: jasmine.createSpy(), addError: jasmine.createSpy() }) | ||
| ).not.toThrow() | ||
| }) | ||
| }) |
Oops, something went wrong.
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.