fix(core): TypeError: Invalid URL while trying to build with sitemap not set to false #8116 #8164
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pre-flight checklist
Motivation
The
urlfield indocusaurus.config.jscurrently accepts URLs without a preceding protocol causing the downstream sitemap builder to fail when callingnew URL()with said invalid URLs.This PR updates the Joi validation of the field to reject URLs that would cause
new URL()to error out.Implementation
The current validation schema
URISchemaused in the validation of theurlfield indocusaurus.config.jsallows relative URLs with the{allowRelative: true}config option.docusaurus/packages/docusaurus-utils-validation/src/validationSchemas.ts
Lines 67 to 68 in c6f5cf9
This lets URLs without protocols pass validation and cause errors when constructing URLs. Disabling that option would fix this but
URISchemais used in a number of other places, changing it could have unintended consequences.This PR removes the dependency on
URISchemaand directly adds Joi.url()validation to theurlfield without theallowRelativeconfig option. It also addsnew URL()construction validation as a fallback to ensure invalid URLs that aren't caught by Joi but error out onnew URL()don't make it past validation.Test Plan
A
'throws for URLs missing protocol'test is added to validate URLs like "docusaurus.io" don't pass config validation.The inline snapshot for an existing test on the same
urlfield is also updated. Providing a number to the URL field throws a"url" must be a stringerror instead of ainvalid valueerror.Test links
Deploy preview: https://deploy-preview-_____--docusaurus-2.netlify.app/
Preview of Changes
Related issues/PRs
Fixes #8116