Skip to content

[GrowthBook provider] Auto refresh flags with a stale-while-revalidate strategy #148

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

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

Conversation

jdorn
Copy link

@jdorn jdorn commented Jun 17, 2025

Changes

Currently in the GrowthBook adapter, flags are fetched once at server startup, stored in-memory, and never refreshed.

This PR changes it so that every time a flag is evaluated, we check if the in-memory flag definitions are stale, and if so, refresh in the background from either Edge Config or the GrowthBook API (depending on which one the adapter is configured to use).

New adapter options / methods

There is a new adapter option cacheTTLms that defaults to 1s in dev (for better DX) and 30s in prod, but can be overridden as needed.

There is also a new adapater method refresh() that can be called manually.

Testing

I tested in a new Next.js app both with and without Edge Config and confirmed features refresh as expected.

Because of the stale-while-revalidate logic for both Edge Config and GrowthBook's built-in fetching, it takes at least 2 refreshes on dev to see any flag changes reflected.

Copy link

vercel bot commented Jun 17, 2025

@jdorn is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@AAorris AAorris left a comment

Choose a reason for hiding this comment

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

Sorry for the delay here! It looks good to me.
Could you add a changeset pnpm changeset before merging?

if (!isStale) return;
_lastRefreshTimestamp = Date.now();

if (options.edgeConfig) {
Copy link
Collaborator

@dferber90 dferber90 Aug 1, 2025

Choose a reason for hiding this comment

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

As a next step: When Edge Config is active you should be able to always read Edge Config without any caching on your ned. The Edge Config client returns the same reference if the Edge Config has not changed, so you can skip any re-initialization then.

Our goal with Edge Config is to eliminate any need for TTL or time based caching. Doesn't need to happen in this pull request but would be nice as a north star for this adapter.

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