#null or undefined metadata isnt ignored

4 messages · Page 1 of 1 (latest)

stark hedge
#

Hi when i export a metadata such as:

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?

hazy stagBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

pastel lily
# stark hedge Hi when i export a metadata such as: ```tsx export async function generateMetada...

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

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

stark hedge
#

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.