feat(preset-classic): exclude debug plugin routes from sitemap (#7122)

This commit is contained in:
Alexey Pyltsyn 2022-04-07 13:59:47 +03:00 committed by GitHub
parent bfbc78e52a
commit 0963bff5e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 10 deletions

View file

@ -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,
@ -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: {
@ -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,
});

View file

@ -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;
}

View file

@ -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,
@ -28,7 +29,7 @@ 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 {
@ -36,12 +37,13 @@ export default function preset(
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));
@ -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) {