Ensure import.meta.env.SSR is true in SSR mode (#3702)
* Ensure import.meta.env.SSR is true in SSR mode * Define in the env plugin instead
This commit is contained in:
parent
5e716e8cd5
commit
b11e3b38eb
9 changed files with 75 additions and 0 deletions
5
.changeset/moody-crabs-stare.md
Normal file
5
.changeset/moody-crabs-stare.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Ensure import.meta.env.SSR is true in SSR mode
|
|
@ -122,6 +122,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
|
||||||
entryFileNames: opts.buildConfig.serverEntry,
|
entryFileNames: opts.buildConfig.serverEntry,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
ssr: true,
|
ssr: true,
|
||||||
// must match an esbuild target
|
// must match an esbuild target
|
||||||
target: 'esnext',
|
target: 'esnext',
|
||||||
|
|
|
@ -78,6 +78,7 @@ export default function envVitePlugin({
|
||||||
privateEnv = getPrivateEnv(config, astroConfig);
|
privateEnv = getPrivateEnv(config, astroConfig);
|
||||||
if (privateEnv) {
|
if (privateEnv) {
|
||||||
privateEnv.SITE = astroConfig.site ? `'${astroConfig.site}'` : 'undefined';
|
privateEnv.SITE = astroConfig.site ? `'${astroConfig.site}'` : 'undefined';
|
||||||
|
privateEnv.SSR = JSON.stringify(true);
|
||||||
const entries = Object.entries(privateEnv).map(([key, value]) => [
|
const entries = Object.entries(privateEnv).map(([key, value]) => [
|
||||||
`import.meta.env.${key}`,
|
`import.meta.env.${key}`,
|
||||||
value,
|
value,
|
||||||
|
@ -86,6 +87,7 @@ export default function envVitePlugin({
|
||||||
// These additional replacements are needed to match Vite
|
// These additional replacements are needed to match Vite
|
||||||
replacements = Object.assign(replacements, {
|
replacements = Object.assign(replacements, {
|
||||||
'import.meta.env.SITE': astroConfig.site ? `'${astroConfig.site}'` : 'undefined',
|
'import.meta.env.SITE': astroConfig.site ? `'${astroConfig.site}'` : 'undefined',
|
||||||
|
'import.meta.env.SSR': JSON.stringify(true),
|
||||||
// This catches destructed `import.meta.env` calls,
|
// This catches destructed `import.meta.env` calls,
|
||||||
// BUT we only want to inject private keys referenced in the file.
|
// BUT we only want to inject private keys referenced in the file.
|
||||||
// We overwrite this value on a per-file basis.
|
// We overwrite this value on a per-file basis.
|
||||||
|
|
5
packages/astro/test/fixtures/ssr-env/astro.config.mjs
vendored
Normal file
5
packages/astro/test/fixtures/ssr-env/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import preact from '@astrojs/preact';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
integrations: [preact()]
|
||||||
|
}
|
9
packages/astro/test/fixtures/ssr-env/package.json
vendored
Normal file
9
packages/astro/test/fixtures/ssr-env/package.json
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"name": "@test/ssr-env",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"astro": "workspace:*",
|
||||||
|
"@astrojs/preact": "workspace:*"
|
||||||
|
}
|
||||||
|
}
|
7
packages/astro/test/fixtures/ssr-env/src/components/Env.jsx
vendored
Normal file
7
packages/astro/test/fixtures/ssr-env/src/components/Env.jsx
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
export default function() {
|
||||||
|
const ssr = import.meta.env.SSR;
|
||||||
|
return (
|
||||||
|
<div id="ssr">{'' + ssr }</div>
|
||||||
|
)
|
||||||
|
}
|
9
packages/astro/test/fixtures/ssr-env/src/pages/ssr.astro
vendored
Normal file
9
packages/astro/test/fixtures/ssr-env/src/pages/ssr.astro
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
---
|
||||||
|
import Env from '../components/Env.jsx';
|
||||||
|
---
|
||||||
|
<html>
|
||||||
|
<head><title>Test</title></head>
|
||||||
|
<body>
|
||||||
|
<Env />
|
||||||
|
</body>
|
||||||
|
</html>
|
29
packages/astro/test/ssr-env.test.js
Normal file
29
packages/astro/test/ssr-env.test.js
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import { expect } from 'chai';
|
||||||
|
import * as cheerio from 'cheerio';
|
||||||
|
import { loadFixture } from './test-utils.js';
|
||||||
|
import testAdapter from './test-adapter.js';
|
||||||
|
|
||||||
|
describe('SSR Environment Variables', () => {
|
||||||
|
/** @type {import('./test-utils').Fixture} */
|
||||||
|
let fixture;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
fixture = await loadFixture({
|
||||||
|
root: './fixtures/ssr-env/',
|
||||||
|
experimental: {
|
||||||
|
ssr: true,
|
||||||
|
},
|
||||||
|
adapter: testAdapter(),
|
||||||
|
});
|
||||||
|
await fixture.build();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('import.meta.env.SSR is true', async () => {
|
||||||
|
const app = await fixture.loadTestAdapterApp();
|
||||||
|
const request = new Request('http://example.com/ssr');
|
||||||
|
const response = await app.render(request);
|
||||||
|
const html = await response.text();
|
||||||
|
const $ = cheerio.load(html);
|
||||||
|
expect($('#ssr').text()).to.equal('true');
|
||||||
|
});
|
||||||
|
});
|
|
@ -1653,6 +1653,14 @@ importers:
|
||||||
'@astrojs/solid-js': link:../../../../integrations/solid
|
'@astrojs/solid-js': link:../../../../integrations/solid
|
||||||
astro: link:../../..
|
astro: link:../../..
|
||||||
|
|
||||||
|
packages/astro/test/fixtures/ssr-env:
|
||||||
|
specifiers:
|
||||||
|
'@astrojs/preact': workspace:*
|
||||||
|
astro: workspace:*
|
||||||
|
dependencies:
|
||||||
|
'@astrojs/preact': link:../../../../integrations/preact
|
||||||
|
astro: link:../../..
|
||||||
|
|
||||||
packages/astro/test/fixtures/ssr-markdown:
|
packages/astro/test/fixtures/ssr-markdown:
|
||||||
specifiers:
|
specifiers:
|
||||||
astro: workspace:*
|
astro: workspace:*
|
||||||
|
|
Loading…
Reference in a new issue