From b11e3b38ebb59ceec3479cbf580276d3b3bd657c Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 24 Jun 2022 16:14:00 -0400 Subject: [PATCH] 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 --- .changeset/moody-crabs-stare.md | 5 ++++ packages/astro/src/core/build/static-build.ts | 1 + packages/astro/src/vite-plugin-env/index.ts | 2 ++ .../test/fixtures/ssr-env/astro.config.mjs | 5 ++++ .../astro/test/fixtures/ssr-env/package.json | 9 ++++++ .../fixtures/ssr-env/src/components/Env.jsx | 7 +++++ .../test/fixtures/ssr-env/src/pages/ssr.astro | 9 ++++++ packages/astro/test/ssr-env.test.js | 29 +++++++++++++++++++ pnpm-lock.yaml | 8 +++++ 9 files changed, 75 insertions(+) create mode 100644 .changeset/moody-crabs-stare.md create mode 100644 packages/astro/test/fixtures/ssr-env/astro.config.mjs create mode 100644 packages/astro/test/fixtures/ssr-env/package.json create mode 100644 packages/astro/test/fixtures/ssr-env/src/components/Env.jsx create mode 100644 packages/astro/test/fixtures/ssr-env/src/pages/ssr.astro create mode 100644 packages/astro/test/ssr-env.test.js diff --git a/.changeset/moody-crabs-stare.md b/.changeset/moody-crabs-stare.md new file mode 100644 index 000000000..19b9a23f2 --- /dev/null +++ b/.changeset/moody-crabs-stare.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Ensure import.meta.env.SSR is true in SSR mode diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 25e0b04e4..c912b74c1 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -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', diff --git a/packages/astro/src/vite-plugin-env/index.ts b/packages/astro/src/vite-plugin-env/index.ts index d3a5f2635..565ea0dae 100644 --- a/packages/astro/src/vite-plugin-env/index.ts +++ b/packages/astro/src/vite-plugin-env/index.ts @@ -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. diff --git a/packages/astro/test/fixtures/ssr-env/astro.config.mjs b/packages/astro/test/fixtures/ssr-env/astro.config.mjs new file mode 100644 index 000000000..42f10a572 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-env/astro.config.mjs @@ -0,0 +1,5 @@ +import preact from '@astrojs/preact'; + +export default { + integrations: [preact()] +} diff --git a/packages/astro/test/fixtures/ssr-env/package.json b/packages/astro/test/fixtures/ssr-env/package.json new file mode 100644 index 000000000..f2d339704 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-env/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/ssr-env", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/preact": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-env/src/components/Env.jsx b/packages/astro/test/fixtures/ssr-env/src/components/Env.jsx new file mode 100644 index 000000000..776cf3785 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-env/src/components/Env.jsx @@ -0,0 +1,7 @@ + +export default function() { + const ssr = import.meta.env.SSR; + return ( +
{'' + ssr }
+ ) +} diff --git a/packages/astro/test/fixtures/ssr-env/src/pages/ssr.astro b/packages/astro/test/fixtures/ssr-env/src/pages/ssr.astro new file mode 100644 index 000000000..e554d28ea --- /dev/null +++ b/packages/astro/test/fixtures/ssr-env/src/pages/ssr.astro @@ -0,0 +1,9 @@ +--- +import Env from '../components/Env.jsx'; +--- + +Test + + + + diff --git a/packages/astro/test/ssr-env.test.js b/packages/astro/test/ssr-env.test.js new file mode 100644 index 000000000..da823fa20 --- /dev/null +++ b/packages/astro/test/ssr-env.test.js @@ -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'); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8c22fd695..7be62432c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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:*