How do I prevent repeated expensive operations during build?
Unanswered
Serengeti posted this in #help-forum
SerengetiOP
Trying to make a blog using
This works great, but it's very slow, and that's a problem because there are several pages that need the whole list of posts, including every post itself. The front page needs it to show the last published posts, the rss feed and sitemap uses it to generate that, each post uses it to find what posts are the next and previous in the list, the category page uses it to find which categories exists and what posts belong to each, and on and on...
What is a good clean way to only run this expensive operation once, preferably during build and never again? So it should only be done once during build, and then not again for the rest of the build, and also not when dynamic pages needs this data.
next-mdx-remote, and part of the process is to read through and get frontmatter from a bunch of files. This is how I do that: import fs from 'fs/promises'
import path from 'path'
import { compileMDX } from 'next-mdx-remote/rsc'
const contentDir = path.resolve(process.cwd(), 'content')
export async function getAllPostsMeta() {
const files = await fs.readdir(contentDir)
return Promise.all(
files.map(async (file) => {
const slug = file.replace(/\.mdx$/, '')
const source = await fs.readFile(path.join(contentDir, file), {
encoding: 'utf8',
flag: 'r',
})
const { frontmatter } = await compileMDX({
source,
options: { parseFrontmatter: true },
})
return {
slug,
pathname: `/blog/${slug}`,
meta: frontmatter,
}
})
)
}This works great, but it's very slow, and that's a problem because there are several pages that need the whole list of posts, including every post itself. The front page needs it to show the last published posts, the rss feed and sitemap uses it to generate that, each post uses it to find what posts are the next and previous in the list, the category page uses it to find which categories exists and what posts belong to each, and on and on...
What is a good clean way to only run this expensive operation once, preferably during build and never again? So it should only be done once during build, and then not again for the rest of the build, and also not when dynamic pages needs this data.