Demo, all content is generated
Question

Vercel build: JavaScript heap out of memory since I added 300 blog posts

Solved · 243 views · asked by valentina_s · edited

Build worked until I imported my old blog (300 MDX posts). Now:

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

at "Generating static pages". Each post page uses generateStaticParams to prebuild all of them.

What I’ve tried

Set NODE_OPTIONS=--max-old-space-size=4096 in Vercel env vars. Got further, now fails at 250 pages instead of 180.

Comment
Does each post page import something that reads all posts? Sidebar, related posts, search index? jonas_k · edited

2 answers

Marked as helpful by the asker
jonas_k · edited

Raising the heap only moves the wall. Two better levers:

  1. Don't prebuild everything. Return only recent/popular posts from generateStaticParams, the rest render on first request and get cached:
export const dynamicParams = true; // default, but be explicit

export async function generateStaticParams() {
  const posts = await getPosts();
  return posts.slice(0, 50).map((p) => ({ slug: p.slug }));
}
  1. Check what each page loads. If every post page imports a helper that reads all 300 MDX files (for "related posts" or the sidebar), you parse 300 × 300 files. Build the index once into a small JSON and read that.

In your case I'd bet on #2, memory growing linearly with post count usually means that.

Comment
It was #2. The sidebar component called getAllPosts() which compiled every MDX file including the body. Made a lightweight getPostMeta() that only reads frontmatter. Build is 2 minutes and I removed the NODE_OPTIONS again. valentina_s · edited
Same bug in my blog, getAllPosts() in the sidebar. Thanks for posting the follow-up. sam_builds · edited
deploydan · edited

For context: the Vercel build container has more memory than the 4096 you gave Node, so the flag alone isn't the problem. But jonas's #2 is the real fix. Memory that scales with post count means something reads everything, per page.

Comment