microscopic-table-65564
03/05/2024, 8:54 PM_app.js
file:
const growthbook = new GrowthBook({
apiHost: "<https://cdn.growthbook.io>",
clientKey: growthbookClientKey,
enableDevMode: true,
subscribeToChanges: true,
decryptionKey: growthbookDecryptionKey,
trackingCallback: (experiment, result) => {
analytics.track(GA4_EXPERIMENT_VIEW, {
experiment_id: experiment.key,
variation_id: result.key,
});
},
});
function MyApp({ Component, pageProps }) {
//... Code omitted for brevity
useEffect(() => {
// Load features async
let isMounted = true;
if (isMounted && isReady && gaClientId) {
growthbook.setAttributes({
id: gaClientId,
url: window.location.href,
});
}
if (isMounted && isReady) {
growthbook.loadFeatures({ autoRefresh: true });
}
return () => {
isMounted = false;
};
}, [isReady, gaClientId]);
//... Code omitted for brevity
}
export default MyApp;
I understand that for react native and nodejs SDKs I need to add the node:crypto
polyfill, but this is not my case here, any ideas on what I am doing wrong?
SDK version: "<@U052WVA1MH6>/growthbook-react": "^0.24.0"
happy-autumn-40938
03/06/2024, 5:40 AMsetPolyfills({
SubtleCrypto: require("node:crypto").webcrypto.subtle,
});
Depending on Node runtime, this may or may not be needed. Node 18+ should have crypto.subtle
available on globalThis
, Node 17 only has crypto
(no .subtle), and Node 16 has nothing.microscopic-table-65564
03/06/2024, 4:21 PMmicroscopic-table-65564
03/06/2024, 5:04 PMSubtleCrypto
polyfill will not work on NextJS for client-side code, as node:crypto
is unavailable in the browser environment; this will only work on server-side code or API routes.
I ended up using this approach:
//_app.js
import { Crypto } from '@peculiar/webcrypto';
const crypto = new Crypto();
setPolyfills({
SubtleCrypto: crypto.subtle,
});