Skip to content

Commit eac3239

Browse files
authored
feat(docs): allow to configure noIndex per doc version (facebook#7963)
1 parent a335a69 commit eac3239

File tree

12 files changed

+103
-42
lines changed

12 files changed

+103
-42
lines changed

packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,7 @@ exports[`simple website content: data 1`] = `
901901
"label": "Next",
902902
"banner": null,
903903
"badge": false,
904+
"noIndex": false,
904905
"className": "docs-version-current",
905906
"isLast": true,
906907
"docsSidebars": {
@@ -2608,6 +2609,7 @@ exports[`versioned website (community) content: data 1`] = `
26082609
"label": "1.0.0",
26092610
"banner": null,
26102611
"badge": true,
2612+
"noIndex": false,
26112613
"className": "docs-version-1.0.0",
26122614
"isLast": true,
26132615
"docsSidebars": {
@@ -2635,6 +2637,7 @@ exports[`versioned website (community) content: data 1`] = `
26352637
"label": "Next",
26362638
"banner": "unreleased",
26372639
"badge": true,
2640+
"noIndex": false,
26382641
"className": "docs-version-current",
26392642
"isLast": false,
26402643
"docsSidebars": {
@@ -3477,6 +3480,7 @@ exports[`versioned website content: data 1`] = `
34773480
"label": "1.0.0",
34783481
"banner": "unmaintained",
34793482
"badge": true,
3483+
"noIndex": false,
34803484
"className": "docs-version-1.0.0",
34813485
"isLast": false,
34823486
"docsSidebars": {
@@ -3544,6 +3548,7 @@ exports[`versioned website content: data 1`] = `
35443548
"label": "1.0.1",
35453549
"banner": null,
35463550
"badge": true,
3551+
"noIndex": true,
35473552
"className": "docs-version-1.0.1",
35483553
"isLast": true,
35493554
"docsSidebars": {
@@ -3599,6 +3604,7 @@ exports[`versioned website content: data 1`] = `
35993604
"label": "Next",
36003605
"banner": "unreleased",
36013606
"badge": true,
3607+
"noIndex": false,
36023608
"className": "docs-version-current",
36033609
"isLast": false,
36043610
"docsSidebars": {
@@ -3674,6 +3680,7 @@ exports[`versioned website content: data 1`] = `
36743680
"label": "withSlugs",
36753681
"banner": "unmaintained",
36763682
"badge": true,
3683+
"noIndex": false,
36773684
"className": "docs-version-withSlugs",
36783685
"isLast": false,
36793686
"docsSidebars": {

packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ describe('versioned website', () => {
362362
options: {
363363
routeBasePath,
364364
sidebarPath,
365+
versions: {
366+
'1.0.1': {
367+
noIndex: true,
368+
},
369+
},
365370
},
366371
});
367372
const plugin = await pluginContentDocs(context, options);

packages/docusaurus-plugin-content-docs/src/__tests__/options.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe('normalizeDocsPluginOptions', () => {
7676
version1: {
7777
path: 'hello',
7878
label: 'world',
79+
noIndex: true,
7980
},
8081
},
8182
sidebarCollapsible: false,

packages/docusaurus-plugin-content-docs/src/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const VersionOptionsSchema = Joi.object({
5959
banner: Joi.string().equal('none', 'unreleased', 'unmaintained').optional(),
6060
badge: Joi.boolean().optional(),
6161
className: Joi.string().optional(),
62+
noIndex: Joi.boolean().optional(),
6263
});
6364

6465
const VersionsOptionsSchema = Joi.object()

packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ declare module '@docusaurus/plugin-content-docs' {
125125
// TODO support custom version banner?
126126
// {type: "error", content: "html content"}
127127
export type VersionBanner = 'unreleased' | 'unmaintained';
128+
129+
export type VersionOptions = {
130+
/**
131+
* The base path of the version, will be appended to `baseUrl` +
132+
* `routeBasePath`.
133+
*/
134+
path?: string;
135+
/** The label of the version to be used in badges, dropdowns, etc. */
136+
label?: string;
137+
/** The banner to show at the top of a doc of that version. */
138+
banner?: 'none' | VersionBanner;
139+
/** Show a badge with the version label at the top of each doc. */
140+
badge?: boolean;
141+
/** Prevents search engines from indexing this version */
142+
noIndex?: boolean;
143+
/** Add a custom class name to the <html> element of each doc. */
144+
className?: string;
145+
};
146+
128147
export type VersionsOptions = {
129148
/**
130149
* The version navigated to in priority and displayed by default for docs
@@ -144,23 +163,7 @@ declare module '@docusaurus/plugin-content-docs' {
144163
/** Include the current version of your docs. */
145164
includeCurrentVersion: boolean;
146165
/** Independent customization of each version's properties. */
147-
versions: {
148-
[versionName: string]: {
149-
/**
150-
* The base path of the version, will be appended to `baseUrl` +
151-
* `routeBasePath`.
152-
*/
153-
path?: string;
154-
/** The label of the version to be used in badges, dropdowns, etc. */
155-
label?: string;
156-
/** The banner to show at the top of a doc of that version. */
157-
banner?: 'none' | VersionBanner;
158-
/** Show a badge with the version label at the top of each doc. */
159-
badge?: boolean;
160-
/** Add a custom class name to the <html> element of each doc. */
161-
className?: string;
162-
};
163-
};
166+
versions: {[versionName: string]: VersionOptions};
164167
};
165168
export type SidebarOptions = {
166169
/**
@@ -263,6 +266,8 @@ declare module '@docusaurus/plugin-content-docs' {
263266
banner: VersionBanner | null;
264267
/** Show a badge with the version label at the top of each doc. */
265268
badge: boolean;
269+
/** Prevents search engines from indexing this version */
270+
noIndex: boolean;
266271
/** Add a custom class name to the <html> element of each doc. */
267272
className: string;
268273
/**
@@ -500,7 +505,7 @@ declare module '@docusaurus/plugin-content-docs' {
500505

501506
export type PropVersionMetadata = Pick<
502507
VersionMetadata,
503-
'label' | 'banner' | 'badge' | 'className' | 'isLast'
508+
'label' | 'banner' | 'badge' | 'className' | 'isLast' | 'noIndex'
504509
> & {
505510
/** ID of the docs plugin this version belongs to. */
506511
pluginId: string;

packages/docusaurus-plugin-content-docs/src/props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export function toVersionMetadataProp(
142142
label: loadedVersion.label,
143143
banner: loadedVersion.banner,
144144
badge: loadedVersion.badge,
145+
noIndex: loadedVersion.noIndex,
145146
className: loadedVersion.className,
146147
isLast: loadedVersion.isLast,
147148
docsSidebars: toSidebarsProp(loadedVersion),

packages/docusaurus-plugin-content-docs/src/versions/__tests__/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe('readVersionsMetadata', () => {
5656
path: '/docs',
5757
banner: null,
5858
badge: false,
59+
noIndex: false,
5960
className: 'docs-version-current',
6061
};
6162
return {simpleSiteDir, defaultOptions, defaultContext, vCurrent};
@@ -218,6 +219,7 @@ describe('readVersionsMetadata', () => {
218219
path: '/docs/next',
219220
banner: 'unreleased',
220221
badge: true,
222+
noIndex: false,
221223
className: 'docs-version-current',
222224
};
223225

@@ -242,6 +244,7 @@ describe('readVersionsMetadata', () => {
242244
path: '/docs',
243245
banner: null,
244246
badge: true,
247+
noIndex: false,
245248
className: 'docs-version-1.0.1',
246249
};
247250

@@ -266,6 +269,7 @@ describe('readVersionsMetadata', () => {
266269
path: '/docs/1.0.0',
267270
banner: 'unmaintained',
268271
badge: true,
272+
noIndex: false,
269273
className: 'docs-version-1.0.0',
270274
};
271275

@@ -290,6 +294,7 @@ describe('readVersionsMetadata', () => {
290294
path: '/docs/withSlugs',
291295
banner: 'unmaintained',
292296
badge: true,
297+
noIndex: false,
293298
className: 'docs-version-withSlugs',
294299
};
295300

@@ -657,6 +662,7 @@ describe('readVersionsMetadata', () => {
657662
path: '/communityBasePath/next',
658663
banner: 'unreleased',
659664
badge: true,
665+
noIndex: false,
660666
className: 'docs-version-current',
661667
};
662668

@@ -681,6 +687,7 @@ describe('readVersionsMetadata', () => {
681687
path: '/communityBasePath',
682688
banner: null,
683689
badge: true,
690+
noIndex: false,
684691
className: 'docs-version-1.0.0',
685692
};
686693

packages/docusaurus-plugin-content-docs/src/versions/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ export function getVersionBadge({
122122
return options.versions[versionName]?.badge ?? defaultVersionBadge;
123123
}
124124

125+
export function getVersionNoIndex({
126+
versionName,
127+
options,
128+
}: VersionContext): VersionMetadata['noIndex'] {
129+
return options.versions[versionName]?.noIndex ?? false;
130+
}
131+
125132
function getVersionClassName({
126133
versionName,
127134
options,
@@ -179,6 +186,7 @@ async function createVersionMetadata(
179186
label: getVersionLabel(context),
180187
banner: getVersionBanner(context),
181188
badge: getVersionBadge(context),
189+
noIndex: getVersionNoIndex(context),
182190
className: getVersionClassName(context),
183191
path: routePath,
184192
tagsPath: normalizeUrl([routePath, options.tagsBasePath]),

packages/docusaurus-theme-classic/src/theme/DocPage/index.tsx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
import React from 'react';
99
import clsx from 'clsx';
10-
import {HtmlClassNameProvider, ThemeClassNames} from '@docusaurus/theme-common';
10+
import {
11+
HtmlClassNameProvider,
12+
ThemeClassNames,
13+
PageMetadata,
14+
} from '@docusaurus/theme-common';
1115
import {
1216
docVersionSearchTag,
1317
DocsSidebarProvider,
@@ -19,13 +23,8 @@ import NotFound from '@theme/NotFound';
1923
import SearchMetadata from '@theme/SearchMetadata';
2024
import type {Props} from '@theme/DocPage';
2125

22-
export default function DocPage(props: Props): JSX.Element {
26+
function DocPageMetadata(props: Props): JSX.Element {
2327
const {versionMetadata} = props;
24-
const currentDocRouteMetadata = useDocRouteMetadata(props);
25-
if (!currentDocRouteMetadata) {
26-
return <NotFound />;
27-
}
28-
const {docElement, sidebarName, sidebarItems} = currentDocRouteMetadata;
2928
return (
3029
<>
3130
<SearchMetadata
@@ -35,6 +34,25 @@ export default function DocPage(props: Props): JSX.Element {
3534
versionMetadata.version,
3635
)}
3736
/>
37+
<PageMetadata>
38+
{versionMetadata.noIndex && (
39+
<meta name="robots" content="noindex, nofollow" />
40+
)}
41+
</PageMetadata>
42+
</>
43+
);
44+
}
45+
46+
export default function DocPage(props: Props): JSX.Element {
47+
const {versionMetadata} = props;
48+
const currentDocRouteMetadata = useDocRouteMetadata(props);
49+
if (!currentDocRouteMetadata) {
50+
return <NotFound />;
51+
}
52+
const {docElement, sidebarName, sidebarItems} = currentDocRouteMetadata;
53+
return (
54+
<>
55+
<DocPageMetadata {...props} />
3856
<HtmlClassNameProvider
3957
className={clsx(
4058
// TODO: it should be removed from here

website/_dogfooding/dogfooding.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ const dogfoodingPluginInstances = [
2626
id: 'docs-tests',
2727
routeBasePath: '/tests/docs',
2828
sidebarPath: '_dogfooding/docs-tests-sidebars.js',
29+
versions: {
30+
current: {
31+
noIndex: true,
32+
},
33+
},
2934

3035
// Using a _ prefix to test against an edge case regarding MDX partials: https://github.com/facebook/docusaurus/discussions/5181#discussioncomment-1018079
3136
path: '_dogfooding/_docs tests',

0 commit comments

Comments
 (0)