From af1b2a3e29d7ac53387bb68beb2197e82b5dcaa5 Mon Sep 17 00:00:00 2001 From: Markus Wendorf Date: Thu, 2 Nov 2023 17:14:30 +0100 Subject: [PATCH 1/3] use correct environment file for rendering spa fallback page --- .changeset/slow-olives-mix.md | 5 +++++ .../adapter-static/test/apps/spa/.env.production | 1 + .../adapter-static/test/apps/spa/.env.staging | 1 + .../adapter-static/test/apps/spa/package.json | 2 +- .../src/routes/fallback/[...rest]/+page.svelte | 6 ++++++ .../adapter-static/test/apps/spa/test/test.js | 6 ++++++ packages/kit/src/core/adapt/builder.js | 6 ++++-- packages/kit/src/core/adapt/index.js | 15 +++++++++++++-- packages/kit/src/exports/vite/index.js | 10 +++++++++- 9 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 .changeset/slow-olives-mix.md create mode 100644 packages/adapter-static/test/apps/spa/.env.production create mode 100644 packages/adapter-static/test/apps/spa/.env.staging diff --git a/.changeset/slow-olives-mix.md b/.changeset/slow-olives-mix.md new file mode 100644 index 000000000000..b6001577ccc2 --- /dev/null +++ b/.changeset/slow-olives-mix.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: use correct environment file for rendering spa fallback page diff --git a/packages/adapter-static/test/apps/spa/.env.production b/packages/adapter-static/test/apps/spa/.env.production new file mode 100644 index 000000000000..79d571855ca8 --- /dev/null +++ b/packages/adapter-static/test/apps/spa/.env.production @@ -0,0 +1 @@ +PUBLIC_VALUE="a .env.production variable should NOT be included in a mode=staging build" \ No newline at end of file diff --git a/packages/adapter-static/test/apps/spa/.env.staging b/packages/adapter-static/test/apps/spa/.env.staging new file mode 100644 index 000000000000..8483bf2722dd --- /dev/null +++ b/packages/adapter-static/test/apps/spa/.env.staging @@ -0,0 +1 @@ +PUBLIC_VALUE=42 \ No newline at end of file diff --git a/packages/adapter-static/test/apps/spa/package.json b/packages/adapter-static/test/apps/spa/package.json index 7b7f1e211a53..4b6230e448a8 100644 --- a/packages/adapter-static/test/apps/spa/package.json +++ b/packages/adapter-static/test/apps/spa/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "dev": "vite dev", - "build": "vite build", + "build": "vite build --mode staging", "preview": "sirv -p 5173 -s 200.html build", "test": "playwright test" }, diff --git a/packages/adapter-static/test/apps/spa/src/routes/fallback/[...rest]/+page.svelte b/packages/adapter-static/test/apps/spa/src/routes/fallback/[...rest]/+page.svelte index 671b43ec7195..8950feed162a 100644 --- a/packages/adapter-static/test/apps/spa/src/routes/fallback/[...rest]/+page.svelte +++ b/packages/adapter-static/test/apps/spa/src/routes/fallback/[...rest]/+page.svelte @@ -1 +1,7 @@ + +

the fallback page was rendered

