docusaurus: The serve command does not respect baseUrl

🐛 Bug Report

If a site config has baseUrl set to anything but '/' such as when a site is meant for GitHub, the docusaurus serve command won’t work since build will add the baseUrl to links, but serve does not respond to it.

Have you read the Contributing Guidelines on issues?

Yes

To Reproduce

  1. Pick any project meant for deployment as a GitHub page that has a baseUrl set to some folder other than root, for example: baseUrl: '/docusaurus/'
  2. Check your package.json scripts to ensure you have the script entry "serve": "docusaurus serve".
  3. Run npm run serve -- --build. This will ensure you are building a fresh copy on every serve.
  4. Open your browser in the URL shown on the console by the previous command

Expected behavior

Site should open as it would when deployed

To see what should be expected,

  1. change the site config baseUrl to baseUrl: '/'
  2. run npm run serve -- --build.

Actual Behavior

The html for the initial page is displayed (or any page if you use the full address of the page) but all resources linked to from that page fail as they contain the baseUrl path. No CSS, images or scripts. Links don’t work.

Your Environment

From package.json:

  "dependencies": {
    "@docusaurus/core": "^2.0.0-alpha.61",
    "@docusaurus/preset-classic": "2.0.0-alpha.59",
    "clsx": "^1.1.1",
    "react": "^16.10.2",
    "react-dom": "^16.10.2"
  }

Chrome: Version 84.0.4147.125 (Official Build) (64-bit) (browser is not particularly relevant since you can see the buildUrl value inserted in the index.html anyway) nodeJS: v14.7.0 Ubuntu 20.04.1 LTS

Reproducible Demo

It is easier to pick any project the tester is familiar with than going through the trouble of setting up another one. Either add a subfolder to baseUrl to a project that doesn’t have one, or use one that does have it set.

Suggested fix:

Ensure serve command reads the baseUrl setting and strip it off the requests it gets.

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 20 (6 by maintainers)

Most upvoted comments

@slorber , as an interim solution it works. My npm script now has

"serve": "docusaurus build --out-dir build/myDocs && docusaurus serve"

Just an additional warning: when it finishes the serve command announces that you can check the site at - Local: http://localhost:3000, which is misleading and confusing since it usually picks some build left over by some previous build that doesn’t work with serve. The actual address to be used is - Local: http://localhost:3000/myDocs

 rewrites: [
        {source: `${props.baseUrl}:path`, destination: '/:path'},
      ],