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

Improve env var handling in SSR #9637

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-socks-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Improves environment variables replacement in SSR
10 changes: 7 additions & 3 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -308,3 +308,7 @@ function isCommonNotAstro(dep: string): boolean {
)
);
}

function stringifyForDefine(value: string | undefined): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be stringifyOrDefine?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is "For" since we're stringifying the value for the define config passed to Vite.

return typeof value === 'string' ? JSON.stringify(value) : 'undefined';
}
18 changes: 0 additions & 18 deletions packages/astro/src/vite-plugin-env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Comment on lines -51 to -56
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to re-specify these here as Vite will already handle them. If we specify them here, we'd do a plain string replacement instead, which is less accurate than Vite's implementation.

return privateEnv;
}

Expand All @@ -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;
},
Expand Down