0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Improve env var handling in SSR (#9637)

This commit is contained in:
Bjorn Lu 2024-01-09 16:29:43 +08:00 committed by GitHub
parent d77a0320b7
commit 5cba637c4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 21 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Improves environment variables replacement in SSR

View file

@ -149,9 +149,9 @@ export async function createVite(
root: fileURLToPath(settings.config.root),
envPrefix: settings.config.vite?.envPrefix ?? 'PUBLIC_',
define: {
'import.meta.env.SITE': settings.config.site
? JSON.stringify(settings.config.site)
: 'undefined',
'import.meta.env.SITE': stringifyForDefine(settings.config.site),
'import.meta.env.BASE_URL': stringifyForDefine(settings.config.base),
'import.meta.env.ASSETS_PREFIX': stringifyForDefine(settings.config.build.assetsPrefix),
},
server: {
hmr:
@ -308,3 +308,7 @@ function isCommonNotAstro(dep: string): boolean {
)
);
}
function stringifyForDefine(value: string | undefined): string {
return typeof value === 'string' ? JSON.stringify(value) : 'undefined';
}

View file

@ -48,12 +48,6 @@ function getPrivateEnv(
}
}
}
privateEnv.SITE = astroConfig.site ? JSON.stringify(astroConfig.site) : 'undefined';
privateEnv.SSR = JSON.stringify(true);
privateEnv.BASE_URL = astroConfig.base ? JSON.stringify(astroConfig.base) : 'undefined';
privateEnv.ASSETS_PREFIX = astroConfig.build.assetsPrefix
? JSON.stringify(astroConfig.build.assetsPrefix)
: 'undefined';
return privateEnv;
}
@ -74,18 +68,6 @@ export default function envVitePlugin({ settings }: EnvPluginOptions): vite.Plug
return {
name: 'astro:vite-plugin-env',
enforce: 'pre',
config() {
return {
define: {
'import.meta.env.BASE_URL': astroConfig.base
? JSON.stringify(astroConfig.base)
: 'undefined',
'import.meta.env.ASSETS_PREFIX': astroConfig.build.assetsPrefix
? JSON.stringify(astroConfig.build.assetsPrefix)
: 'undefined',
},
};
},
configResolved(resolvedConfig) {
viteConfig = resolvedConfig;
},