facebook / docusaurus

Easy to maintain open source documentation websites.
https://docusaurus.io
MIT License
55.54k stars 8.33k forks source link

Locale switcher has confusing behavior in dev #8652

Open start940315 opened 1 year ago

start940315 commented 1 year ago

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

After I chose a language, the locale added to the url one by one. I assumed it to be replaced. For example, my locales is ['en', 'zh-Hant', 'zh-Hans'], the initial url is http://localhost:3000/docu/docs/intro, after I chose zh-Hant then chose zh-Hans, the url became http://localhost:3000/zh-Hans/zh-Hant/docu/docs/intro. And I found currentLocale is not updated.

Reproducible demo

No response

Steps to reproduce

  1. start a new repo
  2. npm install
  3. npm run start
  4. change language
  5. the lang added one by one, making the url longer and longer

Expected behavior

language url be replaced

Actual behavior

language url added one by one

Your environment

Self-service

slorber commented 1 year ago

Please provide a repro using Docusaurus.new so I can easily inspect your setup.

It's hard to figure out with text only and it's not very clear to me what this bug is, so any screenshot is also welcome. I don't know what you mean by "language URL added one by one" for example. Please use real examples using real URLs for expected/actual results.

We'll re-open https://github.com/facebook/docusaurus/pull/8651 if the bug report is valid and if the code seems to fix it, and if possible add tests.

Josh-Cena commented 1 year ago

I can reproduce this in dev but not in prod. This is because in dev there's only one locale anyway, so useAlternatePageUtils naïvely believes that we are still in the default locale and tries to prepend the base URL instead of replacing it. I think it's better if we can simply disable the locale switcher in dev. @slorber WDYT? If you are still unsure what the bug is, it's when you are on localhost:3000 with locale en, and you use the locale dropdown to go to zh-Hans, you end up on localhost:3000/zh-Hans, which is non-existent, but the layout still exists, and it still says you are on en, so you use the locale dropdown to go to zh-Hant, and it prepends that to your URL so the URL becomes localhost:3000/zh-Hant/zh-Hans/.

slorber commented 1 year ago

I see thanks, saw that before

Maybe we could add an alert("this does not work in dev") on locale click and prevent the navigation?

That looks better than simply removing the locale dropdown in dev, which might confuse users?

Josh-Cena commented 1 year ago

When I say "disable" I mean have it show the "forbidden" icon when hovered. (We may still show the dropdown content for the purpose of preview).

slorber commented 1 year ago

Yes using forbidden + prevent selection looks like a nice idea

Code-Milker commented 1 week ago

@slorber just to clarify, we should not expect locale switching to work correctly in local environments right? When I attempt to do it locally my docs get a 404, however when I build and serve everything works as expected.

Josh-Cena commented 1 week ago

That's right; in dev we can only start the site in one locale.

Code-Milker commented 1 week ago

ah good to know, spent a couple hours troubleshooting this