Skip to content

Instantly share code, notes, and snippets.

@squashfold
Last active June 9, 2023 13:25
Show Gist options
  • Save squashfold/8569da2f0e95e0960e5241a0feca37b1 to your computer and use it in GitHub Desktop.
Save squashfold/8569da2f0e95e0960e5241a0feca37b1 to your computer and use it in GitHub Desktop.
Cache Posts in NextJS
const path = require('path')
const fs = require('fs') // Filesystem lets us create files and directories
const greyMatter = require('gray-matter') // Parses data from posts
function getPostData() {
const postsDirectory = path.join(process.cwd(), '_posts')
const fileNames = fs.readdirSync(postsDirectory)
const posts = fileNames.map((fileName) => {
const postId = fileName.replace(/\.md$/, '') // Replace with mdx if using MarkdownX
const fullPath = path.join(postsDirectory, fileName)
const postData = fs.readFileSync(fullPath, 'utf8')
const result = greyMatter(postData)
return {
postId,
slug: postId,
title: result.data.title,
coverImage: result.data.coverImage,
excerpt: result.data.excerpt,
date: result.data.date,
author: result.data.author,
}
})
const postsData = `export const data = ${JSON.stringify(posts)}`
return postsData
}
try {
fs.readdirSync('cache')
} catch (e) {
fs.mkdirSync('cache')
}
// Create a new directory 'data' within the cache folder
fs.mkdir(path.join(__dirname, 'data'),
{ recursive: true }, (error) => {
if (error) {
return console.error(error)
}
console.log('Directory created successfully!')
// Write cache file
fs.writeFile(`${path.join(__dirname, 'data')}/posts.js`, getPostData(), function (error) {
if (error) {
return console.log(error)
}
console.log(`Posts cached!`)
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment