configcat / react-sdk

ConfigCat SDK for React. ConfigCat is a hosted feature flag service: https://configcat.com. Manage feature toggles across frontend, backend, mobile, desktop apps. Alternative to LaunchDarkly. Management app + feature flag SDKs.
MIT License
12 stars 7 forks source link

ReferenceError: XMLHttpRequest is not defined #7

Closed nanoqoi closed 2 years ago

nanoqoi commented 2 years ago

Hi! This is my first bug report and is a problem I've been encountering on one of my projects, I hope this is okay and something in scope for this repository. If this is something I should address on my own or figure out with NextJS instead, let me know!

Describe the bug

When trying to use the library in a NextJS environment, the Next SSR throws the following error:

error - unhandledRejection: ReferenceError: XMLHttpRequest is not defined
    at HttpConfigFetcher.fetchLogic (X:\SyndiqatProjects\bug-report-nextjs-configcat-react\example-next-app\node_modules\configcat-react\lib\cjs\ConfigFetcher.js:9:27)
    at ConfigServiceBase.fetchLogicInternal (X:\SyndiqatProjects\bug-report-nextjs-configcat-react\example-next-app\node_modules\configcat-common\lib\ConfigServiceBase.js:153:32)
    at ConfigServiceBase.fetchLogic (X:\SyndiqatProjects\bug-report-nextjs-configcat-react\example-next-app\node_modules\configcat-common\lib\ConfigServiceBase.js:88:14)
    at X:\SyndiqatProjects\bug-report-nextjs-configcat-react\example-next-app\node_modules\configcat-common\lib\ConfigServiceBase.js:54:19
    at new Promise (<anonymous>)
    at ConfigServiceBase.refreshLogicBaseAsync (X:\SyndiqatProjects\bug-report-nextjs-configcat-react\example-next-app\node_modules\configcat-common\lib\ConfigServiceBase.js:52:16)
    at AutoPollConfigService.<anonymous> (X:\SyndiqatProjects\bug-report-nextjs-configcat-react\example-next-app\node_modules\configcat-common\lib\AutoPollConfigService.js:113:51)
    at step (X:\SyndiqatProjects\bug-report-nextjs-configcat-react\example-next-app\node_modules\configcat-common\lib\AutoPollConfigService.js:48:23)
    at Object.next (X:\SyndiqatProjects\bug-report-nextjs-configcat-react\example-next-app\node_modules\configcat-common\lib\AutoPollConfigService.js:29:53)
    at fulfilled (X:\SyndiqatProjects\bug-report-nextjs-configcat-react\example-next-app\node_modules\configcat-common\lib\AutoPollConfigService.js:20:58)

Here's a code repository with the reproducible bug: https://github.com/Synqat/bug-report-nextjs-configcat-react

To reproduce

  1. Run npx create-next-app@latest
  2. Run yarn add configcat-react or equivalent in project directory
  3. Modify %app%/pages/_app.js to include provider from library (with sdkKey defined)
  4. Run yarn dev in project directory and hard refresh the web page

Expected behavior

The app should be able to fetch when being SSRed by NextJS. Perhaps using the more universal fetch method would be suitable?

Versions

laliconfigcat commented 2 years ago

Hello @Synqat,

Thanks for reaching out. For the short term, I'd recommend you use our JS-SSR SDK in your application: https://configcat.com/docs/sdk-reference/js-ssr/ It uses axios for fetching the config files: https://github.com/configcat/js-ssr-sdk/blob/master/src/ConfigFetcher.ts This SDK is not a React-specific SDK, it is a generic JS-based SDK. It doesn't have hooks/etc. For the long term, we'll figure out a way to provide a dedicated React SDK for SSR applications. I'm adding it to our backlog. Let me know if this works for you.

Cheers, Lali

nanoqoi commented 2 years ago

Hiya @laliconfigcat,

I'll check this out and see what I can get up to, thank you for the quick response! I look forward to the progress on this repository but happy to make a workaround for now 😄

github-actions[bot] commented 2 years ago

This issue is marked stale because it has no activity in the last 3 weeks. The issue will be closed in one week. Please remove the stale flag to keep it open.

github-actions[bot] commented 2 years ago

This issue was closed due to no activity.