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:
parent
d77a0320b7
commit
5cba637c4e
3 changed files with 12 additions and 21 deletions
5
.changeset/curvy-socks-rhyme.md
Normal file
5
.changeset/curvy-socks-rhyme.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
Improves environment variables replacement in SSR
|
|
@ -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';
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue