Skip to content
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

feat(breadcrumbs): add seo breadcrumbList schema #212

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/breadcrumbs/src/Breadcrumbs.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const LIST_ITEM_PROPS = {
itemProp: 'itemListElement',
itemScope: true,
itemType: 'https://schema.org/ListItem',
};

export { LIST_ITEM_PROPS };
39 changes: 34 additions & 5 deletions packages/breadcrumbs/src/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cn from 'classnames';
import { v4 as uuidv4 } from 'uuid';

import useBreakpoint from './hooks/useBreakpoint.hook';
import { LIST_ITEM_PROPS } from './Breadcrumbs.constants';
import {
Breadcrumb,
BreadcrumbLinkProps,
Expand Down Expand Up @@ -56,20 +57,33 @@ const Breadcrumbs: FC<BreadcrumbsProps> = ({
<ul
className={cn(styles.breadcrumbWrapper, className)}
data-test-id={dataTestId}
itemScope={true}
itemType="https://schema.org/BreadcrumbList"
>
{firstBreadcrumb?.link ? (
<li
className={styles.breadcrumbsList}
key={`${firstBreadcrumb.title}-${0}`}
{...LIST_ITEM_PROPS}
>
<Link className={styles.breadcrumbLink} link={firstBreadcrumb.link}>
<Link
className={styles.breadcrumbLink}
itemProp="item"
link={firstBreadcrumb.link}
>
<span itemProp="name">{firstBreadcrumb.title}</span>
<meta content="1" itemProp="position"></meta>
</Link>
</li>
) : (
firstBreadcrumb.title && (
<li className={styles.breadcrumbsList} key={firstBreadcrumb.title}>
<li
className={styles.breadcrumbsList}
key={firstBreadcrumb.title}
{...LIST_ITEM_PROPS}
>
<span itemProp="name">{firstBreadcrumb.title}</span>
<meta content="1" itemProp="position"></meta>
</li>
)
)}
Expand All @@ -83,22 +97,33 @@ const Breadcrumbs: FC<BreadcrumbsProps> = ({
hiddenBreadcrumbs.map((breadcrumb, i) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to keep breadcrumbs in the DOM and hide it with css? If dom is not available in server side rendered html, it might cause crawling issues.

cc @Nazzanuk

if (breadcrumb.link && i + 1 !== breadcrumbs.length) {
return (
<li className={styles.breadcrumbsList} key={`${uuidv4()}`}>
<li
className={styles.breadcrumbsList}
key={`${uuidv4()}`}
{...LIST_ITEM_PROPS}
>
<Link
className={styles.breadcrumbLink}
itemProp="item"
link={breadcrumb.link}
>
<span itemProp="name">{breadcrumb.title}</span>
<meta content={`${i + 2}`} itemProp="position"></meta>
</Link>
</li>
);
}

return (
<li className={styles.breadcrumbsList} key={breadcrumb.title}>
<li
className={styles.breadcrumbsList}
key={breadcrumb.title}
{...LIST_ITEM_PROPS}
>
<span className={styles.disabled} itemProp="name">
{breadcrumb.title}
</span>
<meta content={`${i + 2}`} itemProp="position"></meta>
</li>
);
})}
Expand All @@ -108,20 +133,24 @@ const Breadcrumbs: FC<BreadcrumbsProps> = ({
<li
className={styles.currentBreadcrumb}
key={`${lastBreadcrumb.title}-${breadcrumbs.length - 1}`}
{...LIST_ITEM_PROPS}
>
<Link
className={cn(styles.currentBreadcrumb, styles.breadcrumbLink)}
itemProp="item"
link={lastBreadcrumb.link}
>
<span itemProp="name">{lastBreadcrumb.title}</span>
<meta content={`${breadcrumbs.length}`} itemProp="position"></meta>
</Link>
</li>
) : (
lastBreadcrumb.title && (
<li key={lastBreadcrumb.title}>
<li key={lastBreadcrumb.title} {...LIST_ITEM_PROPS}>
<span className={styles.currentBreadcrumb} itemProp="name">
{lastBreadcrumb.title}
</span>
<meta content={`${breadcrumbs.length}`} itemProp="position"></meta>
</li>
)
)}
Expand Down
1 change: 1 addition & 0 deletions packages/breadcrumbs/src/Breadcrumbs.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface BreadcrumbLinkProps {
link?: string;
className?: string;
children: React.ReactNode;
itemProp: string;
}
export interface Breadcrumb {
link?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,73 @@ Object {
<div>
<ul
class="breadcrumbWrapper"
itemscope=""
itemtype="https://schema.org/BreadcrumbList"
>
<li
class="breadcrumbsList"
itemprop="itemListElement"
itemscope=""
itemtype="https://schema.org/ListItem"
>
<a
class="breadcrumbLink"
href="#breadcrumbs"
itemprop="item"
>
<span
itemprop="name"
>
Breadcrumb Default
</span>
<meta
content="1"
itemprop="position"
/>
</a>
</li>
<li
class="breadcrumbsList"
itemprop="itemListElement"
itemscope=""
itemtype="https://schema.org/ListItem"
>
<a
class="breadcrumbLink"
href="#breadcrumb"
itemprop="item"
>
<span
itemprop="name"
>
Breadcrumb Default
</span>
<meta
content="2"
itemprop="position"
/>
</a>
</li>
<li
class="currentBreadcrumb"
itemprop="itemListElement"
itemscope=""
itemtype="https://schema.org/ListItem"
>
<a
class="currentBreadcrumb breadcrumbLink"
href="#breadcrumb"
itemprop="item"
>
<span
itemprop="name"
>
Breadcrumb Current
</span>
<meta
content="3"
itemprop="position"
/>
</a>
</li>
</ul>
Expand All @@ -56,47 +82,73 @@ Object {
"container": <div>
<ul
class="breadcrumbWrapper"
itemscope=""
itemtype="https://schema.org/BreadcrumbList"
>
<li
class="breadcrumbsList"
itemprop="itemListElement"
itemscope=""
itemtype="https://schema.org/ListItem"
>
<a
class="breadcrumbLink"
href="#breadcrumbs"
itemprop="item"
>
<span
itemprop="name"
>
Breadcrumb Default
</span>
<meta
content="1"
itemprop="position"
/>
</a>
</li>
<li
class="breadcrumbsList"
itemprop="itemListElement"
itemscope=""
itemtype="https://schema.org/ListItem"
>
<a
class="breadcrumbLink"
href="#breadcrumb"
itemprop="item"
>
<span
itemprop="name"
>
Breadcrumb Default
</span>
<meta
content="2"
itemprop="position"
/>
</a>
</li>
<li
class="currentBreadcrumb"
itemprop="itemListElement"
itemscope=""
itemtype="https://schema.org/ListItem"
>
<a
class="currentBreadcrumb breadcrumbLink"
href="#breadcrumb"
itemprop="item"
>
<span
itemprop="name"
>
Breadcrumb Current
</span>
<meta
content="3"
itemprop="position"
/>
</a>
</li>
</ul>
Expand Down