Google Search Console's "Server error (5xx)" report had a few entries I could not explain. In server.log I found a very regular batch of 500s:
All of them are vulnerability scanners guessing paths. Being scanned is normal; every public site gets it. What is not normal is the response code. These URLs do not exist, so they should return 404, not 500. Between 10/14 and 11/05 there were 37 of them, 12 of which were /blog/.env.
Only under /blog/
The same scanners hitting /wp-login.php and /.env at the root correctly got 404 (197 requests over the same period, all 404). /blog/wp-includes/wlwmanifest.xml was also a 404. Only the shape /blog/<one segment> returned 500.
That shape matches the article route app/blog/[slug]/page.tsx. wp-includes/wlwmanifest.xml has two segments, does not match [slug], and so is fine.
Why existing posts work and missing ones break
The article page has generateStaticParams, so every post is pre-rendered at build time. When a visitor opens a real post, Next.js returns the built HTML without running any code.
A slug that does not exist is different. By default Next.js "tries to render it on demand", which actually runs the page code, and the first thing that code does is read the post list:
process.cwd() is supposed to be the project folder. It is during local development and during next build, so it never caused a problem.
On the live site, cwd is C:\Windows\System32
The production Node.js is started by Windows Task Scheduler at boot. A scheduled task has a "Start in" field, and I had left it empty. When it is empty, the process's working directory is C:\Windows\System32.
So readdirSync was reading C:\Windows\System32\content\blog. The folder does not exist → exception → the page blew up before it ever reached notFound() → 500.
Requesting an arbitrary /blog/nope on the live site did return 500.
I already knew the cwd was wrong
Funnily enough, I had hit this once before, on 7/17. The visitor statistics had to write data/visitor-stats.json, and I discovered then that process.cwd() pointed at System32. The fix was to pass the absolute path in through an environment variable in server.js:
Fixing one file at a time misses the next one. The blog came later, blog.ts naturally used process.cwd(), local tests all passed, until a scanner found it for me.
The fix: one line
Rather than patch file after file, I switch the working directory to the project folder when the server starts:
Now all code that depends on process.cwd() is correct at once. After deploying, /blog/.env returned 404.
What I learned
- "All the pre-rendered pages work" can hide the on-demand rendering path. Normal visitors never reach it; only people guessing URLs do.
- Scanners are a free testing tool. They hit lots of URLs you would never think to test.
- Fix environment differences at the source. Patching environment variables file by file only fixed the one file I had thought of; setting cwd when the process starts fixed "cwd is wrong" itself.