Search code examples
next.jsmiddlewarenext.js13

Using getStaticProps in Middleware.js


Is there any way to use getStaticProps inside of the middleware.js file in Next JS?

I want to hit an API endpoint from inside my middleware.js.

The API gives back my list of redirects and if it matches to the current route, I can use the midleware.js to redirect accordingly. I don't want to hit the API endpoint on every page load though. I thought I could use getStaticProps with the revalidate property set to only hit the API endpoint x amount of times per day.

Is there some way I can use getStaticProps from inside my midleware.js file?


Solution

  • I ended up using a package called memory-cache thanks to this answer: https://stackoverflow.com/a/62005551/8193573

    This method allowed me to limit my API requests from inside my middleware.tsx file so I didn't need getStaticProps in the end.