Next.js Discord

Discord Forum

Metadata not showing up in dev mode for not-found.tsx

Unanswered
Polar bear posted this in #help-forum
Open in Discord
Polar bearOP
I have a very simple not-found.tsx in my app folder. After upgrading from 15.1.7 to 15.2.1, the metadata (title tag and meta description) I've defined in that file no longer shows up in dev mode. When I build and deploy the site that information shows up, but just not when I'm using npm run dev. I first noticed this when some of my automated Cypress tests started failing. I couldn't track down breaking changes, wondering if I might have overlooked them or if I need to do something different now with the not-found.tsx page after upgrading to 15.2.x.

import type { Metadata } from 'next';

export const metadata: Metadata = {
  title: 'Page Not Found',
  description: 'The page you are looking for does not exist.',
};

export default function NotFound() {
  return (
    <div className="container py-24">
      <h1 className="text-3xl font-semibold">Not Found</h1>
      <p className="mt-4 text-lg">The page you are looking for does not exist.</p>
    </div>
  );
}


If I watch closely, I can see a "flash" of the page title tag, but it seems to then be quickly overwritten / removed. For what it's worth, if I remove the not-found.tsx file and let Next display the standard 404, the meta information is included. Best I can tell, it has something to do with the way I'm handling dynamic routes.

For example, i have [...slug]/page.tsx and within that page, I do a query to see if that slug exists in Sanity. If it does not, I execute the following:

if (!page) {
    notFound();
  }

This worked in the past, but perhaps that's no longer the way to do it?

2 Replies

Polar bearOP
Catla
Most probably the reason is meta data can only be set on page.tsx and layout.tsx, as far I know and as of my use case, I was also not able to set metadata in not-found.tsx.