Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for building Next.JS apps that use suspending flags. #1157

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

beeme1mr
Copy link
Member

This PR

  • adds an internal use polyfill
  • refactors suspense support to maintain state across rerenders
  • adds support for building Next.JS that use suspending flags.

Notes

Previously, the Next.JS build process would timeout when using a suspense flag hook. The reason for this is because the noop provider (which is used during a build) is never ready. That meant that the promise thrown to initiate suspense never resolved. To address this, I've added global state using a weak map that's keyed off a provider. A useRef won't help because the value is only retained if the component renders successfully.

How to test

I've added some tests and manually tested in the Toggle Shop.

Note

Marking as a draft until #1152 is merged.

Copy link
Member

@lukas-reining lukas-reining left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generally looks good to me!
I left one concern that I am not really sure about.

* work because the value isn't preserved when a promise is thrown in a component,
* which is how suspense operates.
*/
const globalProviderSuspenseStatus = new WeakMap<Provider, Promise<unknown>>();
Copy link
Member

@lukas-reining lukas-reining Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not 100% sure but globals in Next.js code tend to create weird states.
If this code is run on the server globalProviderSuspenseStatus will be the same for every caller if I recall correctly.
This means when we save a provider here I think we could either get multiple instances of the provider, or the same provider with the same eval context on multiple clients.

But I might be overseeing anything here that avoids this.

This is what I am not sure about if it applies here: vercel/next.js#47605

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that Vercel's SWR library also maintains a global state using a weakmap.

https://github.com/vercel/swr/blob/975f99118fc11eedc79accceb6eeaead19753db9/src/_internal/utils/global-state.ts

In our case, the weakmap is useful for handling the noop provider and some minor initialization optimizations. I don't think it would be a big deal if we lose state between requests. Basically, it would behave how it current done.

Copy link
Member

@lukas-reining lukas-reining Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem would be more that we might confuse providers.

From the Issue:

Since mutating server-side global variables does some combination of a) not updating when you expect them to b) resetting arbitrarily c) leaking memory and d) leaking user data,

E.g. look at TanStack Query: https://tanstack.com/query/latest/docs/framework/react/guides/ssr?from=reactQueryV3#initial-setup
They say:

// NEVER DO THIS:
// const queryClient = new QueryClient()
//
// Creating the queryClient at the file root level makes the cache shared
// between all requests and means all data gets passed to all users.
// Besides being bad for performance, this also leaks any sensitive data.

Copy link
Member

@lukas-reining lukas-reining Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine that when we have a static context provider in this list, this could become a problem here.
Maybe we have that problem already between different users with our singleton API, which does not have it's roots in this PR 🤔

Copy link
Member

@lukas-reining lukas-reining Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at swr, I see that the WeakMap maps a cache object to a specific cache state.
For me it is not entirely clear if the provider stored in our case is unique for the user or even the request.

I think OpenFeature.setProvider could be a problem in the React SDK when used with Next.js.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may be right and perhaps I should update the PR title to not include Next.JS. This PR definitely addresses a build-time issue that's easily reproducible with Next. However, as you stated, we'll likely need to do more to properly support Next.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR definitely addresses a build-time issue that's easily reproducible with Next.

Totally!

You may be right and perhaps I should update the PR title to not include Next.JS
Removing Next from the title would make sense to me.

The problem is that running Next with the React SDK currently can lead to security problems if I understand correctly.

Copy link

@RedbackThomson RedbackThomson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see anything obviously violating here, but I haven't done the due diligence to run it locally yet

if (!statusPromiseRef) {
// Noop provider is never ready, so we resolve immediately
const statusPromise = provider !== NOOP_PROVIDER ? isProviderReady(client) : Promise.resolve();
// Storing the promise globally because

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This comment is unfinished

@beeme1mr beeme1mr marked this pull request as ready for review March 6, 2025 20:49
@beeme1mr beeme1mr requested a review from a team as a code owner March 6, 2025 20:49
@beeme1mr beeme1mr changed the title feat: add support for suspense flags in next.js feat: add support for building Next.JS apps that use suspending flags. Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants