Unverified Commit e83bc939 authored by Josh Story's avatar Josh Story Committed by GitHub

Instant Insights: favor reported errors over missing slots (#93709)

When we can't validate the entire render because some expected slot
wasn't rendered we report that as an error. However we do this before
checking if there were errors to report from the slots the did render.
Since conditional rendering is a thing and it might inadvertantly cause
you to not see more directly actional feedback we now prioritize
reported errors from the validation before reporting any unrendered
slots
parent 4e633139
......@@ -1338,30 +1338,14 @@ export function getNavigationDisallowedDynamicReasons(
return validationPreventingErrors
}
if (!allRequiredBoundariesRendered(boundaryState)) {
const { thrownErrorsOutsideBoundary } = dynamicValidation
const rootInstantStack = dynamicValidation.slotStacks[0]
if (thrownErrorsOutsideBoundary.length === 0) {
const message = `Route "${workStore.route}": Could not validate \`unstable_instant\` because the target segment was prevented from rendering for an unknown reason.`
const error = rootInstantStack !== null ? rootInstantStack() : new Error()
error.name = 'Error'
error.message = message
return [error]
} else if (thrownErrorsOutsideBoundary.length === 1) {
const message = `Route "${workStore.route}": Could not validate \`unstable_instant\` because the target segment was prevented from rendering, likely due to the following error.`
const error = rootInstantStack !== null ? rootInstantStack() : new Error()
error.name = 'Error'
error.message = message
return [error, thrownErrorsOutsideBoundary[0] as Error]
} else {
const message = `Route "${workStore.route}": Could not validate \`unstable_instant\` because the target segment was prevented from rendering, likely due to one of the following errors.`
const error = rootInstantStack !== null ? rootInstantStack() : new Error()
error.name = 'Error'
error.message = message
return [error, ...(thrownErrorsOutsideBoundary as Error[])]
}
}
// Missing boundaries on their own aren't a strong signal — a parent
// layout may legitimately omit a slot. Run the remaining validation
// layers first; if they find a genuine problem we'd rather surface
// that. Only fall back to the missing-boundary error when nothing
// else explains the failure (so the user is still made aware that
// validation didn't complete). When we add a markers API, the
// marker-based variant of this check can become strict again.
//
// NOTE: We don't care about Suspense above body here,
// we're only concerned with the validation boundary
if (prelude !== PreludeState.Full) {
......@@ -1370,13 +1354,14 @@ export function getNavigationDisallowedDynamicReasons(
return dynamicErrors
}
if (prelude === PreludeState.Empty) {
// If a client component suspended prevented us from rendering a shell
// but didn't block validation, we don't require a prelude.
if (dynamicValidation.hasAllowedClientDynamicAboveBoundary) {
return []
}
// If we ever get this far then we messed up the tracking of invalid dynamic.
if (
prelude === PreludeState.Empty &&
!dynamicValidation.hasAllowedClientDynamicAboveBoundary &&
allRequiredBoundariesRendered(boundaryState)
) {
// If we ever get this far then we messed up the tracking of invalid
// dynamic. (When boundaries are missing the fallback below will
// surface a more useful error.)
return [
new InvariantError(
`Route "${workStore.route}" failed to render during instant validation and Next.js was unable to determine a reason.`
......@@ -1396,6 +1381,31 @@ export function getNavigationDisallowedDynamicReasons(
return [dynamicValidation.dynamicMetadata]
}
}
if (!allRequiredBoundariesRendered(boundaryState)) {
const { thrownErrorsOutsideBoundary } = dynamicValidation
const rootInstantStack = dynamicValidation.slotStacks[0]
if (thrownErrorsOutsideBoundary.length === 0) {
const message = `Route "${workStore.route}": Could not validate \`unstable_instant\` because the target segment was prevented from rendering for an unknown reason.`
const error = rootInstantStack !== null ? rootInstantStack() : new Error()
error.name = 'Error'
error.message = message
return [error]
} else if (thrownErrorsOutsideBoundary.length === 1) {
const message = `Route "${workStore.route}": Could not validate \`unstable_instant\` because the target segment was prevented from rendering, likely due to the following error.`
const error = rootInstantStack !== null ? rootInstantStack() : new Error()
error.name = 'Error'
error.message = message
return [error, thrownErrorsOutsideBoundary[0] as Error]
} else {
const message = `Route "${workStore.route}": Could not validate \`unstable_instant\` because the target segment was prevented from rendering, likely due to one of the following errors.`
const error = rootInstantStack !== null ? rootInstantStack() : new Error()
error.name = 'Error'
error.message = message
return [error, ...(thrownErrorsOutsideBoundary as Error[])]
}
}
// We had a non-empty prelude and there are no dynamic holes
return []
}
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