Skip to content

Commit c63e158

Browse files
Copilotteemingc
andauthored
fix: set manifest and read implementation before evaluating remote function files during build (#14672)
* Initial plan * Initial test setup for remote function read at top-level issue Co-authored-by: teemingc <[email protected]> * Update test to use prerender remote function Co-authored-by: teemingc <[email protected]> * Fix: set manifest and read implementation before loading remote functions in prerender Co-authored-by: teemingc <[email protected]> * Remove temporary test app Co-authored-by: teemingc <[email protected]> * Add page using with_read remote function and changeset Co-authored-by: teemingc <[email protected]> * Apply suggestion from @teemingc * Apply suggestion from @teemingc * Apply suggestion from @teemingc * Apply suggestion from @teemingc * Apply suggestion from @teemingc * Apply suggestion from @teemingc * Apply suggestion from @teemingc --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: teemingc <[email protected]> Co-authored-by: Tee Ming <[email protected]>
1 parent 6f13d2b commit c63e158

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed
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: allow `read` to be used at the top-level of remote function files

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async function analyse({
5656
// essential we do this before analysing the code
5757
internal.set_building();
5858

59-
// set env, in case it's used in initialisation
59+
// set env, `read`, and `manifest`, in case they're used in initialisation
6060
const { publicPrefix: public_prefix, privatePrefix: private_prefix } = config.env;
6161
const private_env = filter_env(env, private_prefix, public_prefix);
6262
const public_env = filter_env(env, public_prefix, private_prefix);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,16 @@ async function prerender({ hash, out, manifest_path, metadata, verbose, env }) {
485485
}
486486
}
487487

488-
// the user's remote function modules may reference environment variables at
489-
// the top-level so we need to set `env` before evaluating those modules
490-
// to avoid potential runtime errors
488+
// the user's remote function modules may reference environment variables,
489+
// `read` or the `manifest` at the top-level so we need to set them before
490+
// evaluating those modules to avoid potential runtime errors
491491
const { publicPrefix: public_prefix, privatePrefix: private_prefix } = config.env;
492492
const private_env = filter_env(env, private_prefix, public_prefix);
493493
const public_env = filter_env(env, public_prefix, private_prefix);
494494
internal.set_private_env(private_env);
495495
internal.set_public_env(public_env);
496+
internal.set_manifest(manifest);
497+
internal.set_read_implementation((file) => createReadableStream(`${out}/server/${file}`));
496498

497499
/** @type {Array<import('types').RemoteInfo & { type: 'prerender'}>} */
498500
const prerender_functions = [];

packages/kit/test/apps/basics/src/routes/remote/prerender/+page.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script>
2-
import { prerendered, prerendered_entries } from './prerender.remote.js';
2+
import { prerendered, prerendered_entries, with_read } from './prerender.remote.js';
33
44
let prerendered_result = $state(null);
55
let live_result = $state(null);
6+
let read_result = $state(null);
67
</script>
78

89
<a href="/remote/prerender/whole-page">whole-page</a>
@@ -17,3 +18,6 @@
1718
>
1819
{live_result}
1920
</button>
21+
<button id="fetch-with-read" onclick={async () => (read_result = await with_read())}>
22+
{read_result}
23+
</button>

packages/kit/test/apps/basics/src/routes/remote/prerender/prerender.remote.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { building, dev } from '$app/environment';
2-
import { prerender } from '$app/server';
2+
import { prerender, read } from '$app/server';
3+
import text from './test.txt?url';
4+
5+
// test that using `read()` at the top-level of a remote function file doesn't
6+
// throw an error when we evaluate the remote function files during build
7+
const response = read(text);
8+
const content = await response.text();
39

410
export const prerendered = prerender(() => {
511
if (!building && !dev) {
@@ -23,3 +29,7 @@ export const prerendered_entries = prerender(
2329
},
2430
{ inputs: () => ['a', 'b', /* to test correct encoding */ '中文'], dynamic: true }
2531
);
32+
33+
export const with_read = prerender(() => {
34+
return content;
35+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test content from read()

0 commit comments

Comments
 (0)