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

Wrong envPrefix in Astro Integration #689

Closed
Eptagone opened this issue Mar 21, 2024 · 7 comments
Closed

Wrong envPrefix in Astro Integration #689

Eptagone opened this issue Mar 21, 2024 · 7 comments

Comments

@Eptagone
Copy link

Hi, thank you very much for this library.

I'm using the Astro Integration and I discovered that all my PUBLIC_* variables are been ignored after build in my custom service worker.
I tested with the following variables:

PUBLIC_SAMPLE=ASTRO
VITE_SAMPLE=VITE

I saw that the VITE_SAMPLE variable is included but not PUBLIC_SAMPLE.

The default value of envPrefix in Vite is VITE_ but in Astro the prefix is PUBLIC_ for client scripts.

Is there a way to change that?

@userquin
Copy link
Member

userquin commented Mar 21, 2024

Only variables starting with VITE_ will be exposed by vite https://vitejs.dev/guide/env-and-mode.html#env-variables-and-modes

To prevent accidentally leaking env variables to the client, only variables prefixed with VITE_ are exposed to your Vite-processed code. e.g. for the following env variables:

VITE_SOME_KEY=123
DB_PASSWORD=foobar

Only VITE_SOME_KEY will be exposed as import.meta.env.VITE_SOME_KEY to your client source code, but DB_PASSWORD will not.

@Eptagone
Copy link
Author

Eptagone commented Mar 21, 2024

That prefix can be customized with the envPrefix option.

https://vitejs.dev/config/shared-options.html#envprefix

@userquin
Copy link
Member

Yeah, you can change it, but only those env vars starting with the prefix will be available via import.meta.env.

@userquin
Copy link
Member

userquin commented Mar 21, 2024

You can configure empty prefix (vite will thow an error), you will need to use define

@userquin
Copy link
Member

userquin commented Mar 21, 2024

Read the SECURITY NOTES from your VITE_ link, you can also use define.

@Eptagone
Copy link
Author

Yeah, you can change it, but only those env vars starting with the prefix will be available via import.meta.env.

Yes, that's what I want. I want to make VitePWA to use the same prefix as Astro: PUBLIC_. Because that's the default prefix for client variables in Astro projects.

@Eptagone
Copy link
Author

Eptagone commented Mar 21, 2024

Oops. Sorry, I thought this was a monorepo. I moved this issue to the correct repository.

New in vite-pwa/astro#42

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

No branches or pull requests

2 participants