Reputation: 135
It is well known that you can serve static files (e.g., JSON, CSV, etc.) in the public folder of a Next.js project (e.g., public/data/MyData.json
). It is also well known that you can have pages with wildcard names (e.g., pages/subfolder/[name]/index.tsx
) inside the pages folder, by leveraging getStaticPaths
. My question is: can we also leverage getStaticPaths
to build static files inside the public folder (e.g., maybe something like public/data/[name].json
or similar, you get my intention).
It seems that such would be a natural addition to Next.js. It makes so much sense to me that I wonder if it's already supported, but I couldn't find a discussion on the internet about this besides this unanswered GitHub issue in the official Vercel's repository (Return a JSON response from page instead of a React component? #37937 ), which discussed it in the context of incremental static regeneration. I don't need it in the context of incremental static regeneration, I just want to be able to build my static files at build time when I run the command yarn build
.
Naturally, the use case is that we have data from somewhere else that we update sometimes. But I don't want to keep running server-less functions to retrieve that data. While I can use cache in the API request handlers, and also the front-end, ideally I just feel that I would much prefer to have static JSON files inside the public folder that can retrieved much faster without any cloud computation inside an API endpoint.
It really just make sense, so there must be a way to do it. I thought of having a page return the content (i.e., something inside the pages folder), but I realized those can only return React components. I hope Vercel realizes that the same paradigm of getStaticPaths
is applicable to more than mere React components/pages.
By the way, I know I could have some sort of function that writes my static JSON data to a bucket in AWS or something similar, but I would much prefer to apply an approach very similar to getStaticPaths
which I am already doing all over my Next.js project. It would be just so convenient.
Upvotes: 1
Views: 79