Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process.env.DOCUSAURUS_CURRENT_LOCALE is undefined in serve #8849

Closed
Josh-Cena opened this issue Mar 30, 2023 Discussed in #8848 · 6 comments
Closed

process.env.DOCUSAURUS_CURRENT_LOCALE is undefined in serve #8849

Josh-Cena opened this issue Mar 30, 2023 Discussed in #8848 · 6 comments
Labels
closed: working as intended This issue is intended behavior, there's no need to take any action.

Comments

@Josh-Cena
Copy link
Collaborator

Discussed in #8848

Originally posted by laojun March 30, 2023
Hi:
First of all, Thanks for the v2.4。

I use process.env.DOCUSAURUS_CURRENT_LOCALE to solve my problem in https://github.com/facebook/docusaurus/discussions/8745。

Here is my docusaurus.config.js

baseUrl: `/${process.env.DOCUSAURUS_CURRENT_LOCALE}/`

Seems right when i run build
image

But error when i run server
image

How can i make it right when i run server?

Thanks

@Josh-Cena Josh-Cena added the bug An error in the Docusaurus core causing instability or issues with its execution label Mar 30, 2023
@slorber
Copy link
Collaborator

slorber commented Apr 7, 2023

@laojun according to you, which value process.env.DOCUSAURUS_CURRENT_LOCALE should have when running the serve command?

The original PR (#8677) mentions that this env variable is a temporary workaround until we figure out a better API

And it is also explained that you have to program defensively against the value being potentially undefined.

docusaurus serve belongs to the commands we documented as having an undefined current locale env variable.

My suggestion, try something like this:

baseUrl: process.env.DOCUSAURUS_CURRENT_LOCALE
  ? `/${process.env.DOCUSAURUS_CURRENT_LOCALE}/`
  : "/"; // or "/zh-cn/" ???

Going to close because for me it's not a bug, it's a documented limitation of the current implementation.

Even if we had a better API, I have no idea which locale we should define as the "current one" when running docusaurus serve. I'm not sure we could generalize a good solution here. There are many cases to consider when serving a build output depending on what has been built (single localized site vs site with multiple locales built one after the other)

Open to discussing this more but please explain me your expectations when running this command.

@slorber slorber closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2023
@Josh-Cena
Copy link
Collaborator Author

Yeah, that makes sense :/ Multiple locales are served at once so it's hard to tell

@Josh-Cena Josh-Cena added closed: working as intended This issue is intended behavior, there's no need to take any action. and removed bug An error in the Docusaurus core causing instability or issues with its execution labels Apr 7, 2023
@laojun
Copy link

laojun commented Apr 11, 2023

@laojun according to you, which value process.env.DOCUSAURUS_CURRENT_LOCALE should have when running the serve command?

The original PR (#8677) mentions that this env variable is a temporary workaround until we figure out a better API

And it is also explained that you have to program defensively against the value being potentially undefined.

docusaurus serve belongs to the commands we documented as having an undefined current locale env variable.

My suggestion, try something like this:

baseUrl: process.env.DOCUSAURUS_CURRENT_LOCALE
  ? `/${process.env.DOCUSAURUS_CURRENT_LOCALE}/`
  : "/"; // or "/zh-cn/" ???

Going to close because for me it's not a bug, it's a documented limitation of the current implementation.

Even if we had a better API, I have no idea which locale we should define as the "current one" when running docusaurus serve. I'm not sure we could generalize a good solution here. There are many cases to consider when serving a build output depending on what has been built (single localized site vs site with multiple locales built one after the other)

Open to discussing this more but please explain me your expectations when running this command.

Thanks for the reply。

I try it like this

baseUrl: process.env.DOCUSAURUS_CURRENT_LOCALE ? `/${process.env.DOCUSAURUS_CURRENT_LOCALE}/` : '/zh-cn/',

It goes well when i choose locale zh-cn, And the url goes to /zh-cn/en-us when i chose en-us, The url should be /en-us.

@slorber
Copy link
Collaborator

slorber commented Apr 12, 2023

It goes well when i choose locale zh-cn, And the url goes to /zh-cn/en-us when i chose en-us, The url should be /en-us.

That's probably a bug in the locale dropdown. Can you open another dedicated issue and provide a repro we can look at?

@Josh-Cena
Copy link
Collaborator Author

Josh-Cena commented Apr 12, 2023

This is the same issue as #8652 I believe

This is working as intended. We don't support two locales both having base URLs—one must be the default and not have any base URL. When you choose /zh-cn/ as the site's base URL, this means zh-CN docs actually have base URL /zh-cn/ + / = /zh-cn/, and en-US has base URL /zh-cn/ + /en-us/ = /zh-cn/en-us/.

@slorber
Copy link
Collaborator

slorber commented Apr 12, 2023

I think we could fix this behavior as part of #6731

This means you could keep / for your site baseUrl, but make Docusaurus use /zh-cn/ for the default locale instead of /

/ + /zh-cn/ = /zh-cn/

Does it make sense?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: working as intended This issue is intended behavior, there's no need to take any action.
Projects
None yet
Development

No branches or pull requests

3 participants