notFound

La función notFound te permite renderizar el archivo not-found dentro de un segmento de ruta, además de inyectar una etiqueta <meta name="robots" content="noindex" />.

notFound()

Invocar la función notFound() lanza un error NEXT_HTTP_ERROR_FALLBACK;404 y termina el renderizado del segmento de ruta donde fue lanzado. Al especificar un archivo not-found, puedes manejar estos errores de forma elegante mostrando una interfaz de No Encontrado dentro del segmento.

app/user/[id]/page.js
import { notFound } from 'next/navigation'

async function fetchUser(id) {
  const res = await fetch('https://...')
  if (!res.ok) return undefined
  return res.json()
}

export default async function Profile({ params }) {
  const { id } = await params
  const user = await fetchUser(id)

  if (!user) {
    notFound()
  }

  // ...
}

Dato útil: notFound() no requiere que uses return notFound() debido a que utiliza el tipo never de TypeScript.

Historial de versiones

VersiónCambios
v13.0.0Se introdujo notFound.

On this page