5

I learned ReactJS and NextJS, but I am having trouble implementing rate-limiting/throttling in NextJS. I want there to be a limit on the number of times the user can access certain requests per period of time.

CodeRocks
  • 655
  • 5
  • 10
  • 25
  • With examples from https://nextjs.org/docs/api-routes/api-middlewares and [rate-limiter-flexible](https://www.npmjs.com/package/rate-limiter-flexible) you can build whatever you want. – Animir Sep 09 '20 at 12:41
  • Have you tried throttling the requests using (https://lodash.com/docs/#throttle) . – Jonathan Dsouza Dec 13 '20 at 17:12

2 Answers2

2

exist and answer for that on the next link.

  • looking into whether your serverless database offers any rate-limiting features.
  • if you have a reverse proxy available that has rate-limiting features, I'd use that.
  • since next.js API routes allow using existing connect middleware, you should be able to rate limit with one of the available rate-limiting middleware

https://github.com/vercel/next.js/discussions/12134#discussioncomment-6792

And for the last alternative, you can add an express library into a nextjs middleware

https://nextjs.org/docs/api-routes/api-middlewares

2

Update 2022-Jul-13: As of Next.js v12.2.0, middleware is now stable.


In the latest version of nextjs it is possible through https://nextjs.org/docs/middleware even though it is still in beta.

You can find examples here https://github.com/vercel/examples/tree/main/edge-functions

Your specific use case examples:

humble_barnacle
  • 460
  • 1
  • 4
  • 19