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..f8f941ea2a2c 100644
--- a/packages/adapter-static/test/apps/spa/test/test.js
+++ b/packages/adapter-static/test/apps/spa/test/test.js
@@ -27,3 +27,8 @@ 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('/fallback/x/y/z');
+ expect(await page.textContent('b')).toEqual('42');
+});
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'));