[GrowthBook provider] Auto refresh flags with a stale-while-revalidate strategy #148
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.