Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-remote-read-top-level.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: allow `read` to be used at the top-level of remote function files
2 changes: 1 addition & 1 deletion packages/kit/src/core/postbuild/analyse.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function analyse({
// essential we do this before analysing the code
internal.set_building();

// set env, in case it's used in initialisation
// set env, `read`, and `manifest`, in case they're used in initialisation
const { publicPrefix: public_prefix, privatePrefix: private_prefix } = config.env;
const private_env = filter_env(env, private_prefix, public_prefix);
const public_env = filter_env(env, public_prefix, private_prefix);
Expand Down
8 changes: 5 additions & 3 deletions packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,16 @@ async function prerender({ hash, out, manifest_path, metadata, verbose, env }) {
}
}

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

/** @type {Array<import('types').RemoteInfo & { type: 'prerender'}>} */
const prerender_functions = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script>
import { prerendered, prerendered_entries } from './prerender.remote.js';
import { prerendered, prerendered_entries, with_read } from './prerender.remote.js';

let prerendered_result = $state(null);
let live_result = $state(null);
let read_result = $state(null);
</script>

<a href="/remote/prerender/whole-page">whole-page</a>
Expand All @@ -17,3 +18,6 @@
>
{live_result}
</button>
<button id="fetch-with-read" onclick={async () => (read_result = await with_read())}>
{read_result}
</button>
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { building, dev } from '$app/environment';
import { prerender } from '$app/server';
import { prerender, read } from '$app/server';
import text from './test.txt?url';

// test that using `read()` at the top-level of a remote function file doesn't throw an error when we evaluate the remote function files
const response = read(testfile);
const content = await response.text();

export const prerendered = prerender(() => {
if (!building && !dev) {
Expand All @@ -23,3 +28,7 @@ export const prerendered_entries = prerender(
},
{ inputs: () => ['a', 'b', /* to test correct encoding */ '中文'], dynamic: true }
);

export const with_read = prerender(() => {
return content;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test content from read()