garmeeh / next-seo

Next SEO is a plug in that makes managing your SEO easier in Next.js projects.
MIT License
7.56k stars 390 forks source link

Make final breadcrumb's item field optional #1477

Open micahsteinberg opened 2 months ago

micahsteinberg commented 2 months ago

The Google SEO doc for adding breadcrumbs says

If the breadcrumb is the last item in the breadcrumb trail, item is not required. If item isn't included for the last item, Google uses the URL of the containing page.

Currently, providing an item for every breadcrumb is required in this library. This makes adding the final breadcrumb more difficult since you have to manually add the current url. I wish BreadCrumbJsonLdProps => ItemListElements => item would be optional. That way it would more closely follow Google's recommendation, and the final url would be added for you automatically.

An example output would be:

{
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{
    "@type": "ListItem",
    "position": 1,
    "name": "Books",
    "item": "https://example.com/books"
  },{
    "@type": "ListItem",
    "position": 2,
    "name": "Science Fiction",
    "item": "https://example.com/books/sciencefiction"
  },{
    "@type": "ListItem",
    "position": 3,
    "name": "Award Winners"
  }]
}

Related to https://github.com/garmeeh/next-seo/issues/95

The new prop types could look like:

export interface BreadCrumbJsonLdProps extends JsonLdProps {
  itemListElements: {
    item?: string;
    name: string;
    position: number;
  }[];
}

or maybe something like this instead to capture that it should only be for the last one:

export interface BreadCrumbJsonLdProps extends JsonLdProps {
  itemListElements: {
    item: string;
    name: string;
    position: number;
    isFinalBreadcrumb?: boolean;
  } | {
    item?: string;
    name: string;
    position: number;
    isFinalBreadcrumb: true;
  }[];
}

I realize this isn't entirely trivial to add because of how setItemListElements is used, but would be a nice addition! Thank you :)