Skip to content

Commit

Permalink
feat(seo): add robots support
Browse files Browse the repository at this point in the history
This change adds support for the robots meta tag
by allowing robots directives to be passed as an array of strings
  • Loading branch information
Jonathan Golden committed Aug 13, 2020
1 parent 5eeb2f7 commit d4de1e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const MyModule = () => (
<SEO
title="Lorem Ipsum"
description="Lorem ipsum sat delor."
robots={['index', 'follow']}
keywords={['foo', 'bar']}
siteUrl="http://example.com"
openGraph={{
Expand Down Expand Up @@ -201,6 +202,7 @@ SEO.propTypes = {
}),
}),
keywords: arrayOf(string),
robots: arrayOf(string),
locale: string,
meta: arrayOf(object),
siteUrl: string,
Expand All @@ -212,6 +214,7 @@ SEO.defaultProps = {
description: '',
image: null,
keywords: [],
robots: [],
locale: 'en-US',
meta: [],
pathname: '',
Expand Down
22 changes: 22 additions & 0 deletions __tests__/components/SEO.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ describe('SEO', () => {
}]);
});

it('should provide the robots', () => {
const component = shallow(
<SEO
description="Lorem ipsum sat delor."
keywords={['foo', 'bar']}
robots={['index', 'follow']}
siteUrl="https://example.com"
title="Lorem Ipsum"
canonical="https://example.com/index.html"
/>
);

const helmet = component.find('Helmet');
const { meta } = helmet.props();

const robots = meta.find((tag) => tag.name === 'robots');

expect(robots).toMatchObject({
name: 'robots', content: 'index,follow',
});
});

it('should render image tags correctly', () => {
const component = shallow(
<SEO
Expand Down
7 changes: 7 additions & 0 deletions src/components/SEO.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const SEO = ({
titleTemplate,
canonical,
keywords,
robots,
openGraph,
twitterCard,
image,
Expand Down Expand Up @@ -66,6 +67,10 @@ const SEO = ({
meta.push({ name: 'keywords', content: keywords.join(',') });
}

if (robots.length > 0) {
meta.push({ name: 'robots', content: robots.join(',') });
}

return (
<Helmet
htmlAttributes={{
Expand All @@ -90,6 +95,7 @@ SEO.propTypes = {
openGraph: openGraphShape,
twitterCard: twitterCardShape,
keywords: PropTypes.arrayOf(PropTypes.string),
robots: PropTypes.arrayOf(PropTypes.string),
image: PropTypes.shape({
url: PropTypes.string,
secureUrl: PropTypes.string,
Expand All @@ -114,6 +120,7 @@ SEO.defaultProps = {
titleTemplate: '',
canonical: '',
keywords: [],
robots: [],
image: undefined,
video: undefined,
openGraph: undefined,
Expand Down

0 comments on commit d4de1e6

Please sign in to comment.