notFound

La fonction notFound vous permet d'afficher le fichier not-found dans un segment de route, ainsi que d'injecter une balise <meta name="robots" content="noindex" />.

notFound()

L'appel de la fonction notFound() génère une erreur NEXT_HTTP_ERROR_FALLBACK;404 et interrompt le rendu du segment de route dans lequel elle a été déclenchée. La spécification d'un fichier not-found vous permet de gérer ces erreurs de manière élégante en affichant une interface "Non trouvé" dans le segment.

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()
  }

  // ...
}

Bon à savoir : notFound() ne nécessite pas d'utiliser return notFound() car elle utilise le type never de TypeScript.

Historique des versions

VersionChangements
v13.0.0Introduction de notFound.

On this page