Comments (9)
I don't think that will run as expected on Cloudflare/Vercel etc. I'll experiment with it later this week. And again, if you have any user-land implementations, sharing it will help :)
Well, we are using unleash-client
for now, but wanted to migrate to @unleash/nextjs
. We are using a Unleash
client instance, which will just fetch definitions every n seconds in the background. The client then exposes unleash.getFeatureToggleDefinitions()
method which just returns these definitions from memory.
Something similar to this could work for us:
let definitions: Promise<ClientFeaturesResponse> | undefined;
export const getDefinitions = async (): Promise<ClientFeaturesResponse> => {
if (!definitions) {
definitions = loadDefinitions();
setInterval(async () => {
const updated = await loadDefinitions();
definitions = Promise.resolve(updated);
}, 15000);
}
return definitions;
};
from unleash-client-nextjs.
Ok. Thank you for explaining 👍🏻
I'm in favor of wrapping cache(getDefinitions(...), ttl)
in a functional style instead of OOP instances. It's flexible. An example use case with long-running Next.js server is definitely something I'd like to cover soon.
from unleash-client-nextjs.
What we have right now
Server-side
App Router
You can do it with App Router server-side fetch cache, example here:
https://github.com/Unleash/unleash-client-nextjs/blob/main/example/src/app/app-page/page.tsx#L22
const definitions = await getDefinitions({
fetchOptions: {
next: { revalidate: 15 },
},
});
Docs: https://nextjs.org/docs/app/building-your-application/data-fetching#caching-data
Pages Router
You can have an API endpoint that will fetch definitions, with cache-control (Docs). Then use this URL as source of definitions. As in middleware example.
Client-side
Unleash Edge
If you need to scale right now, we have Unleash Edge. It's written in Rust, crazy-performant implementation of a concept similar to Unleash Proxy. We have clients experimenting with setting it up on CloudFlare Edge Workers. It doesn't work on Vercel yet.
from unleash-client-nextjs.
Plans
Client-side
I'll add an example of how to implement API route "proxying" for frontend API.
from unleash-client-nextjs.
@Tymek thanks for the reply. I'm particularly interested in the Pages Router
. While cache-control would help with the load on BE, its still a network roundtrip, which will add at least 50ms latency to the SSR render time (assuming its cached on CDN).
I was thinking more of a in-memory cache which would be instant and add no overhead at all.
from unleash-client-nextjs.
You can use caching on SSR. (docs). Downside is that if you are returning definitions from there to the client, you will expose your configuration. You can filter toggles you're interested in I guess. Will including this in the library help? I'm hesitant because resolving feature toggles on frontend (running server-client on frontend) isn't something Unleash supported before Next.js SDK.
Correct me if I'm wrong, but I don't think it's possible to store something in-memory in a Serverless/Edge environment. I looked into using KV store, but definitions are too big for that.
from unleash-client-nextjs.
You can use caching on SSR. (docs). Downside is that if you are returning definitions from there to the client, you will expose your configuration. You can filter toggles you're interested in I guess. Will including this in the library help? I'm hesitant because resolving feature toggles on frontend (running server-client on frontend) isn't something Unleash supported before Next.js SDK.
Correct me if I'm wrong, but I don't think it's possible to store something in-memory in a Serverless/Edge environment. I looked into using KV store, but definitions are too big for that.
By in memory I don't mean anything fansy, just storing things in a local variable. Not using Serveless/Edge, but I'm pretty sure you can do that. That being said, this would be more for the pages router, where you have a node server running for a long time.
from unleash-client-nextjs.
I don't think that will run as expected on Cloudflare/Vercel etc. I'll experiment with it later this week. And again, if you have any user-land implementations, sharing it will help :)
Previous Next.js SDK experiment was using this approach (archived https://github.com/Unleash/next-unleash). I'll look into bringing it back.
from unleash-client-nextjs.
Related Issues (13)
- Defining UNLEASH_SERVER_API_URL with a trailing slash is not working HOT 1
- Doesn't work but unleash-client does; Can't use instance_id HOT 7
- Wrong example in docs HOT 1
- "createContext only works in Client Components" error in API routes HOT 1
- getFrontendFlags hangs indefinitely when unleash-proxy is not reachable HOT 1
- Unable to setup with CSR and GitLab instance HOT 5
- Support for custom strategies HOT 6
- Metrics support
- Unleash: unable to fetch feature toggles TypeError: this.fetch is not a function HOT 4
- URL from getDefinitions is not loading correctly. HOT 5
- Middleware cause Error : Initial locale argument was not passed into serverSideTranslations HOT 1
- Broken Next.js build: Cannot destructure property 'updateContext' of null HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unleash-client-nextjs.