Next.js Discord

Discord Forum

Limiting number of requests to a server

Unanswered
フェイン (Fane) posted this in #help-forum
Open in Discord
If I am building a dashboard that interfaces to an external API, and that external API has a rate limit of 2 requests per second, how would I manage this in NextJS so that my user does not go over this limit? I imagine some kind of middleware is possible. Whether this is just a function that all other functions call, or a middleware, or a component that other components get wrapped in (a provider?) I would like to get some advice on. Thanks

1 Reply

Toyger
probably something like that
fetch('https://...', { next: { revalidate: 5 } })

5 seconds probably ok

or if you want in api itself then something like that
// pages/api/user.js
export default function handler(req, res) {
  res.setHeader('Cache-Control', 's-maxage=30');
  res.status(200).json({ name: 'John Doe' });
}