vite icon indicating copy to clipboard operation
vite copied to clipboard

`serve`: with `base: '/docs/'`, should `/docs` work as `/docs/`?

Open JounQin opened this issue 2 years ago • 12 comments

Describe the bug

When run with vitepress dev docs --base /docs/ and visit /docs route, it will be response with a message:

The server is configured with a public base URL of /docs/ - did you mean to visit [/docs/docs](http://localhost:3000/docs/docs) instead?

I would expect /docs render same as /docs/ or just redirect to /docs/ instead.

Related https://github.com/vuejs/vitepress/issues/852

Reproduction

https://github.com/JounQin/test/tree/vitepress

System Info

System:
    OS: macOS 13.0
    CPU: (10) arm64 Apple M1 Max
    Memory: 20.53 GB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.1 - ~/Library/Caches/fnm_multishells/50102_1656090482477/bin/node
    Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/50102_1656090482477/bin/yarn
    npm: 8.13.1 - ~/Library/Caches/fnm_multishells/50102_1656090482477/bin/npm
  Browsers:
    Chrome: 103.0.5060.53
    Firefox: 101.0.1
    Safari: 16.0
  npmPackages:
    @vitejs/plugin-react: ^1.3.2 => 1.3.2 
    vite: ^2.9.12 => 2.9.12

Used Package Manager

yarn

Logs

N/A

Validations

JounQin avatar Jun 24 '22 17:06 JounQin

I think this is expected. /docs is outside of /docs/.

That said, IMHO changing the message to below would be friendly.

did you mean to visit [/docs/docs](http://localhost:3000/docs/docs) or [/docs/](http://localhost:3000/docs/) instead?

sapphi-red avatar Jul 03 '22 15:07 sapphi-red

We now consider `` same as /, what means http://localhost:3000 is exactly same as http://localhost:3000/, the consistent behavior should be http://localhost:3000/docs vshttp://localhost:3000/docs/.

JounQin avatar Jul 03 '22 15:07 JounQin

In URL spec, http://localhost:3000 is equivalent to http://localhost:3000/, but http://localhost:3000/docs is not equivalent to http://localhost:3000/docs/.

new URL('http://localhost:3000').href === new URL('http://localhost:3000/').href // true
new URL('http://localhost:3000/docs').href === new URL('http://localhost:3000/docs/').href // false

https://url.spec.whatwg.org/#url-equivalence

Vite is not doing something special here.

sapphi-red avatar Jul 03 '22 16:07 sapphi-red

As a user it is very common to visit /docs instead of /docs/ just like `` vs /, and the current behavior is meaningless for end user because there could never be a /docs page? It's a UX issue.

JounQin avatar Jul 03 '22 16:07 JounQin

It is a server configuration issue, if you want to have this behavior, add a server middleware to do a redirection or forwarding, maybe we can add an option to enable it in the dev server and also in the preview.

I configure my production servers when using some context/scope instead root.

userquin avatar Jul 03 '22 16:07 userquin

I understand that but it is confusing if /docs returns the content of /docs/. I think it is ok to change the message.

Maybe it is not confusing if the redirect works after some seconds like <meta http-equiv="refresh" content="3;url=http://localhost:3000/docs/" />.

sapphi-red avatar Jul 03 '22 16:07 sapphi-red

It is a server configuration issue

Right, that's why it should 'fixed' in serve command.

JounQin avatar Jul 03 '22 16:07 JounQin

I understand that but it is confusing if /docs returns the content of /docs/.

My fix is just doing a redirect from /docs to /docs/, it should not be confusing?

Maybe it is not confusing if the redirect works after some seconds like <meta http-equiv="refresh" content="3;url=http://localhost:3000/docs/" />.

It's still meaningless for an end user to understand the difference between /docs/ and /docs? Why should they care about that?

JounQin avatar Jul 03 '22 16:07 JounQin

maybe we can add an option to enable it in the dev server and also in the preview

I believe that option is not very helpful? When would an end user expect /docs results different from /docs/ when there can never be a /docs page?

JounQin avatar Jul 03 '22 16:07 JounQin

It's still meaningless for an end user to understand the difference between /docs/ and /docs? Why should they care about that?

I think it is not completely meaningless but I agree that a user does not need to care about it usually.

sapphi-red avatar Jul 03 '22 17:07 sapphi-red

@sapphi-red I've just rebased #8772, please help to review, it should fix serve and preview at the same time.

JounQin avatar Jul 03 '22 17:07 JounQin

I also encountered the same problem, after changing 'base', many links still can't be added...

jiesir avatar Jul 22 '22 02:07 jiesir