From 86795865e48d29bab6217d0d1f2c7e6847fb7c2c Mon Sep 17 00:00:00 2001 From: Shane Brunson Date: Wed, 12 Feb 2025 15:50:06 -0600 Subject: [PATCH] add timing data to xprops --- src/zoid/buttons/component.jsx | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/zoid/buttons/component.jsx b/src/zoid/buttons/component.jsx index 983ee51ff..5ffc17d5b 100644 --- a/src/zoid/buttons/component.jsx +++ b/src/zoid/buttons/component.jsx @@ -43,6 +43,7 @@ import { getJsSdkLibrary, wasShopperInsightsUsed, isPayPalTrustedUrl, + getSDKInitTime, } from "@paypal/sdk-client/src"; import { rememberFunding, @@ -1067,6 +1068,47 @@ export const getButtonsComponent: () => ButtonsComponent = memoize(() => { queryParam: true, }, + sdkInitTimings: { + type: "object", + queryParam: false, + required: false, + value: () => { + // eslint-disable-next-line compat/compat + const sdkScript = window?.performance + ?.getEntriesByType("resource") + // eslint-disable-next-line security/detect-unsafe-regex + .find(({ name }) => /paypal\.com(?::\d+)?\/sdk\/js/.test(name)); + + const isCached = (performanceEntry) => { + if ( + !performanceEntry || + typeof performanceEntry.duration === "undefined" + ) { + return "unknown"; + } + + return performanceEntry.duration === 0 ? "yes" : "no"; + }; + + let sdkInitTimeStamp; + + // this technically isn't possible with the way paypal-sdk-client + // is set up but one day it could be refactored and this would throw + // an error and block the button from rendering + try { + sdkInitTimeStamp = getSDKInitTime(); + } catch (error) { + // do nothing + } + + return { + sdkInitTimeStamp, + sdkScriptDownloadDuration: sdkScript?.duration, + isSdkCached: isCached(sdkScript), + }; + }, + }, + sdkMeta: { type: "string", queryParam: true,