-
Notifications
You must be signed in to change notification settings - Fork 28.3k
Add app/
directory support for custom title in Error page
#45620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
app/dir
support for custom title in Error pageapp/ directory
support for custom title in Error page
app/ directory
support for custom title in Error pageapp/
directory support for custom title in Error page
Interesting, seems like Shu was able to get a This was part of this PR: Not sure how this was achieved, I've asked over here: |
I was actually having a different problem to this one just now - the |
Workaround (
|
We're thinking to support it but might only support |
Oh too bad, so no way to generate dynamic titles / descriptions based on the requested content 🙁 Maybe there would still be some ways to support
|
@karlhorky error checking is possible to be added there to avoid using |
Sure, I was thinking of something kind of similar to the one in the issue description: // app/animals/[animalId]/not-found.js
export async function generateMetadata(props) {
return {
title: `Animal id ${props.params.animalId} not found`,
description: `There is no animal with id "${props.params.animalId}"`,
};
}
export default function AnimalNotFound(props) {
return <div>Sorry, an animal with id "{props.params.animalId}" was not found</div>;
} |
I see, dynamic routes with params makes sense. We'll think of support for that case |
I'm also hoping this feature is added soon. We were able to do this in the |
Would love this feature as well. Running a i18n site with this folder structure -> I want to be able to get the locale param so we can fetch correct translations for not-found page. |
A related issue I don't see mentioned here is that without metadata in error pages, the |
I have something working for me. I determine if I am in a 404 state in |
Currently, I need to manually add |
@huozhi Any ETA on when |
@bacvietswiss that issue is fixed in #50044 |
@leonbe02 Can you file another issue about that as the original issue here is related to metadata support |
@MuhammadMinhaj nice, but the workaround still doesn't solve de problem for the catch-all unmatched routes |
@itsjavi One possible workaround for that is similar to #48763 (comment) ...you can use a catch all route and put a A possibly simpler interim solution to keep everything in
...as I suppose for error pages other meta tags are probably less important and don't really need SSR support. However, I agree with this issue that error pages should have their own |
Did some research around metadata support for |
### What? Support metadata exports for `not-found.js` conventions ### Why? We want to define metadata such as title or description basic properties for error pages, including 404 and 500 which referrs to `error.js` and `not-found.js` convention. See more requests in #45620 Did some research around metadata support for not-found and error convention. It's possible to support in `not-found.js` when it's server components as currently metadata is only available but for `error.js` it has to be client components for now so it's hard to support it for now as it's a boundary. ### How? We determine the convention if we're going to render is page or `not-found` boundary then we traverse the loader tree based on the convention type. One special case is for redirection the temporary metadata is not generated yet, we leave it as default now. Fixes #52636
Should |
It only doesn’t work during development, if you make a build, the metadata is displayed correctly. I also noticed that if you specify robots, it is duplicated on the page 2 times. Initially, next adds robots to the page with the value noindex. |
Nope.. at least for me, I'm testing 14.0 and my issue is still there. |
Just like @karlhorky mentioned
works. But I am using template
So for someone like me, I had to write the below code
The <title> in my not-found.tsx replaces my template's fallback Title as well. I know its obvious, but I just wanted to let ya'll know |
Next.js 14.0.4 using App Router and export const metadata = {
title: 'Page not found',
description: 'This page could not be found',
}; It works when I load the page, briefly, but the Tested in developer-mode. Obviously, that should not happen. Reading the docs: https://nextjs.org/docs/app/building-your-application/optimizing/metadata ...also doesn't clear up the issue. Pages like |
Meanwhile, to solve the issue of page titles on 404-pages, assuming you have:
Then your not-found metadata currently cannot come from your not-found.tsx file, instead, you are to put it into your page.tsx file: type Params = {
id: string;
};
const metadata: Metadata = {
title: 'My page',
description: 'Viewing a single page',
};
export async function generateMetadata({
params,
}: {
params: Params;
}): Promise<Metadata> {
const exists = await pageExists(Number(params.id));
if (!exists) {
return {
title: 'Page not found',
description: 'This page could not be found.',
};
}
return metadata;
}
export default async function Page({ params }: { params: Params }) {
const exists = await pageExists(Number(params.id));
if (!exists) {
notFound();
}
return (
<p>
Your page goes here.
</p>
);
} My issue with this is that I want the not-found page to be exclusively responsible for deciding how a 404-situation would look and feel. Now I need to make two files responsible for it, and I even need to do But at least it works :) |
I'm running into this issue now too, and it feels very unintuitive. I'd expect that when I call Having the underlying page title show up breaks that experience. Suppose I have a dynamic page that fetches some resource and checks to see if the current user should be able to view it. If they shouldn't, then I want to tell them that the resource doesn't exist by rendering our 404 page. If the name of the resource shows up in the page title, it feels like a leaking of information that the current user shouldn't have access to. Of course, I can do the additional check in the page's Edit: I figured out that I can get the export const generateMetadata = async ({ params }: { params: id }): Promise<Metadata> => {
const thing = await getThing();
if (!thing) {
notFound();
}
const { user } = await getCurrentUser();
if (!user || !hasAccess(user, thing)) {
notFound();
}
return {
title: `Custom title for ${thing.name}`
}
} However this also feels like a hack and unexpected behaviour. Just wanted to mention this here in case others need a workaround while this issue is being addressed. |
This comment has been minimized.
This comment has been minimized.
This is my usecase and some notes on it: I have: When visiting: The /app/blog/[...slug]/page.tsx export const generateMetadata = async ({ params }: { params: { slug: string[] } }): Promise<Metadata> => {
const post = await getPostFromParams({ params });
if (!post) {
console.warn('Failed to find post during generateMetadata.');
return {
title: 'fallback';
};
}
return {
title: 'Blog!';
}
}
const BlogPostHandler = async ({ params }: { params: { slug: string[] } }) => {
const post = await getPostFromParams({ params });
if (!post) {
notFound();
return null;
}
//other logic
} not-found.tsx: import NotFoundPage from '@/containers/NotFoundPage';
import { Metadata } from 'next';
export const metadata: Metadata = {
title: 'Not Found',
};
const NotFound = () => <NotFoundPage />;
export default NotFound; The behaviour I observe is:
Now if both your PS: You should probably use It would be nice if we could export |
Is it possible to set metadata for error.tsx file on SSR? I cannot find any information, generateMetadata function also does not work for client components (error.tsx forced to be client component). |
error.tsx is client only page, for now, there's no possible way to set metadata or even use generateMetaData to error.tsx page But you can use a hack like this |
@Super-Kenil Thanks for reply It is logical that page with error must not be indexed, but still it is strange that there is no way to handle error page's meta tags on server side. All depends on project needs, it is not ok to just to remove such possibility at all. |
Here's my workaround for the Next.js metadata issue in not-found.tsx (App Router): export async function generateMetadata({ params }) {
const data = await checkIfExists(params.id)
if (!data) {
return {
title: '404 - Not Found',
description: 'This page could not be found'
}
}
return {
title: `Page: ${data.title}`,
description: data.description
}
}
export default async function Page({ params }) {
const data = await checkIfExists(params.id)
if (!data) {
notFound()
}
return <div>...</div>
} This approach works because:
Hope this helps others who are facing the same issue! 🙌 |
I think this can be handled by React v19's support for document metadata. But you would need to update all pages to use that mechanism instead of |
Some of it can, sure, but Next.js metadata offers way more features (metadata base, title templates, setting title will also set |
@huozhi am I correct in assuming this missing metadata feature also applies to the new I guess the current workaround is still the cc @gnoff , in case your work with Float has anything to do with this. |
I think this PR not full fixed, #52678 |
Just hit an issue with this - we upgraded from Next 14 to Next 15 and the workarounds above no longer seem to work. If metadata is specified in the Tried calling Adding At a loss as to what to do now. |
I have the exact same problem. I feel stumped. I don't know what to do. Except for the header no Metadata is substituted at all. Including favicon and other tags. |
Describe the feature you'd like to request
I would like to have the possibility to control the page title, description etc. in the error and not-found page.
currently Its possible to get around not-found page by creating a conditional in metadata function for the not-found page but is still not nice.
Describe the solution you'd like
I would like not-found.js and error.js accept
metadata
andgenerateMetadata()
similar topage.js
andlayout.js
Describe alternatives you've considered
for not-found, maybe allow to pass the metadata object as an argument for the function
The text was updated successfully, but these errors were encountered: