After reading through the Next.JS documentation, I came across an interesting point.
Note: Instead of using fetch() to call an API route in getStaticProps, it's recommended to directly import the logic from within your API route and make necessary code adjustments accordingly.
Fetching data from an external API is acceptable!
However, this raises two questions for me regarding internal API usage:
- How should POST requests be handled? Should only GET requests be managed through the API while excluding POST or PUT methods? This approach seems unusual to me.
- If internal POST requests shouldn't be made, why include the option to use the API at all?
edit:
Upon further consideration, one reason behind this could be that when utilizing getStaticProps and generating a static version of the site, the API may not be active during compilation. It can easily be resolved by running the API concurrently. Since GetStaticProps isn't particularly relevant for interactive pages involving POST requests.
edit2:
Another user also pointed out a solution. Check out Next.js - Error: only absolute urls are supported. The suggestion is to run export and server separately so that you can perform fetches in static props when required, consolidating everything in one place.