Commit e4d8bd00 authored by Luke Sandberg's avatar Luke Sandberg

Fix 404 interception and SSR error overlay breakage

Two bugs:
1. The error shell intercepted ALL App Router error responses including
   404s, preventing custom not-found pages from rendering. Fix: only
   serve the shell when err !== null (actual error, not status-based).
2. The shell reused __next_error__ as HTML id, conflicting with
   app-render.tsx's SSR error rendering which uses the same id for
   CSR fallback. Fix: use __next_dev_error_shell__ as a distinct marker.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent c8e36f61
...@@ -360,8 +360,8 @@ export async function hydrate( ...@@ -360,8 +360,8 @@ export async function hydrate(
staticIndicatorState = { pathname: null, appIsrManifest: null } staticIndicatorState = { pathname: null, appIsrManifest: null }
webSocket = createWebSocket(assetPrefix, staticIndicatorState) webSocket = createWebSocket(assetPrefix, staticIndicatorState)
if (document.documentElement.id === '__next_error__') { if (document.documentElement.id === '__next_dev_error_shell__') {
// Dev error page served from the in-memory __next_error__ shell. // Dev error page served from the in-memory error shell.
// No real RSC payload exists and the full App Router machinery // No real RSC payload exists and the full App Router machinery
// (ServerRoot, AppRouter, etc.) would crash without valid router // (ServerRoot, AppRouter, etc.) would crash without valid router
// state. Instead, we skip mounting the React tree entirely. // state. Instead, we skip mounting the React tree entirely.
...@@ -369,6 +369,9 @@ export async function hydrate( ...@@ -369,6 +369,9 @@ export async function hydrate(
// build/runtime errors. The overlay itself is mounted independently // build/runtime errors. The overlay itself is mounted independently
// by renderAppDevOverlay() in the bootstrap's finally block. // by renderAppDevOverlay() in the bootstrap's finally block.
// //
// Note: this is distinct from __next_error__ which is used by
// app-render.tsx for SSR errors that have valid RSC payloads.
//
// Replay any error embedded in the <template> tag so the overlay // Replay any error embedded in the <template> tag so the overlay
// shows the initial SSR/compilation error immediately. // shows the initial SSR/compilation error immediately.
const ssrErrorTemplate = document.querySelector( const ssrErrorTemplate = document.querySelector(
......
...@@ -2810,10 +2810,11 @@ export default abstract class Server< ...@@ -2810,10 +2810,11 @@ export default abstract class Server<
} }
} }
// In dev mode, for App Router requests (or app-only projects), serve an // In dev mode, for App Router requests with real errors (compilation or
// in-memory __next_error__ HTML shell instead of going through the Pages // SSR crashes — NOT 404s), serve an in-memory HTML shell instead of going
// Router _error/_app/_document rendering pipeline. // through the Pages Router _error/_app/_document rendering pipeline.
if (this.dev && this.enabledDirectories.app) { // 404s must flow through to the normal not-found handling below.
if (this.dev && err != null && this.enabledDirectories.app) {
// For app-only projects we always use the in-memory shell. // For app-only projects we always use the in-memory shell.
// For mixed projects we auto-detect whether the failing route is an // For mixed projects we auto-detect whether the failing route is an
// App Router route, so callers don't need to explicitly tag requests. // App Router route, so callers don't need to explicitly tag requests.
......
...@@ -21,10 +21,11 @@ function escapeHtmlAttr(str: string): string { ...@@ -21,10 +21,11 @@ function escapeHtmlAttr(str: string): string {
/** /**
* Builds a minimal HTML shell for dev mode error pages in App Router apps. * Builds a minimal HTML shell for dev mode error pages in App Router apps.
* *
* Reuses the `__next_error__` pattern: the client detects * Uses a `__next_dev_error_shell__` marker on the `<html>` element so the
* `document.documentElement.id === '__next_error__'` and does CSR with * client can detect this shell and skip mounting the full App Router React
* the dev overlay instead of hydrating. Error details are embedded in a * tree. This is distinct from `__next_error__` which is used by
* `<template>` tag for the overlay to pick up. * app-render.tsx for SSR errors that have valid RSC payloads. Error details
* are embedded in a `<template>` tag for the overlay to pick up.
* *
* The script loading mirrors what React's renderToReadableStream does: * The script loading mirrors what React's renderToReadableStream does:
* all rootMainFiles are loaded as `<script async>` tags. The last * all rootMainFiles are loaded as `<script async>` tags. The last
...@@ -70,7 +71,7 @@ export async function buildAppRouterDevErrorHtml( ...@@ -70,7 +71,7 @@ export async function buildAppRouterDevErrorHtml(
.join('\n') .join('\n')
const html = `<!DOCTYPE html> const html = `<!DOCTYPE html>
<html id="__next_error__"> <html id="__next_dev_error_shell__">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
</head> </head>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment