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 environment prefix. It should be PUBLIC_ instead of VITE_ #42

Closed
Eptagone opened this issue Mar 21, 2024 · 12 comments · Fixed by #43
Closed

Wrong environment prefix. It should be PUBLIC_ instead of VITE_ #42

Eptagone opened this issue Mar 21, 2024 · 12 comments · Fixed by #43
Labels
bug Something isn't working

Comments

@Eptagone
Copy link

Eptagone commented Mar 21, 2024

If you have client environment variables which should be included in the client side scripts, in Astro projects, they should have the prefix PUBLIC_ but if you want to include them in a custom service worker, it won't work because VitePWA is ignoring them.
That's because VitePWA is expecting variables with the prefix VITE_.

The default value for the environment prefix in Vite is VITE_ but in Astro that prefix is PUBLIC_ instead.

@userquin
Copy link
Member

can you try changing the vite entry in your astro config file?

vite: {
  envPrefix: 'PUBLIC_'
}

@userquin
Copy link
Member

FYI: Astro configuring envPrefix properly:

imagen

@userquin
Copy link
Member

You cannot access in the client to non PUBLIC_ env variables:

imagen

@userquin
Copy link
Member

can you provide a minimal repo with the custom sw?

@Eptagone
Copy link
Author

can you try changing the vite entry in your astro config file?

vite: {
  envPrefix: 'PUBLIC_'
}

I tried but it didn't work :(

@Eptagone
Copy link
Author

Eptagone commented Mar 21, 2024

can you provide a minimal repo with the custom sw?

Here: https://github.com/Eptagone/astro-vitepwa-envprefix-test

I also discovered a strange thing. The PUBLIC_ are correctly included in development mode. They are only ignored in production (build).

This is a video. In development, it works as expected. The PUBLIC_* variables are read but in production (build) the VITE_* variables are read instead.

BIzqDs9Lpj.mp4

@userquin
Copy link
Member

userquin commented Mar 21, 2024

oh wait, you're using custom sw, maybe we need to change the astro pwa integration...

We'll need to include envPrefix here https://github.com/vite-pwa/vite-plugin-pwa/blob/main/src/vite-build.ts#L91-L108 via new option

@userquin userquin added the bug Something isn't working label Mar 21, 2024
@userquin
Copy link
Member

userquin commented Mar 21, 2024

Local test seems to work, rn only PUBLIC_ working, we need to add the array to make it work with PUBLIC_ and VITE_.

After adding .env and running build with the new vite plugin pwa (PR and release 0.19.6 in a few minutes with pwa astro):

imagen

@userquin
Copy link
Member

With both entries also working:

imagen

@userquin
Copy link
Member

tree shaking also working... nice

@userquin
Copy link
Member

userquin commented Mar 21, 2024

@Eptagone released 0.3.1 version, this issue should be fixed (your reproduction working)

@Eptagone
Copy link
Author

I can confirm that it's working now. Thank very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants