File size: 5,702 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
'use client'
/**
* HTTPAccessFallbackBoundary is a boundary that catches errors and renders a
* fallback component for HTTP errors.
*
* It receives the status code, and determine if it should render fallbacks for few HTTP 4xx errors.
*
* e.g. 404
* 404 represents not found, and the fallback component pair contains the component and its styles.
*
*/
import React, { useContext } from 'react'
import { useUntrackedPathname } from '../navigation-untracked'
import {
HTTPAccessErrorStatus,
getAccessFallbackHTTPStatus,
getAccessFallbackErrorTypeByStatus,
isHTTPAccessFallbackError,
} from './http-access-fallback'
import { warnOnce } from '../../../shared/lib/utils/warn-once'
import { MissingSlotContext } from '../../../shared/lib/app-router-context.shared-runtime'
interface HTTPAccessFallbackBoundaryProps {
notFound?: React.ReactNode
forbidden?: React.ReactNode
unauthorized?: React.ReactNode
children: React.ReactNode
missingSlots?: Set<string>
}
interface HTTPAccessFallbackErrorBoundaryProps
extends HTTPAccessFallbackBoundaryProps {
pathname: string | null
missingSlots?: Set<string>
}
interface HTTPAccessBoundaryState {
triggeredStatus: number | undefined
previousPathname: string | null
}
class HTTPAccessFallbackErrorBoundary extends React.Component<
HTTPAccessFallbackErrorBoundaryProps,
HTTPAccessBoundaryState
> {
constructor(props: HTTPAccessFallbackErrorBoundaryProps) {
super(props)
this.state = {
triggeredStatus: undefined,
previousPathname: props.pathname,
}
}
componentDidCatch(): void {
if (
process.env.NODE_ENV === 'development' &&
this.props.missingSlots &&
this.props.missingSlots.size > 0 &&
// A missing children slot is the typical not-found case, so no need to warn
!this.props.missingSlots.has('children')
) {
let warningMessage =
'No default component was found for a parallel route rendered on this page. Falling back to nearest NotFound boundary.\n' +
'Learn more: https://nextjs.org/docs/app/building-your-application/routing/parallel-routes#defaultjs\n\n'
const formattedSlots = Array.from(this.props.missingSlots)
.sort((a, b) => a.localeCompare(b))
.map((slot) => `@${slot}`)
.join(', ')
warningMessage += 'Missing slots: ' + formattedSlots
warnOnce(warningMessage)
}
}
static getDerivedStateFromError(error: any) {
if (isHTTPAccessFallbackError(error)) {
const httpStatus = getAccessFallbackHTTPStatus(error)
return {
triggeredStatus: httpStatus,
}
}
// Re-throw if error is not for 404
throw error
}
static getDerivedStateFromProps(
props: HTTPAccessFallbackErrorBoundaryProps,
state: HTTPAccessBoundaryState
): HTTPAccessBoundaryState | null {
/**
* Handles reset of the error boundary when a navigation happens.
* Ensures the error boundary does not stay enabled when navigating to a new page.
* Approach of setState in render is safe as it checks the previous pathname and then overrides
* it as outlined in https://react.dev/reference/react/useState#storing-information-from-previous-renders
*/
if (props.pathname !== state.previousPathname && state.triggeredStatus) {
return {
triggeredStatus: undefined,
previousPathname: props.pathname,
}
}
return {
triggeredStatus: state.triggeredStatus,
previousPathname: props.pathname,
}
}
render() {
const { notFound, forbidden, unauthorized, children } = this.props
const { triggeredStatus } = this.state
const errorComponents = {
[HTTPAccessErrorStatus.NOT_FOUND]: notFound,
[HTTPAccessErrorStatus.FORBIDDEN]: forbidden,
[HTTPAccessErrorStatus.UNAUTHORIZED]: unauthorized,
}
if (triggeredStatus) {
const isNotFound =
triggeredStatus === HTTPAccessErrorStatus.NOT_FOUND && notFound
const isForbidden =
triggeredStatus === HTTPAccessErrorStatus.FORBIDDEN && forbidden
const isUnauthorized =
triggeredStatus === HTTPAccessErrorStatus.UNAUTHORIZED && unauthorized
// If there's no matched boundary in this layer, keep throwing the error by rendering the children
if (!(isNotFound || isForbidden || isUnauthorized)) {
return children
}
return (
<>
<meta name="robots" content="noindex" />
{process.env.NODE_ENV === 'development' && (
<meta
name="boundary-next-error"
content={getAccessFallbackErrorTypeByStatus(triggeredStatus)}
/>
)}
{errorComponents[triggeredStatus]}
</>
)
}
return children
}
}
export function HTTPAccessFallbackBoundary({
notFound,
forbidden,
unauthorized,
children,
}: HTTPAccessFallbackBoundaryProps) {
// When we're rendering the missing params shell, this will return null. This
// is because we won't be rendering any not found boundaries or error
// boundaries for the missing params shell. When this runs on the client
// (where these error can occur), we will get the correct pathname.
const pathname = useUntrackedPathname()
const missingSlots = useContext(MissingSlotContext)
const hasErrorFallback = !!(notFound || forbidden || unauthorized)
if (hasErrorFallback) {
return (
<HTTPAccessFallbackErrorBoundary
pathname={pathname}
notFound={notFound}
forbidden={forbidden}
unauthorized={unauthorized}
missingSlots={missingSlots}
>
{children}
</HTTPAccessFallbackErrorBoundary>
)
}
return <>{children}</>
}
|