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:
Matthew Phillips 2022-06-24 16:14:00 -04:00 committed by GitHub
parent 5e716e8cd5
commit b11e3b38eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 75 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Ensure import.meta.env.SSR is true in SSR mode

View file

@ -122,6 +122,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
entryFileNames: opts.buildConfig.serverEntry,
},
},
ssr: true,
// must match an esbuild target
target: 'esnext',

View file

@ -78,6 +78,7 @@ export default function envVitePlugin({
privateEnv = getPrivateEnv(config, astroConfig);
if (privateEnv) {
privateEnv.SITE = astroConfig.site ? `'${astroConfig.site}'` : 'undefined';
privateEnv.SSR = JSON.stringify(true);
const entries = Object.entries(privateEnv).map(([key, value]) => [
`import.meta.env.${key}`,
value,
@ -86,6 +87,7 @@ export default function envVitePlugin({
// These additional replacements are needed to match Vite
replacements = Object.assign(replacements, {
'import.meta.env.SITE': astroConfig.site ? `'${astroConfig.site}'` : 'undefined',
'import.meta.env.SSR': JSON.stringify(true),
// This catches destructed `import.meta.env` calls,
// BUT we only want to inject private keys referenced in the file.
// We overwrite this value on a per-file basis.

View file

@ -0,0 +1,5 @@
import preact from '@astrojs/preact';
export default {
integrations: [preact()]
}

View file

@ -0,0 +1,9 @@
{
"name": "@test/ssr-env",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/preact": "workspace:*"
}
}

View file

@ -0,0 +1,7 @@
export default function() {
const ssr = import.meta.env.SSR;
return (
<div id="ssr">{'' + ssr }</div>
)
}

View file

@ -0,0 +1,9 @@
---
import Env from '../components/Env.jsx';
---
<html>
<head><title>Test</title></head>
<body>
<Env />
</body>
</html>

View 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');
});
});

View file

@ -1653,6 +1653,14 @@ importers:
'@astrojs/solid-js': link:../../../../integrations/solid
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:
specifiers:
astro: workspace:*