Skip to content

Commit fae6b5a

Browse files
authored
fix: avoid running init hook during builds when there's nothing to prerender (#14464)
fixes #14347 This PR reverts the fix from #14219 which caused the init hook to run even if there's nothing to prerender. Instead of running the init hook early, we just set the env variables when looking for prerendered remote functions. This avoids any errors for missing env variables when evaluating the user's remote function modules. We only run the init hook once we've found things we need to prerender.
1 parent ff9cf14 commit fae6b5a

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

.changeset/loose-parks-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: avoid running the `init` hook during builds if there's nothing to prerender

packages/kit/src/core/postbuild/prerender.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as devalue from 'devalue';
1515
import { createReadableStream } from '@sveltejs/kit/node';
1616
import generate_fallback from './fallback.js';
1717
import { stringify_remote_arg } from '../../runtime/shared.js';
18+
import { filter_env } from '../../utils/env.js';
1819

1920
export default forked(import.meta.url, prerender);
2021

@@ -125,12 +126,6 @@ async function prerender({ hash, out, manifest_path, metadata, verbose, env }) {
125126

126127
installPolyfills();
127128

128-
const server = new Server(manifest);
129-
await server.init({
130-
env,
131-
read: (file) => createReadableStream(`${config.outDir}/output/server/${file}`)
132-
});
133-
134129
/** @type {Map<string, string>} */
135130
const saved = new Map();
136131

@@ -489,6 +484,15 @@ async function prerender({ hash, out, manifest_path, metadata, verbose, env }) {
489484
}
490485
}
491486

487+
// the user's remote function modules may reference environment variables at
488+
// the top-level so we need to set `env` before evaluating those modules
489+
// to avoid potential runtime errors
490+
const { publicPrefix: public_prefix, privatePrefix: private_prefix } = config.env;
491+
const private_env = filter_env(env, private_prefix, public_prefix);
492+
const public_env = filter_env(env, public_prefix, private_prefix);
493+
internal.set_private_env(private_env);
494+
internal.set_public_env(public_env);
495+
492496
/** @type {Array<import('types').RemoteInfo & { type: 'prerender'}>} */
493497
const prerender_functions = [];
494498

@@ -507,6 +511,14 @@ async function prerender({ hash, out, manifest_path, metadata, verbose, env }) {
507511
return { prerendered, prerender_map };
508512
}
509513

514+
// only run the server after the `should_prerender` check so that we
515+
// don't run the user's init hook unnecessarily
516+
const server = new Server(manifest);
517+
await server.init({
518+
env,
519+
read: (file) => createReadableStream(`${config.outDir}/output/server/${file}`)
520+
});
521+
510522
log.info('Prerendering');
511523

512524
for (const entry of config.prerender.entries) {

packages/kit/test/apps/options/source/hooks.server.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { building } from '$app/environment';
12
import { env } from '$env/dynamic/private';
23

34
// this verifies that dynamic env vars can be read during analysis phase
@@ -13,3 +14,11 @@ export function handle({ event, resolve }) {
1314
preload: ({ type }) => type !== 'css'
1415
});
1516
}
17+
18+
export function init() {
19+
if (building) {
20+
throw new Error(
21+
'There are no prerendered pages or functions in this app so init() should not be called during the build'
22+
);
23+
}
24+
}

0 commit comments

Comments
 (0)