NoFallbackError Logged To Console.Error When DynamicParams=False Rejects A Param
When dynamicParams=false is set and a request arrives for a param not in generateStaticParams, Next.js correctly returns a 404 but also logs 'Error: Internal: NoFallbackError' to console.error. This internal control-flow error should be handled silently, but it leaks to application logs and triggers false-positive error alerts in APM tools.
Next.js throws NoFallbackError as an internal control-flow mechanism to signal that a dynamic route segment has no fallback and should result in a 404. However, in the request handling pipeline, this error is caught by a generic error handler that unconditionally logs it via console.error before rethrowing or handling the 404 response. The handler fails to distinguish NoFallbackError from unexpected errors.
1. Create a Next.js route with dynamicParams = false and define generateStaticParams returning a limited set of params.
2. Build and start the server (next start or custom server using getRequestHandler).
3. Request a URL with a param not included in generateStaticParams (e.g., /nonexistent).
4. Observe that the server returns a 404 response, but console.error shows 'Error: Internal: NoFallbackError'.
5. Confirm that wrapping console.error (as done in the reproduction repo) reveals the leak even when the response is correct.
Fixing Code Block
```typescript
// File: packages/next/src/server/next-server.ts
// Inside the catch block of route rendering (approx. line where errors are logged)
import { NoFallbackError } from './lib/no-fallback-error';
// In handleRequest or similar:
try {
// ... existing rendering logic
} catch (err) {
// NoFallbackError is an internal control flow error from dynamicParams=false.
// It is expected and handled by returning a 404, so do not log it as an error.
if (err instanceof NoFallbackError) {
throw err; // propagate for upstream 404 handling without console.error
}
console.error(err);
throw err;
}
```
Alternatively, if console.error is called inside a shared error-logging function, modify that function to skip NoFallbackError:
```typescript
function logError(err: unknown) {
if (err instanceof NoFallbackError) {
return; // internal control flow, not a real error
}
console.error(err);
}
```
The fix adds a type check for NoFallbackError before logging. Since NoFallbackError is explicitly used by Next.js to stop rendering and trigger a 404 when dynamicParams is false, it should not be treated as an unexpected error. By rethrowing without console.error (or skipping logging in the centralized error logger), the internal signal remains functional but silent, eliminating false-positive error alerts while preserving the correct 404 response.
Edge Case Audit
This change suppresses console.error output for NoFallbackError, which may remove a diagnostic signal that some developers have (incorrectly) relied on to detect dynamic route configuration issues. Ensure that all routes with dynamicParams=false still return 404 as expected after the patch. Rollback strategy: revert the specific check or change the logging level from console.error to a debug/verbose log if full silence is undesirable. No concurrency or multi-threading impact (Node.js single-threaded), but be careful when upgrading/downgrading Next.js versions as internal module paths may change; ensure NoFallbackError import path is still valid.