Unverified Commit b7438b4e authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[Instant] Show owner stack for validation-blocking errors (#91277)

Adds extra context to errors that we think may have prevented
validation:

<img width="816" height="765" alt="Screenshot 2026-03-12 at 22 07 40"
src="https://github.com/user-attachments/assets/35ce60d0-4840-496e-b91e-62ada4096f64"
/>

This is particularly useful for errors where the component comes from
node_modules, so it gets ignore-listed. previously, all we got is the
message with no stack, no we at least get a location. One notable case
of this is `next/dynamic` with `ssr: false`.

<img width="802" height="556" alt="Screenshot 2026-03-12 at 22 08 30"
src="https://github.com/user-attachments/assets/b1240732-3346-4056-89b2-9a864a3166f2"
/>
parent 90861b0f
...@@ -1115,5 +1115,6 @@ ...@@ -1115,5 +1115,6 @@
"1114": "Route \"%s\" accessed root param \"%s\" which is not defined in the \\`samples\\` of \\`unstable_instant\\`. Add it to the sample's \\`params\\` object.", "1114": "Route \"%s\" accessed root param \"%s\" which is not defined in the \\`samples\\` of \\`unstable_instant\\`. Add it to the sample's \\`params\\` object.",
"1115": "Route \"%s\" accessed cookie \"%s\" which is not defined in the \\`samples\\` of \\`unstable_instant\\`. Add it to the sample's \\`cookies\\` array, or \\`{ name: \"%s\", value: null }\\` if it should be absent.", "1115": "Route \"%s\" accessed cookie \"%s\" which is not defined in the \\`samples\\` of \\`unstable_instant\\`. Add it to the sample's \\`cookies\\` array, or \\`{ name: \"%s\", value: null }\\` if it should be absent.",
"1116": "Route \"%s\" accessed header \"%s\" which is not defined in the \\`samples\\` of \\`unstable_instant\\`. Add it to the sample's \\`headers\\` array, or \\`[\"%s\", null]\\` if it should be absent.", "1116": "Route \"%s\" accessed header \"%s\" which is not defined in the \\`samples\\` of \\`unstable_instant\\`. Add it to the sample's \\`headers\\` array, or \\`[\"%s\", null]\\` if it should be absent.",
"1117": "Instant validation boundaries should never appear in browser bundles." "1117": "Instant validation boundaries should never appear in browser bundles.",
"1118": "An error occurred while attempting to validate instant UI. This error may be preventing the validation from completing."
} }
...@@ -6,7 +6,7 @@ export interface NextError extends Error { ...@@ -6,7 +6,7 @@ export interface NextError extends Error {
page?: string page?: string
code?: string | number code?: string | number
cancelled?: boolean cancelled?: boolean
digest?: number digest?: string
} }
/** /**
......
...@@ -947,7 +947,7 @@ export function trackDynamicHoleInNavigation( ...@@ -947,7 +947,7 @@ export function trackDynamicHoleInNavigation(
export function trackThrownErrorInNavigation( export function trackThrownErrorInNavigation(
workStore: WorkStore, workStore: WorkStore,
dynamicValidation: InstantValidationState, dynamicValidation: InstantValidationState,
thrownError: unknown, thrownValue: unknown,
componentStack: string componentStack: string
) { ) {
const boundaryLocation = const boundaryLocation =
...@@ -955,7 +955,20 @@ export function trackThrownErrorInNavigation( ...@@ -955,7 +955,20 @@ export function trackThrownErrorInNavigation(
if (!boundaryLocation) { if (!boundaryLocation) {
// There's no validation boundary on the component stack. // There's no validation boundary on the component stack.
// This error may have blocked a boundary from rendering. // This error may have blocked a boundary from rendering.
dynamicValidation.thrownErrorsOutsideBoundary.push(thrownError)
// Wrap the error to provide component context.
// This helps for errors from node_modules which would otherwise
// have no useful stack information due to ignore-listing,
// e.g. next/dynamic with `ssr: false`.
const error = addErrorContext(
new Error(
'An error occurred while attempting to validate instant UI. This error may be preventing the validation from completing.',
{ cause: thrownValue }
),
componentStack,
null
)
dynamicValidation.thrownErrorsOutsideBoundary.push(error)
} else { } else {
// There's validation boundary on the component stack, // There's validation boundary on the component stack,
// so we know this error didn't block a validation boundary from rendering. // so we know this error didn't block a validation boundary from rendering.
...@@ -975,7 +988,7 @@ export function trackThrownErrorInNavigation( ...@@ -975,7 +988,7 @@ export function trackThrownErrorInNavigation(
} }
const message = `Route "${workStore.route}": Could not validate \`unstable_instant\` because an error prevented the target segment from rendering.` const message = `Route "${workStore.route}": Could not validate \`unstable_instant\` because an error prevented the target segment from rendering.`
const error = addErrorContext( const error = addErrorContext(
new Error(message, { cause: thrownError }), new Error(message, { cause: thrownValue }),
componentStack, componentStack,
null // TODO(instant-validation-build): conflicting use of cause null // TODO(instant-validation-build): conflicting use of cause
) )
......
...@@ -126,6 +126,12 @@ export default async function Page() { ...@@ -126,6 +126,12 @@ export default async function Page() {
<li> <li>
<DebugLinks href="/suspense-in-root/static/invalid-client-error-in-parent-blocks-children" /> <DebugLinks href="/suspense-in-root/static/invalid-client-error-in-parent-blocks-children" />
</li> </li>
<li>
<DebugLinks href="/suspense-in-root/static/invalid-csr-bailout-blocks-children" />
</li>
<li>
<DebugLinks href="/suspense-in-root/static/invalid-error-in-node-modules-blocks-children" />
</li>
<li> <li>
<DebugLinks href="/suspense-in-root/static/invalid-client-error-in-parent-sibling" /> <DebugLinks href="/suspense-in-root/static/invalid-client-error-in-parent-sibling" />
</li> </li>
......
import { Suspense, type ReactNode } from 'react' import { Suspense, type ReactNode } from 'react'
import { ErrorInSSR } from './client' import { ErrorInSSR } from './client'
import { connection } from 'next/server'
// Make sure that the holes from this layout aren't factored in for validation // Make sure that the holes from this layout aren't factored in for validation
// (otherwise, we'd check a navigation into it from the root layout and fail) // (otherwise, we'd check a navigation into it from the root layout and fail)
export const unstable_instant = false export const unstable_instant = false
export default function Layout({ children }: { children: ReactNode }) { export default async function Layout({ children }: { children: ReactNode }) {
await connection() // Prevent the error from failing the prerender in build
return ( return (
<> <>
<div> <div>
......
import { type ReactNode } from 'react' import { type ReactNode } from 'react'
import { ErrorInSSR } from './client' import { ErrorInSSR } from './client'
import { connection } from 'next/server'
// Make sure that the holes from this layout aren't factored in for validation // Make sure that the holes from this layout aren't factored in for validation
// (otherwise, we'd check a navigation into it from the root layout and fail) // (otherwise, we'd check a navigation into it from the root layout and fail)
export const unstable_instant = false export const unstable_instant = false
export default function Layout({ children }: { children: ReactNode }) { export default async function Layout({ children }: { children: ReactNode }) {
await connection() // Prevent the error from failing the prerender in build
return ( return (
<> <>
<div> <div>
......
'use client'
import { ReactNode } from 'react'
export function ClientWrapper({ children }: { children: ReactNode }) {
return (
<div>
<p>Hello from a client wrapper</p>
{children}
</div>
)
}
import type { ReactNode } from 'react'
import { LazyClientWrapperWithNoSSR } from './lazy-client'
import { connection } from 'next/server'
// Make sure that the holes from this layout aren't factored in for validation
// (otherwise, we'd check a navigation into it from the root layout and fail)
export const unstable_instant = false
export default async function Layout({ children }: { children: ReactNode }) {
await connection() // Prevent the error from failing the prerender in build
return (
<>
<div>
<p>
This layout renders a component wrapped in next/dynamic with ssr:
false around the children slot. This blocks the children slot so it
prevents validation.
</p>
<LazyClientWrapperWithNoSSR>{children}</LazyClientWrapperWithNoSSR>
</div>
</>
)
}
'use client'
import dynamic from 'next/dynamic'
export const LazyClientWrapperWithNoSSR = dynamic(
() => import('./client').then((mod) => mod.ClientWrapper),
{ ssr: false }
)
export const unstable_instant = {
prefetch: 'static',
}
export default function Page() {
return (
<main>
<p>This page is static, so it should pass instant validation</p>
</main>
)
}
import { Suspense, type ReactNode } from 'react'
import { ErrorInSSRFromPackage } from 'my-pkg'
import { connection } from 'next/server'
// Make sure that the holes from this layout aren't factored in for validation
// (otherwise, we'd check a navigation into it from the root layout and fail)
export const unstable_instant = false
export default async function Layout({ children }: { children: ReactNode }) {
await connection() // Prevent the error from failing the prerender in build
return (
<>
<div>
<p>
This layout errors in SSR, and the errors is caught by a Suspense
boundary, but it blocks the children slot so it prevents validation.
The error is thrown in a component from node_modules, which means that
the component is ignore-listed away.
</p>
<Suspense>
<ErrorInSSRFromPackage>{children}</ErrorInSSRFromPackage>
</Suspense>
</div>
</>
)
}
export const unstable_instant = {
prefetch: 'static',
}
export default function Page() {
return (
<main>
<p>This page is static, so it should pass instant validation</p>
</main>
)
}
import { Suspense, type ReactNode } from 'react' import { Suspense, type ReactNode } from 'react'
import { ErrorInSSR } from './client' import { ErrorInSSR } from './client'
import { connection } from 'next/server'
// Make sure that the holes from this layout aren't factored in for validation // Make sure that the holes from this layout aren't factored in for validation
// (otherwise, we'd check a navigation into it from the root layout and fail) // (otherwise, we'd check a navigation into it from the root layout and fail)
export const unstable_instant = false export const unstable_instant = false
export default function Layout({ children }: { children: ReactNode }) { export default async function Layout({ children }: { children: ReactNode }) {
await connection() // Prevent the error from failing the prerender in build
return ( return (
<> <>
<div> <div>
......
'use client'
export function ErrorInSSRFromPackage({ children }) {
if (typeof window === 'undefined') {
throw new Error('Error from node_modules')
}
return children
}
{
"name": "my-pkg",
"type": "module",
"exports": {
".": "./index.js"
}
}
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