diff --git a/cypress/e2e/norobots.spec.js b/cypress/e2e/norobots.spec.js new file mode 100644 index 00000000..5814effc --- /dev/null +++ b/cypress/e2e/norobots.spec.js @@ -0,0 +1,38 @@ +describe('SEO Meta No Robots', () => { + it('App loads', () => { + cy.visit('http://localhost:3000'); + cy.get('h1').should('contain', 'Default SEO'); + }); + + it('SEO norobots', () => { + cy.visit('http://localhost:3000/norobots'); + cy.get('head meta[name="robots"]').should('not.exist'); + }); + + it('SEO overrides norobots with nofollow correctly', () => { + cy.visit('http://localhost:3000/norobots/nofollow'); + cy.get('head meta[name="robots"]').should( + 'have.attr', + 'content', + 'index,nofollow', + ); + }); + + it('SEO overrides norobots with nofollow correctly', () => { + cy.visit('http://localhost:3000/norobots/noindex'); + cy.get('head meta[name="robots"]').should( + 'have.attr', + 'content', + 'noindex,follow', + ); + }); + + it('SEO overrides norobots with robots props correctly', () => { + cy.visit('http://localhost:3000/norobots/robots'); + cy.get('head meta[name="robots"]').should( + 'have.attr', + 'content', + 'index,follow,nosnippet,max-snippet:-1,max-image-preview:none,noarchive,noimageindex,max-video-preview:-1,notranslate', + ); + }); +}); diff --git a/e2e/pages/_app.tsx b/e2e/pages/_app.tsx index 1aeec902..234e722a 100644 --- a/e2e/pages/_app.tsx +++ b/e2e/pages/_app.tsx @@ -16,6 +16,7 @@ function MyApp({ Component, pageProps, router }: AppProps) { router.pathname === '/dangerously/noindex' || router.pathname === '/dangerously/nofollow-and-noindex' } + norobots={router.pathname.startsWith('/norobots')} /> diff --git a/e2e/pages/norobots/index.tsx b/e2e/pages/norobots/index.tsx new file mode 100644 index 00000000..678bdc32 --- /dev/null +++ b/e2e/pages/norobots/index.tsx @@ -0,0 +1,12 @@ +import React from 'react'; +import { NextSeo } from '../../..'; +import Links from '../../components/links'; + +const NoRobots = () => ( + <> + +

norobots

+ + +); +export default NoRobots; diff --git a/e2e/pages/norobots/nofollow.tsx b/e2e/pages/norobots/nofollow.tsx new file mode 100644 index 00000000..a9861884 --- /dev/null +++ b/e2e/pages/norobots/nofollow.tsx @@ -0,0 +1,12 @@ +import React from 'react'; +import { NextSeo } from '../../..'; +import Links from '../../components/links'; + +const NoRobotsNoFollow = () => ( + <> + +

norobots and nofollow

+ + +); +export default NoRobotsNoFollow; diff --git a/e2e/pages/norobots/noindex.tsx b/e2e/pages/norobots/noindex.tsx new file mode 100644 index 00000000..020d2db8 --- /dev/null +++ b/e2e/pages/norobots/noindex.tsx @@ -0,0 +1,12 @@ +import React from 'react'; +import { NextSeo } from '../../..'; +import Links from '../../components/links'; + +const NoRobotsNoIndex = () => ( + <> + +

norobots and noindex

+ + +); +export default NoRobotsNoIndex; diff --git a/e2e/pages/norobots/robots.tsx b/e2e/pages/norobots/robots.tsx new file mode 100644 index 00000000..7c8798a0 --- /dev/null +++ b/e2e/pages/norobots/robots.tsx @@ -0,0 +1,24 @@ +import React from 'react'; +import { NextSeo } from '../../..'; +import Links from '../../components/links'; + +const Robots = () => ( + <> + +

Norobots with Robots meta properties

+ + +); + +export default Robots; diff --git a/src/meta/buildTags.tsx b/src/meta/buildTags.tsx index 0c395a20..32e07b61 100644 --- a/src/meta/buildTags.tsx +++ b/src/meta/buildTags.tsx @@ -4,6 +4,7 @@ const defaults = { templateTitle: '', noindex: false, nofollow: false, + norobots: false, defaultOpenGraphImageWidth: 0, defaultOpenGraphImageHeight: 0, defaultOpenGraphVideoWidth: 0, @@ -128,7 +129,10 @@ const buildTags = (config: BuildTagsParams) => { ? defaults.nofollow || config.dangerouslySetAllPagesToNoFollow : config.nofollow; + const norobots = config.norobots || defaults.norobots; + let robotsParams = ''; + if (config.robotsProps) { const { nosnippet, @@ -152,6 +156,10 @@ const buildTags = (config: BuildTagsParams) => { }`; } + if (config.norobots) { + defaults.norobots = true; + } + if (noindex || nofollow) { if (config.dangerouslySetAllPagesToNoIndex) { defaults.noindex = true; @@ -169,7 +177,7 @@ const buildTags = (config: BuildTagsParams) => { }${robotsParams}`} />, ); - } else { + } else if (!norobots || robotsParams) { tagsToRender.push( { return ( ); }; diff --git a/src/types.ts b/src/types.ts index 90abbbc6..dbfe2ab6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -498,6 +498,7 @@ export interface DefaultSeoProps { titleTemplate?: string; themeColor?: string; defaultTitle?: string; + norobots?: boolean; robotsProps?: AdditionalRobotsProps; description?: string; canonical?: string;