How to cache data in Next.js globally across all pages at build time
In my Next.js based website had the need to fetch data from an API at build time.
I tried various solutions, nothing worked. Then after lots of research and trial and error, here’s what worked.
The solution works both on localhost (development) and on Vercel (production).
This is the scenario: I have the emails of the “members” stored somewhere. It can be a remote database, Airtable, anything.
I only want to download this data once, and then have it available on the website, until I trigger the next build.
This members list is very static. It might change once a day max. If this list changes, I will just programmatically trigger a redeploy on Vercel using their Deploy Hooks.
Here’s the plan. We’ll create a local cache of the data, in a .members file.
A function in the lib/members.js file will take care of fetching the data and storing it in the cache as JSON when it’s first called, and subsequently it will read the data from the cache:
import fs from 'fs'
import path from 'path'
function fetchMembersData() {
console.log('Fetching members data...')
return [{ email: 'test1@email.com' }, { email: 'test12@email.com' }]
}
const MEMBERS_CACHE_PATH = path.resolve('.members')
export default async function getMembers() {
let cachedData
try {
cachedData = JSON.parse(
fs.readFileSync(path.join(__dirname, MEMBERS_CACHE_PATH), 'utf8')
)
} catch (error) {
console.log('Member cache not initialized')
}
if (!cachedData) {
const data = fetchMembersData()
try {
fs.writeFileSync(
path.join(__dirname, MEMBERS_CACHE_PATH),
JSON.stringify(data),
'utf8'
)
console.log('Wrote to members cache')
} catch (error) {
console.log('ERROR WRITING MEMBERS CACHE TO FILE')
console.log(error)
}
cachedData = data
}
return cachedData
}
Now inside any page we can add
import getMembers from 'lib/members'
and inside getStaticProps():
const members = await getMembers()
Now with this data we can do what we want.
A quick example is to add members to the props object returned by getStaticProps, adding it to the props received by the page component, and we can iterate over it in the page:
{members?.map((member) => {
return (
<p key={member.email} className='mt-3'>
{member.email}
</p>
)
})} download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.