Skip to content

Commit

Permalink
feat(preset-classic): exclude debug plugin routes from sitemap (#7122)
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 authored Apr 7, 2022
1 parent bfbc78e commit 0963bff
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
16 changes: 9 additions & 7 deletions packages/docusaurus-plugin-debug/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {LoadContext, Plugin} from '@docusaurus/types';
import {docuHash, normalizeUrl, posixPath} from '@docusaurus/utils';
import path from 'path';

export const routeBasePath = '__docusaurus/debug';

export default function pluginDebug({
siteConfig: {baseUrl},
generatedFilesDir,
Expand Down Expand Up @@ -40,37 +42,37 @@ export default function pluginDebug({

// Home is config (duplicate for now)
addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug']),
path: normalizeUrl([baseUrl, routeBasePath]),
component: '@theme/DebugConfig',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/config']),
path: normalizeUrl([baseUrl, routeBasePath, 'config']),
component: '@theme/DebugConfig',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/metadata']),
path: normalizeUrl([baseUrl, routeBasePath, 'metadata']),
component: '@theme/DebugSiteMetadata',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/registry']),
path: normalizeUrl([baseUrl, routeBasePath, 'registry']),
component: '@theme/DebugRegistry',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/routes']),
path: normalizeUrl([baseUrl, routeBasePath, 'routes']),
component: '@theme/DebugRoutes',
exact: true,
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/content']),
path: normalizeUrl([baseUrl, routeBasePath, 'content']),
component: '@theme/DebugContent',
exact: true,
modules: {
Expand All @@ -79,7 +81,7 @@ export default function pluginDebug({
});

addRoute({
path: normalizeUrl([baseUrl, '__docusaurus/debug/globalData']),
path: normalizeUrl([baseUrl, routeBasePath, 'globalData']),
component: '@theme/DebugGlobalData',
exact: true,
});
Expand Down
4 changes: 4 additions & 0 deletions packages/docusaurus-plugin-debug/src/plugin-debug.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

/// <reference types="@docusaurus/module-type-aliases" />

declare module '@docusaurus/plugin-debug' {
export const routeBasePath: string;
}

declare module '@theme/DebugConfig' {
export default function DebugMetadata(): JSX.Element;
}
Expand Down
12 changes: 9 additions & 3 deletions packages/docusaurus-preset-classic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {routeBasePath as debugPluginRouteBasePath} from '@docusaurus/plugin-debug';
import type {
Preset,
LoadContext,
Expand All @@ -28,20 +29,21 @@ export default function preset(
opts: Options = {},
): Preset {
const {siteConfig} = context;
const {themeConfig} = siteConfig;
const {themeConfig, baseUrl} = siteConfig;
const {algolia} = themeConfig as Partial<ThemeConfig>;
const isProd = process.env.NODE_ENV === 'production';
const {
debug,
docs,
blog,
pages,
sitemap,
sitemap = {},
theme,
googleAnalytics,
gtag,
...rest
} = opts;
const isDebugEnabled = debug || (debug === undefined && !isProd);

const themes: PluginConfig[] = [];
themes.push(makePluginConfig('@docusaurus/theme-classic', theme));
Expand Down Expand Up @@ -74,13 +76,17 @@ export default function preset(
makePluginConfig('@docusaurus/plugin-google-analytics', googleAnalytics),
);
}
if (debug || (debug === undefined && !isProd)) {
if (isDebugEnabled) {
plugins.push(require.resolve('@docusaurus/plugin-debug'));
}
if (gtag) {
plugins.push(makePluginConfig('@docusaurus/plugin-google-gtag', gtag));
}
if (isProd && sitemap !== false) {
if (isDebugEnabled) {
sitemap.ignorePatterns ??= [];
sitemap.ignorePatterns.push(`${baseUrl}${debugPluginRouteBasePath}/**`);
}
plugins.push(makePluginConfig('@docusaurus/plugin-sitemap', sitemap));
}
if (Object.keys(rest).length > 0) {
Expand Down

0 comments on commit 0963bff

Please sign in to comment.