-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Add app/
directory support for custom title in Error page
#45620
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 |
WorkaroundI guess for a static title, this hack can be used with the App Router (doesn't result in a hydration error, interestingly enough): // app/not-found.tsx
export default function NotFound() {
return (
<div>
{/*
No support for metadata in not-found.tsx yet
https://github.com/vercel/next.js/pull/47328#issuecomment-1488891093
*/}
<title>Not Found | example.com</title>
</div>
);
} Maybe this is what it will look like in the future once React Float more fully lands... Or @gnoff @sebmarkbage should this already work now with the current state of React Float + Next.js? Was really surprised by lack of hydration error in both dev and production 🤔 Update Feb 2024: Maybe this is actually the new Document Metadata feature mentioned in the React Labs Feb 2024 "What We've Been Working On" blog post:
|
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 |
Regarding the custom metadata for the not-found.js page, maybe I've found a workaround! Currently, we are facing a problem where we throw a notFound() error when no page is found in our dynamic route. At that time, we don't see the meta title and description of the not-found.js page. Instead, we see the meta data of the current page. I believe this is logical and not a bug in Next.js. In the case of a dynamic route, the meta data of the not-found.js page should also be dynamic, as it might be useful in some scenarios. So, in the case of a dynamic route, we can set the meta data for the not-found.js page dynamically from page.js. It's important to note that the generateMetadata function should not throw a notFound error. Instead, the page component should throw a notFound error. For example, you can see the screenshot below. I hope this solution works. My folder structure:
|
@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. |
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: