Skip to content

Commit

Permalink
Merge pull request #12 from americanexpress/feature/canonical
Browse files Browse the repository at this point in the history
feat(canonical): add custom canonical url
  • Loading branch information
Jonathan Golden authored May 5, 2020
2 parents e93f16f + e232103 commit 8503653
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ SEO.propTypes = {
pathname: PropTypes.string,
siteUrl: PropTypes.string,
title: PropTypes.string,
canonical: PropTypes.string,
};

SEO.defaultProps = {
Expand All @@ -78,6 +79,7 @@ SEO.defaultProps = {
pathname: '',
siteUrl: '',
title: '',
canonical: '',
};
```

Expand Down
9 changes: 8 additions & 1 deletion __tests__/__snapshots__/index.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,14 @@ exports[`SEO should render the snapshot correctly 1`] = `
"lang": "en-US",
}
}
link={Array []}
link={
Array [
Object {
"href": "https://example.com/foo/bar",
"rel": "canonical",
},
]
}
meta={
Array [
Object {
Expand Down
1 change: 1 addition & 0 deletions __tests__/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('SEO', () => {
keywords={['foo', 'bar']}
siteUrl="https://example.com"
title="Lorem Ipsum"
canonical="https://example.com/foo/bar"
/>
);
expect(component).toMatchSnapshot();
Expand Down
5 changes: 4 additions & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ const SEO = ({
pathname,
siteUrl,
title,
canonical: canonicalTag,
}) => {
const image = metaImage && metaImage.src ? `${siteUrl}${metaImage.src}` : null;

const canonical = pathname ? `${siteUrl}${pathname}` : null;
const canonical = canonicalTag || (pathname ? `${siteUrl}${pathname}` : null);

const link = canonical ? [{ rel: 'canonical', href: canonical }] : [];

Expand Down Expand Up @@ -126,6 +127,7 @@ SEO.propTypes = {
pathname: PropTypes.string,
siteUrl: PropTypes.string,
title: PropTypes.string,
canonical: PropTypes.string,
};

SEO.defaultProps = {
Expand All @@ -140,6 +142,7 @@ SEO.defaultProps = {
pathname: '',
siteUrl: '',
title: '',
canonical: '',
};

export default SEO;

0 comments on commit 8503653

Please sign in to comment.