Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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/flat-ducks-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: ensure rendering starts off synchronously
12 changes: 11 additions & 1 deletion packages/kit/src/exports/internal/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ export function getRequestEvent() {
export function get_request_store() {
const result = try_get_request_store();
if (!result) {
throw new Error('Could not get the request store. This is an internal error.');
let message = 'Could not get the request store.';

if (als) {
message += ' This is an internal error.';
} else {
message +=
' In environments without `AsyncLocalStorage`, the request store (used by e.g. remote functions) must be accessed synchronously, not after an `await`.' +
' If it was accessed synchronously then this is an internal error.';
}

throw new Error(message);
}
return result;
}
Expand Down
11 changes: 10 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,16 @@ export async function render_response({
// portable as possible, but reset afterwards
if (paths.relative) paths.override({ base, assets });

const rendered = options.root.render(props, render_opts);
const maybe_promise = options.root.render(props, render_opts);
// We have to invoke .then eagerly here in order to kick off rendering: it's only starting on access,
// and `await maybe_promise` would eagerly access the .then property but call its function only after a tick, which is too late
// for the paths.reset() below and for any eager getRequestEvent() calls during rendering without AsyncLocalStorage available.
const rendered =
options.async && 'then' in maybe_promise
? /** @type {ReturnType<typeof options.root.render> & Promise<any>} */ (
maybe_promise
).then((r) => r)
: maybe_promise;

// TODO 3.0 remove options.async
if (options.async) {
Expand Down
Loading