Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): TypeError: Invalid URL while trying to build with sitemap not set to false #8116 #8164

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -94,7 +94,19 @@ describe('normalizeConfig', () => {
url: 1,
}),
).toThrowErrorMatchingInlineSnapshot(`
""url" contains an invalid value
""url" must be a string
"
`);
});

it('throws for URLs missing protocol', () => {
expect(() =>
normalizeConfig({
url: 'docusaurus.io',
}),
).toThrowErrorMatchingInlineSnapshot(`
"Field "url" must be a valid URL, (value='docusaurus.io'). Ensure you are not missing the protocol in the URL e.g. https://docusaurus.io.
"url" contains an invalid value
"
`);
});
Expand Down
31 changes: 20 additions & 11 deletions packages/docusaurus/src/server/configValidation.ts
Expand Up @@ -12,7 +12,7 @@ import {
addTrailingSlash,
removeTrailingSlash,
} from '@docusaurus/utils';
import {Joi, URISchema, printWarning} from '@docusaurus/utils-validation';
import {Joi, printWarning} from '@docusaurus/utils-validation';
import type {DocusaurusConfig, I18nConfig} from '@docusaurus/types';

const DEFAULT_I18N_LOCALE = 'en';
Expand Down Expand Up @@ -152,17 +152,26 @@ const I18N_CONFIG_SCHEMA = Joi.object<I18nConfig>({
.optional()
.default(DEFAULT_I18N_CONFIG);

const SiteUrlSchema = URISchema.required().custom((value: string, helpers) => {
try {
const {pathname} = new URL(String(value));
if (pathname !== '/') {
helpers.warn('docusaurus.configValidationWarning', {
warningMessage: `The url is not supposed to contain a sub-path like '${pathname}'. Please use the baseUrl field for sub-paths.`,
});
const SiteUrlSchema = Joi.string()
.uri()
.required()
.custom((value: string, helpers) => {
try {
const {pathname} = new URL(String(value));
if (pathname !== '/') {
helpers.warn('docusaurus.configValidationWarning', {
warningMessage: `The url is not supposed to contain a sub-path like '${pathname}'. Please use the baseUrl field for sub-paths.`,
});
}
} catch {
return helpers.error('any.invalid');
}
} catch {}
return removeTrailingSlash(value);
});

return removeTrailingSlash(value);
})
.messages({
'string.uri': `Field {{#label}} must be a valid URL, (value='{{#value}}'). Ensure you are not missing the protocol in the URL e.g. https://docusaurus.io.`,
});

// TODO move to @docusaurus/utils-validation
export const ConfigSchema = Joi.object<DocusaurusConfig>({
Expand Down