null or undefined metadata isnt ignored
Answered
Yellow-breasted Bunting posted this in #help-forum
Yellow-breasted BuntingOP
Hi when i export a metadata such as:
if
export async function generateMetadata({
params,
}: {
params: {
slug: string;
};
}): Promise<Metadata> {
const { data } = await query({params});
return {
title: data.title,
description: data.description,
};
}if
data.title is null or undefined, I would expect the metadata of layout.tsx to be picked up, but it doesnt. any ideas?Answered by joulev
this looks like a nextjs bug (or it could be an intended behaviour, then it would be a "feature"). in the source code, they just [check for available properties](https://github.com/vercel/next.js/blob/89e68377baf3ea6d2bf09e0ae694f57ec5c8ea93/packages/next/src/lib/metadata/resolve-metadata.ts#L166) without checking whether the values for those properties are falsy.
i think what you should do in this case is to do something like this (yeah it's dirty, i know, but there isn't much else that can be done here)
i'll see if i can find time to file a PR to fix this "bug" in nextjs
i think what you should do in this case is to do something like this (yeah it's dirty, i know, but there isn't much else that can be done here)
export async function generateMetadata({
params,
}: {
params: {
slug: string;
};
}): Promise<Metadata> {
const { data } = await query({params});
const metadata: Metadata = {
title: data.title,
description: data.description,
};
if (!data.title) delete metadata.title;
if (!data.description) delete metadata.description;
return metadata;
}i'll see if i can find time to file a PR to fix this "bug" in nextjs
2 Replies
@Yellow-breasted Bunting Hi when i export a metadata such as:
tsx
export async function generateMetadata({
params,
}: {
params: {
slug: string;
};
}): Promise<Metadata> {
const { data } = await query({params});
return {
title: data.title,
description: data.description,
};
}
if `data.title` is null or undefined, I would expect the metadata of `layout.tsx` to be picked up, but it doesnt. any ideas?
this looks like a nextjs bug (or it could be an intended behaviour, then it would be a "feature"). in the source code, they just [check for available properties](https://github.com/vercel/next.js/blob/89e68377baf3ea6d2bf09e0ae694f57ec5c8ea93/packages/next/src/lib/metadata/resolve-metadata.ts#L166) without checking whether the values for those properties are falsy.
i think what you should do in this case is to do something like this (yeah it's dirty, i know, but there isn't much else that can be done here)
i'll see if i can find time to file a PR to fix this "bug" in nextjs
i think what you should do in this case is to do something like this (yeah it's dirty, i know, but there isn't much else that can be done here)
export async function generateMetadata({
params,
}: {
params: {
slug: string;
};
}): Promise<Metadata> {
const { data } = await query({params});
const metadata: Metadata = {
title: data.title,
description: data.description,
};
if (!data.title) delete metadata.title;
if (!data.description) delete metadata.description;
return metadata;
}i'll see if i can find time to file a PR to fix this "bug" in nextjs
Answer
Yellow-breasted BuntingOP
Ah nice the delete keyword, forgot about that one, it does the job! Good work finding the source, thanks for looking into it for me.