A utility to fetch data at build-time and cache it for use in production.
MIT License
fetch-static
is a TypeScript package that wraps around the native fetch
function. It fetches data during build time, stores the result in the filesystem, and retrieves it from these static files during production. This allows you to avoid unnecessary network requests in production by using cached data, making it ideal for static site generation or build-time data fetching.
Note: This package is a workaround for the inability to use getStaticProps
in the Next.js App Router. It enables similar functionality by handling server-side data fetching and caching.
fetch
options, including HTTP methods, headers, and more.Install via npm:
npm install fetch-static
module.exports = [
"https://jsonplaceholder.typicode.com/todos/1",
"https://jsonplaceholder.typicode.com/todos/2"
];
Replace the URLs in the array with the endpoints you want to prefetch during the build.
Add Prebuild Script
"scripts": {
"prebuild": "fetch-static",
"build": "npm run prebuild && next build"
}
This script will run the prebuild process before building your Next.js application.