-
Notifications
You must be signed in to change notification settings - Fork 191
🐛 [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
Open
lierniel
wants to merge
11
commits into
main
Choose a base branch
from
stephan.koshcheev/RUM-18173/shopify-fix-double-session-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+654
−219
Open
Changes from 3 commits
Commits
Show all changes
11 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 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
Some comments aren't visible on the classic Files Changed page.
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
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💬 suggestion: If we want to decouple the logic for the onInit and onStart hooks, I would create two separate functions. We could keep it relatively simple: /**
* Calls each plugin's `onInit`, and returns whether the initialization should go on: `false` if any
* plugin aborts it. Stays synchronous as long as no plugin returns a thenable.
*/
export function callPluginsOnInit(
plugins: RumPlugin[] | undefined,
parameter: { initConfiguration: RumInitConfiguration; publicApi: RumPublicApi }
): boolean | Promise<boolean> {
const results = (plugins ?? []).map((plugin) => plugin.onInit?.(parameter))
if (results.some(isThenable)) {
return Promise.all(results.map((result) => Promise.resolve(result))).then(
(resolvedResults) => !resolvedResults.includes(false)
)
}
return !results.includes(false)
}
export function callPluginsOnRumStart(plugins: RumPlugin[] | undefined, options: OnRumStartOptions): void {
for (const plugin of plugins ?? []) {
plugin.onRumStart?.(options)
}
} |
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.
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.