The watchdog on my self-hosted site rescues it and sends email when "the whole site does not respond". But there is one kind of failure it cannot see: the site is alive and the home page is fine, but one page or API throws an exception on the server. Those errors only appear in logs/server.log, nobody reads it, and I only find out when a visitor reports it.
onRequestError: the error hook Next.js provides
Besides register(), instrumentation.ts can export onRequestError. Whenever a page render, Route Handler or Server Action fails on the server, Next.js calls it with the error, the request path and method, and "what kind of failure" (render, route, action, proxy).
I made it do just one thing: append a line of JSON to logs/errors.log.
What to record and what not to is the design point. It records the time, path, method, category, the first 300 characters of the error message, and the digest Next.js provides. The path is cut at the ?, dropping the query string, and no cookies, headers or IPs are recorded. When the file passes 1MB, only the last 500 lines are kept so it never grows without limit, and a failed write must be swallowed, since "recording an error" cannot itself cause another error.
The admin "Ops" page has a new block, "Recent server errors", which reads this file.
Do not email for a one-off, email when it keeps failing
Emailing on every error would drown me. The watchdog runs every 5 minutes, and along the way it reads the last 300 lines of errors.log and groups errors with the same "path plus message". If the same group appears 5 or more times in the last hour, it sends one alert (with a 6-hour cooldown); when the errors stop, it sends a follow-up saying "it has stopped".
I could not wait for a real failure to verify this PowerShell part, so I built a fake environment: an errors.log with 6 identical errors, 1 old error from 5 hours ago, and 1 corrupt line, and a fake function in place of Send-Alert that prints its arguments, then ran that section. The result: the 6-error case sends an email and the old error and corrupt line are skipped; with only 3 errors left, it takes the "clear the alert" path.
A small gotcha while verifying: folders starting with an underscore are not routes
To confirm onRequestError was really hooked up, I temporarily added an API that throws. The first time I put it at src/app/api/_boom/route.ts, and requests to /api/_boom kept getting 404, so nothing ever failed.
In the App Router, a folder starting with an underscore is a "private folder" and does not become a route. After renaming it to boom-test, the request returned 500 and a new line appeared in errors.log (the query string was indeed cut). After verifying, I deleted the test API and built again to make sure it would not reach production.
The other side of the same thing: the first request after a deploy is slow
Each deploy of this self-hosted site restarts Node. The Next.js Cache Components cache lives in memory and is emptied on restart; also, each page has to load its code the first time it is requested. A comment in scripts/server.js records the gap I measured back then: the first request after a restart took 5 seconds for the home page and 7 seconds for one admin page, and only 40 to 120 milliseconds once warm.
So after the server finishes starting, it requests a round of pages itself. It originally listed only 13 pages; I changed it to hit those 13 first, then read its own sitemap.xml and hit all the remaining public pages too, at most 4 at a time so it does not fight real people for CPU:
In testing, 62 paths were warm in about 2 to 3 seconds. The warm-up requests use the User-Agent kbs0830-warmup; the kbs0830- prefix is recognized by the stats code as the site's own tool, so it counts neither as visitors nor as unfiltered traffic.
Summary
onRequestErrorrecords server-side errors to a dedicated file, keeping only the necessary fields, dropping query strings, and capping the file size- Alerts need a threshold: email only if the same error occurs 5 or more times in an hour, with a cooldown and a recovery notice
- For code that cannot see a real failure, run the logic through with fake data and a fake send function
- In the App Router, a folder starting with
_is a private folder, not a route - Warm-up after restart: read your own sitemap, limit concurrency, and use a User-Agent the stats can exclude