diff --git a/.changeset/wise-months-serve.md b/.changeset/wise-months-serve.md new file mode 100644 index 000000000..57b7569ea --- /dev/null +++ b/.changeset/wise-months-serve.md @@ -0,0 +1,7 @@ +--- +'astro': patch +--- + +Fixes use of `PUBLIC_` to reference env vars + +Previously `PUBLIC_` worked in server-only components such as .astro components. However if you had a client-side component you had to use `VITE_`. This was a bug with our build that is now fixed. \ No newline at end of file diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 8ae8312f7..1842afc22 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -203,6 +203,7 @@ class AstroBuilder { ], publicDir: viteConfig.publicDir, root: viteConfig.root, + envPrefix: 'PUBLIC_', server: viteConfig.server, base: this.config.buildOptions.site ? new URL(this.config.buildOptions.site).pathname : '/', }); diff --git a/packages/astro/test/astro-envs.test.js b/packages/astro/test/astro-envs.test.js index d8c7a3a8c..7e0c40026 100644 --- a/packages/astro/test/astro-envs.test.js +++ b/packages/astro/test/astro-envs.test.js @@ -20,4 +20,23 @@ describe('Environment Variables', () => { expect(indexHtml).to.not.include('CLUB_33'); expect(indexHtml).to.include('BLUE_BAYOU'); }); + + it('includes public env in client-side JS', async () => { + let dirs = await fixture.readdir('/assets'); + let found = false; + + // Look in all of the .js files to see if the public env is inlined. + // Testing this way prevents hardcoding expected js files. + // If we find it in any of them that's good enough to know its working. + await Promise.all(dirs.map(async path => { + if(path.endsWith('.js')) { + let js = await fixture.readFile(`/assets/${path}`); + if(js.includes('BLUE_BAYOU')) { + found = true; + } + } + })); + + expect(found).to.equal(true, 'found the env variable in the JS build'); + }); }); diff --git a/packages/astro/test/fixtures/astro-envs/src/components/Client.vue b/packages/astro/test/fixtures/astro-envs/src/components/Client.vue new file mode 100644 index 000000000..01bae708a --- /dev/null +++ b/packages/astro/test/fixtures/astro-envs/src/components/Client.vue @@ -0,0 +1,15 @@ + + + diff --git a/packages/astro/test/fixtures/astro-envs/src/pages/index.astro b/packages/astro/test/fixtures/astro-envs/src/pages/index.astro index 9dc6a380e..b2cb02b9d 100644 --- a/packages/astro/test/fixtures/astro-envs/src/pages/index.astro +++ b/packages/astro/test/fixtures/astro-envs/src/pages/index.astro @@ -1,2 +1,6 @@ +--- +import Client from '../components/Client.vue'; +--- {import.meta.env.PUBLIC_PLACE} {import.meta.env.SECRET_PLACE} + \ No newline at end of file diff --git a/packages/astro/test/test-utils.js b/packages/astro/test/test-utils.js index 702f3aef5..302191867 100644 --- a/packages/astro/test/test-utils.js +++ b/packages/astro/test/test-utils.js @@ -15,6 +15,7 @@ import preview from '../dist/core/preview/index.js'; * @property {typeof build} build * @property {(url: string, opts: any) => Promise} fetch * @property {(path: string) => Promise} readFile + * @property {(path: string) => Promise} readdir * @property {() => Promise} startDevServer */ @@ -70,6 +71,7 @@ export async function loadFixture(inlineConfig) { return previewServer; }, readFile: (filePath) => fs.promises.readFile(new URL(filePath.replace(/^\//, ''), config.dist), 'utf8'), + readdir: (fp) => fs.promises.readdir(new URL(fp.replace(/^\//, ''), config.dist)) }; }