+ +{env.PUBLIC_VALUE} diff --git a/packages/adapter-static/test/apps/spa/test/test.js b/packages/adapter-static/test/apps/spa/test/test.js index 70f18670ea1b..ef72cbc62707 100644 --- a/packages/adapter-static/test/apps/spa/test/test.js +++ b/packages/adapter-static/test/apps/spa/test/test.js @@ -27,3 +27,9 @@ test('renders error page for missing page', async ({ page }) => { await page.goto('/nosuchpage'); expect(await page.textContent('h1')).toEqual('404'); }); + +test('uses correct environment variables for fallback page (mode = staging)', async ({ page }) => { + await page.goto('/') + await page.goto('/fallback/x/y/z'); + expect(await page.textContent('b')).toEqual('42'); +}); \ No newline at end of file diff --git a/packages/kit/src/core/adapt/builder.js b/packages/kit/src/core/adapt/builder.js index c7e57bd9ab29..cc99e8da8555 100644 --- a/packages/kit/src/core/adapt/builder.js +++ b/packages/kit/src/core/adapt/builder.js @@ -24,6 +24,7 @@ const extensions = ['.html', '.js', '.mjs', '.json', '.css', '.svg', '.xml', '.w * prerendered: import('types').Prerendered; * prerender_map: import('types').PrerenderMap; * log: import('types').Logger; + * vite_config: import('vite').ResolvedConfig; * }} opts * @returns {import('@sveltejs/kit').Builder} */ @@ -34,7 +35,8 @@ export function create_builder({ route_data, prerendered, prerender_map, - log + log, + vite_config }) { /** @type {Map} */ const lookup = new Map(); @@ -144,7 +146,7 @@ export function create_builder({ async generateFallback(dest) { const manifest_path = `${config.kit.outDir}/output/server/manifest-full.js`; - const env = get_env(config.kit.env, 'production'); + const env = get_env(config.kit.env, vite_config.mode); const fallback = await generate_fallback({ manifest_path, diff --git a/packages/kit/src/core/adapt/index.js b/packages/kit/src/core/adapt/index.js index b5b3d151887b..235d18526687 100644 --- a/packages/kit/src/core/adapt/index.js +++ b/packages/kit/src/core/adapt/index.js @@ -8,8 +8,17 @@ import { create_builder } from './builder.js'; * @param {import('types').Prerendered} prerendered * @param {import('types').PrerenderMap} prerender_map * @param {import('types').Logger} log + * @param {import('vite').ResolvedConfig} vite_config */ -export async function adapt(config, build_data, server_metadata, prerendered, prerender_map, log) { +export async function adapt( + config, + build_data, + server_metadata, + prerendered, + prerender_map, + log, + vite_config +) { const { name, adapt } = config.kit.adapter; console.log(colors.bold().cyan(`\n> Using ${name}`)); @@ -21,8 +30,10 @@ export async function adapt(config, build_data, server_metadata, prerendered, pr route_data: build_data.manifest_data.routes.filter((route) => route.page || route.endpoint), prerendered, prerender_map, - log + log, + vite_config }); + await adapt(builder); log.success('done'); diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index 426dd36d265d..ddaa9e4ab74f 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -807,7 +807,15 @@ function kit({ svelte_config }) { if (kit.adapter) { const { adapt } = await import('../../core/adapt/index.js'); - await adapt(svelte_config, build_data, metadata, prerendered, prerender_map, log); + await adapt( + svelte_config, + build_data, + metadata, + prerendered, + prerender_map, + log, + vite_config + ); } else { console.log(colors.bold().yellow('\nNo adapter specified')); From 977ed373beedb81d2c9ae63ef6e80334d0bc93b0 Mon Sep 17 00:00:00 2001 From: Markus Wendorf Date: Thu, 2 Nov 2023 17:39:38 +0100 Subject: [PATCH 2/3] format --- packages/adapter-static/test/apps/spa/test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/adapter-static/test/apps/spa/test/test.js b/packages/adapter-static/test/apps/spa/test/test.js index ef72cbc62707..ee24e4de5e99 100644 --- a/packages/adapter-static/test/apps/spa/test/test.js +++ b/packages/adapter-static/test/apps/spa/test/test.js @@ -29,7 +29,7 @@ test('renders error page for missing page', async ({ page }) => { }); test('uses correct environment variables for fallback page (mode = staging)', async ({ page }) => { - await page.goto('/') + await page.goto('/'); await page.goto('/fallback/x/y/z'); expect(await page.textContent('b')).toEqual('42'); -}); \ No newline at end of file +}); From 670d6d86acfca2816880bdd656518db1e327a0c4 Mon Sep 17 00:00:00 2001 From: Markus Wendorf Date: Thu, 2 Nov 2023 19:24:32 +0100 Subject: [PATCH 3/3] Update packages/adapter-static/test/apps/spa/test/test.js Co-authored-by: Tee Ming --- packages/adapter-static/test/apps/spa/test/test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/adapter-static/test/apps/spa/test/test.js b/packages/adapter-static/test/apps/spa/test/test.js index ee24e4de5e99..f8f941ea2a2c 100644 --- a/packages/adapter-static/test/apps/spa/test/test.js +++ b/packages/adapter-static/test/apps/spa/test/test.js @@ -29,7 +29,6 @@ test('renders error page for missing page', async ({ page }) => { }); test('uses correct environment variables for fallback page (mode = staging)', async ({ page }) => { - await page.goto('/'); await page.goto('/fallback/x/y/z'); expect(await page.textContent('b')).toEqual('42'); });