nextjs - 💡(How to fix) Fix 16.2 Files written to public/ after `next build` are not served on Vercel deployments [6 comments, 6 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#91864Fetched 2026-04-08 01:20:36
View on GitHub
Comments
6
Participants
6
Timeline
11
Reactions
2
Author
Timeline (top)
commented ×6subscribed ×2cross-referenced ×1issue_type_added ×1

Code Example

"scripts": {
  "build": "next build && npm run postbuild",
  "postbuild": "node ./scripts/generate-files.js"
}

---

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.0.0: Mon Aug 25 21:17:56 PDT 2025; root:xnu-12377.1.9~3/RELEASE_ARM64_T6030
  Available memory (MB): 18432
  Available CPU cores: 12
Binaries:
  Node: 22.19.0
  npm: 10.9.3
  Yarn: N/A
  pnpm: 10.32.1
Relevant Packages:
  next: 16.2.0 
  eslint-config-next: N/A
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/pensive-fast-8vtfhv

To Reproduce

The below steps were working just fine in version 16.1.7.

  1. Add a postbuild script that writes files to public/
"scripts": {
  "build": "next build && npm run postbuild",
  "postbuild": "node ./scripts/generate-files.js"
}

Where generate-files.js writes JSON files to public/ (e.g. public/data.json).

  1. Deploy to Vercel with Next.js 16.2.0 → Fetching /data.json returns 404 ✗

Current vs. Expected behavior

Current

Deploy to Vercel with Next.js 16.2.0 → Fetching /data.json returns 404 ✗ This is a regression from 16.1.7.

Expected

Deploy to Vercel with Next.js 16.2.0 → Fetching /data.json returns 200 ✓

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.0.0: Mon Aug 25 21:17:56 PDT 2025; root:xnu-12377.1.9~3/RELEASE_ARM64_T6030
  Available memory (MB): 18432
  Available CPU cores: 12
Binaries:
  Node: 22.19.0
  npm: 10.9.3
  Yarn: N/A
  pnpm: 10.32.1
Relevant Packages:
  next: 16.2.0 
  eslint-config-next: N/A
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

Vercel (Deployed)

Additional context

The issue is still present in version 16.2.1.

extent analysis

Fix Plan

The fix involves modifying the next.config.js file to include the generated files in the public directory.

  • Update next.config.js to include the following code:
module.exports = {
  //... other configurations ...
  experimental: {
    externalDir: true,
  },
}

Alternatively, you can also use the distDir option to specify the output directory for the generated files:

module.exports = {
  //... other configurations ...
  distDir: 'build',
}

Then, update the postbuild script to write files to the build directory instead of public:

"scripts": {
  "build": "next build && npm run postbuild",
  "postbuild": "node ./scripts/generate-files.js build"
}

And update the generate-files.js script to write files to the build directory:

const fs = require('fs');
const path = require('path');

const buildDir = process.argv[2] || 'build';
const data = { /* your data */ };

fs.writeFileSync(path.join(buildDir, 'data.json'), JSON.stringify(data));

Verification

To verify that the fix worked, deploy your application to Vercel and check that fetching /data.json returns a 200 status code.

Extra Tips

Make sure to update your generate-files.js script to handle the new output directory. Also, be aware that using the experimental option may have unintended consequences, so use it with caution.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - 💡(How to fix) Fix 16.2 Files written to public/ after `next build` are not served on Vercel deployments [6 comments, 6 participants]