fix: serve directory index when path segment contains a dot (e.g. /docs/1.4)#230
Open
Ethan-Arrowood wants to merge 3 commits intovercel:mainfrom
Open
fix: serve directory index when path segment contains a dot (e.g. /docs/1.4)#230Ethan-Arrowood wants to merge 3 commits intovercel:mainfrom
Ethan-Arrowood wants to merge 3 commits intovercel:mainfrom
Conversation
path.extname() returns a non-empty string for segments like '4.6', causing serve-handler to treat them as file requests. If the early lstat reveals a directory, clear stats so findRelated can fall back to index.html or the clean-URL .html sibling. Fixes: paths like /docs/4.6 returning 404 when directoryListing is disabled and cleanUrls is enabled.
- Rename 4.6/ fixture to 1.4/ (more generic versioning example) - Add 1.2.html to cover cleanUrls serving a .html file whose name contains a dot (requested as /1.2) - Add 1.3/ + 1.3.html to cover the case where both a dotted directory and a same-named .html sibling exist; directory index.html wins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
I ran into this while using the Docusaurus
servecommand (which usesserve-handlerunder the hood) to preview a built Docusaurus site. The site has versioned docs at URLs like/docs/1.4, where1.4is a directory on disk (build/docs/1.4/index.html) — not a file with an extension.path.extname('/docs/1.4')returns'.4'(non-empty), soserve-handlertreats the path as a file request and runs the earlylstatoptimization. Thatlstatcall succeeds — becausebuild/docs/1.4is a real directory. Sincestatsis now set, theif (!stats && (cleanUrl || rewrittenPath))block is skipped, andfindRelatednever runs to look upindex.htmlinside the directory. WithdirectoryListing: false,renderDirectoryreturns empty,statsis cleared, and the response is a 404 — even thoughbuild/docs/1.4/index.htmlexists.Concretely: a build output like this:
returns 404 when served with:
{ "cleanUrls": true, "directoryListing": false, "trailingSlash": false }Fix
After the early
lstat, if the resolved path turns out to be a directory, clearstatssofindRelatedruns and can fall back toindex.html(or a.htmlsibling):Affected configuration
Reproduces when all of the following are true:
cleanUrls: truedirectoryListing: falsetrailingSlash: false/docs/1.4,/api/v1.0)Tests
Added fixtures and four new test cases covering the affected scenarios:
GET /1.41.4/index.htmlGET /1.4/1.4/index.html/1.4(trailing-slash redirect unaffected)GET /1.21.2.html.htmlfile served via cleanUrlsGET /1.31.3/index.html+1.3.html.htmlsibling