https://www.growthbook.io/ logo
#ask-questions
Title
# ask-questions
m

microscopic-table-65564

03/05/2024, 8:54 PM
Hello, I am attempting to update the SDK to the latest version to begin using sticky bucketing and payload encryption. However, I am encountering an error. The app in question is a NextJS app (page router). I am unsure of what I might be missing. Can you please help me with this issue? This is the code in my
_app.js
file:
Copy code
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"
h

happy-autumn-40938

03/06/2024, 5:40 AM
You may still need to apply the polyfill via
Copy code
setPolyfills({
  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.
m

microscopic-table-65564

03/06/2024, 4:21 PM
Thanks @happy-autumn-40938. I’ll give it a try. I am using node 18+, though
Here’s an update on this if it’s useful for anyone. The
SubtleCrypto
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:
Copy code
//_app.js
import { Crypto } from '@peculiar/webcrypto';

const crypto = new Crypto();
setPolyfills({
  SubtleCrypto: crypto.subtle,
});
7 Views