CodeRocks
CodeRocks

Reputation: 715

Implement Rate Limiting in NextJS

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.

Upvotes: 5

Views: 20315

Answers (2)

humble_barnacle
humble_barnacle

Reputation: 490

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:

Upvotes: 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

Upvotes: 2

Related Questions