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

Setting mode: "production" in programmatic build() doesn’t set NODE_ENV #9574

Closed
1 task done
KonnorRogers opened this issue Dec 20, 2023 · 5 comments · Fixed by #9575
Closed
1 task done

Setting mode: "production" in programmatic build() doesn’t set NODE_ENV #9574

KonnorRogers opened this issue Dec 20, 2023 · 5 comments · Fixed by #9575
Labels
needs triage Issue needs to be triaged

Comments

@KonnorRogers
Copy link

What version of starlight are you using?

0.15.0

What version of astro are you using?

4.0.6

What package manager are you using?

npm

What operating system are you using?

mac

What browser are you using?

Firefox

Describe the Bug

When using programmatic JS APIs, setting mode: "production", still requires setting: process.env.NODE_ENV = "production" to enable pagefind.

Example:

   import { build } from "astro"
   // Pagefind doesn't work
    await build({
      ...config,
      mode: "production"
    })
   
    // Enables pagefind
    process.env.NODE_ENV = "production"
    await build({
      ...config,
      mode: "production"
    })

I'm not sure if this is expected behavior, or if its a bug.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-a6bay1?file=README.md

Participation

  • I am willing to submit a pull request for this issue.
@HiDeoo
Copy link
Member

HiDeoo commented Dec 20, 2023

Thanks for your report 🙌

From a quick test, it looks like Pagefind is running at the end of the build as expected although the search feature is definitely not accessible.

This is due to this check and this one relying on import.meta.env.DEV which would be set to true with the following code:

import { build } from 'astro';

await build({
	root: './',
	// Setting `mode` will not change the value of `import.meta.env.DEV`
});

We could easily switch to import.meta.env.MODE === 'development' which should fix the issue altho I wonder if the API behaves in the way we expect it to? When running astro build, import.meta.env.DEV is false without explicitly setting anything so I guess the same could be expected when running the API version considering the docs states:

Similar to astro build

@KonnorRogers
Copy link
Author

@HiDeoo yea I found the Search.astro component and was mostly looking for clarification if this is intended behavior or not. But it is interested it seems to set the ENV properly with astro build, but not the programmatic API. It's small, easily fixed, was mostly just looking for clarification

@delucis
Copy link
Member

delucis commented Jan 2, 2024

Thanks for the issue @KonnorRogers! I think this is a bug in the JS API not setting the same environment as its CLI equivalent. Let me check with the astro team to see if they’d intend to fix it — if not we can patch it here as @HiDeoo suggested.

@delucis delucis transferred this issue from withastro/starlight Jan 2, 2024
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jan 2, 2024
@delucis delucis changed the title Setting mode: "production" does not enable pagefind search Setting mode: "production" in programmatic build() doesn’t set NODE_ENV Jan 2, 2024
@delucis
Copy link
Member

delucis commented Jan 2, 2024

Chatted with @bluwy and confirmed we can fix this upstream in the main Astro repo. Thanks again for reporting @KonnorRogers!

@KonnorRogers
Copy link
Author

❤️ thank you all! Astro rocks 🤘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants