Skip to content

Commit 7975ee4

Browse files
authored
add timing data to xprops (#2471)
1 parent 6dad53d commit 7975ee4

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Diff for: src/zoid/buttons/component.jsx

+42
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
getJsSdkLibrary,
4444
wasShopperInsightsUsed,
4545
isPayPalTrustedUrl,
46+
getSDKInitTime,
4647
} from "@paypal/sdk-client/src";
4748
import {
4849
rememberFunding,
@@ -1067,6 +1068,47 @@ export const getButtonsComponent: () => ButtonsComponent = memoize(() => {
10671068
queryParam: true,
10681069
},
10691070

1071+
sdkInitTimings: {
1072+
type: "object",
1073+
queryParam: false,
1074+
required: false,
1075+
value: () => {
1076+
// eslint-disable-next-line compat/compat
1077+
const sdkScript = window?.performance
1078+
?.getEntriesByType("resource")
1079+
// eslint-disable-next-line security/detect-unsafe-regex
1080+
.find(({ name }) => /paypal\.com(?::\d+)?\/sdk\/js/.test(name));
1081+
1082+
const isCached = (performanceEntry) => {
1083+
if (
1084+
!performanceEntry ||
1085+
typeof performanceEntry.duration === "undefined"
1086+
) {
1087+
return "unknown";
1088+
}
1089+
1090+
return performanceEntry.duration === 0 ? "yes" : "no";
1091+
};
1092+
1093+
let sdkInitTimeStamp;
1094+
1095+
// this technically isn't possible with the way paypal-sdk-client
1096+
// is set up but one day it could be refactored and this would throw
1097+
// an error and block the button from rendering
1098+
try {
1099+
sdkInitTimeStamp = getSDKInitTime();
1100+
} catch (error) {
1101+
// do nothing
1102+
}
1103+
1104+
return {
1105+
sdkInitTimeStamp,
1106+
sdkScriptDownloadDuration: sdkScript?.duration,
1107+
isSdkCached: isCached(sdkScript),
1108+
};
1109+
},
1110+
},
1111+
10701112
sdkMeta: {
10711113
type: "string",
10721114
queryParam: true,

0 commit comments

Comments
 (0)