Skip to content

fix(core): do not append trailing slash to static asset URLs in serve command (#12476) - #12737

Open
404kernel wants to merge 1 commit into
meta:mainfrom
404kernel:fix/serve-asset-trailing-slash-12476
Open

404kernel wants to merge 1 commit into
meta:mainfrom
404kernel:fix/serve-asset-trailing-slash-12476

Conversation

@404kernel

Copy link
Copy Markdown

Motivation

Fixes #12476.

When trailingSlash: true and baseUrl !== '/', running docusaurus serve returned 404 for certain static assets.
Specifically:

  1. looksLikeAsset = !!req.url.match(/\.[a-z\d]{1,4}$/i); failed on longer extensions such as .woff2 (5 characters) or .webmanifest (11 characters).
  2. Any asset URL containing query parameters or hash fragments (e.g. /assets/styles.css?v=1.0) failed the regex match.
  3. When looksLikeAsset returned false, applyTrailingSlash appended a trailing slash (e.g. /myBase/font.woff2/), causing serve-handler to attempt resolving index files and returning 404.

Solution

  • Replaced the character-length regex check with a robust isAssetUrl(url, options) helper:
    • Strips query parameters and hash fragments before evaluation.
    • Matches standard file extensions without arbitrary length limits.
    • Verifies whether the target path matches a directory or file in outDir when available.
  • Added comprehensive unit tests in packages/docusaurus/src/commands/__tests__/serve.test.ts covering root baseUrl, custom baseUrls, font assets (.woff2), manifest assets, and query strings.

Test Plan

  • Ran vitest packages/docusaurus/src/commands/__tests__/serve.test.ts (all 7 tests passing).
  • Verified formatting with oxfmt.

@meta-cla

meta-cla Bot commented Oct 18, 2026

Copy link
Copy Markdown

Hi @404kernel!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@netlify

netlify Bot commented Oct 18, 2026

Copy link
Copy Markdown

✅ [V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit cdb3d13
🔍 Latest deploy log https://app.netlify.com/projects/docusaurus-2/deploys/6ad4abc55e40321a4e9053f4
😎 Deploy Preview https://deploy-preview-12737--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@meta-cla meta-cla Bot added the CLA Signed Signed Facebook CLA label Oct 18, 2026
@meta-cla

meta-cla Bot commented Oct 18, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docusaurus serve: static assets 404 when baseUrl is not "/" and trailingSlash is true

1 participant