Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix TypeError when outputDir doesn't contain docPath #1234

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

Open
kkman021 wants to merge 1 commit into PaloAltoNetworks:main
base: main
Choose a base branch
Loading
from kkman021:main

Conversation

@kkman021
Copy link

@kkman021 kkman021 commented Oct 15, 2025

Fix undefined reference error in basePath calculation when outputDir.split(docPath)[1] returns undefined. This
occurs when using multiple docs plugin instances with mismatched path configurations.

Add fallback to empty string to prevent .replace() call on undefined value.

Description

This PR fixes a TypeError that occurs during sidebar generation when outputDir.split(docPath)[1] returns
undefined. The fix adds a fallback to an empty string using the logical OR operator (|| ""), preventing
the subsequent .replace() call from being invoked on undefined.

Changed Line (index.ts:129):

// Before
const basePath = docPath
 ? outputDir.split(docPath!)[1].replace(/^\/+/g, "")
 : outputDir.slice(outputDir.indexOf("/", 1)).replace(/^\/+/g, "");
// After
const basePath = docPath
 ? (outputDir.split(docPath!)[1] || "").replace(/^\/+/g, "")
 : outputDir.slice(outputDir.indexOf("/", 1)).replace(/^\/+/g, "");
Motivation and Context
When configuring multiple @docusaurus/plugin-content-docs instances with different paths alongside
docusaurus-plugin-openapi-docs, the plugin crashes with:
TypeError: Cannot read properties of undefined (reading 'replace')
 at groupByTags (node_modules/docusaurus-plugin-openapi-docs/lib/sidebars/index.js:85:38)
This occurs because outputDir.split(docPath)[1] returns undefined when the outputDir path doesn't contain
docPath as expected. The code then attempts to call .replace() on undefined, causing the error.
Example configuration that triggers the bug:
plugins: [
 [
 '@docusaurus/plugin-content-docs',
 {
 id: 'WEDAApi',
 path: 'Weda/Api',
 routeBasePath: 'apis/weda',
 // ...
 },
 ],
 [
 'docusaurus-plugin-openapi-docs',
 {
 config: {
 weda: {
 specPath: "src/api/weda.yml",
 outputDir: "Weda/Api",
 sidebarOptions: {
 groupPathsBy: "tag",
 },
 }
 }
 }
 ],
]
The fix allows the plugin to gracefully handle mismatched path configurations by defaulting to an empty string
 when the split operation fails.
How Has This Been Tested?
Testing Environment:
- Node.js: v22.14.0
- Docusaurus: 3.9.1
- docusaurus-plugin-openapi-docs: 4.5.1
- OS: Windows 11
Test Steps:
1. Configured multiple docs plugin instances with different paths
2. Set up OpenAPI docs plugin with sidebarOptions.groupPathsBy: "tag"
3. Ran npx docusaurus gen-api-docs weda
4. Verified successful sidebar generation without errors
5. Started dev server with npm start
6. Confirmed API documentation displays correctly with proper tag-based categorization
Test Results:
-  Sidebar generation completes without TypeError
-  Tag-based sidebar structure generated correctly
-  All API endpoints accessible and properly categorized
-  Development server starts without errors
Before Fix: Command failed with TypeError: Cannot read properties of undefined (reading 'replace')
After Fix: All commands execute successfully, sidebar generated with 90+ API endpoints properly organized by
tags.
Screenshots (if appropriate)
N/A - This is a bug fix with no visual changes to the UI.
Types of changes
- Bug fix (non-breaking change which fixes an issue)

Fix undefined reference error in basePath calculation when
outputDir.split(docPath)[1] returns undefined. This occurs
when using multiple docs plugin instances with mismatched
path configurations.
Add fallback to empty string to prevent .replace() call on
undefined value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /