diff --git a/.changeset/calm-brooms-burn.md b/.changeset/calm-brooms-burn.md new file mode 100644 index 000000000000..d9ce3a8c8373 --- /dev/null +++ b/.changeset/calm-brooms-burn.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': major +--- + +breaking: remove top-level promise awaiting diff --git a/packages/kit/src/core/sync/write_types/index.js b/packages/kit/src/core/sync/write_types/index.js index d21030e01109..2c2f0fa0b5b3 100644 --- a/packages/kit/src/core/sync/write_types/index.js +++ b/packages/kit/src/core/sync/write_types/index.js @@ -480,7 +480,7 @@ function process_node(node, outdir, is_page, proxies, all_pages_have_load = true const from = proxy.modified ? `./proxy${replace_ext_with_js(path.basename(file_path))}` : path_to_original(outdir, file_path); - const type = `Kit.AwaitedProperties>>`; + const type = `Kit.LoadProperties>>`; return expand ? `Expand>>` : type; } else { return fallback; diff --git a/packages/kit/src/exports/public.d.ts b/packages/kit/src/exports/public.d.ts index e160cbb1b7d8..d196defdae40 100644 --- a/packages/kit/src/exports/public.d.ts +++ b/packages/kit/src/exports/public.d.ts @@ -39,20 +39,11 @@ export interface Adapter { adapt(builder: Builder): MaybePromise; } -type AwaitedPropertiesUnion | void> = input extends void +export type LoadProperties | void> = input extends void ? undefined // needs to be undefined, because void will break intellisense : input extends Record - ? { - [key in keyof input]: Awaited; - } - : {} extends input // handles the any case - ? input - : unknown; - -export type AwaitedProperties | void> = - AwaitedPropertiesUnion extends Record - ? OptionalUnion> - : AwaitedPropertiesUnion; + ? input + : unknown; export type AwaitedActions any>> = OptionalUnion< { diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 5afcd6e63bda..875f48f90fac 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -22,15 +22,14 @@ import { get_link_info, get_router_options, is_external_url, - scroll_state, - origin + origin, + scroll_state } from './utils.js'; import { base } from '__sveltekit/paths'; import * as devalue from 'devalue'; import { compact } from '../../utils/array.js'; import { validate_page_exports } from '../../utils/exports.js'; -import { unwrap_promises } from '../../utils/promises.js'; import { HttpError, Redirect } from '../control.js'; import { INVALIDATED_PARAM, TRAILING_SLASH_PARAM, validate_depends } from '../shared.js'; import { INDEX_KEY, PRELOAD_PRIORITIES, SCROLL_KEY, SNAPSHOT_KEY } from './constants.js'; @@ -555,7 +554,6 @@ export function create_client(app, target) { } else { data = (await node.universal.load.call(null, load_input)) ?? null; } - data = data ? await unwrap_promises(data, route.id) : null; } return { diff --git a/packages/kit/src/runtime/server/page/load_data.js b/packages/kit/src/runtime/server/page/load_data.js index 32aab24da0ce..ca126dd3c7eb 100644 --- a/packages/kit/src/runtime/server/page/load_data.js +++ b/packages/kit/src/runtime/server/page/load_data.js @@ -1,6 +1,5 @@ -import { disable_search, make_trackable } from '../../../utils/url.js'; -import { unwrap_promises } from '../../../utils/promises.js'; import { DEV } from 'esm-env'; +import { disable_search, make_trackable } from '../../../utils/url.js'; import { validate_depends } from '../../shared.js'; /** @@ -112,16 +111,15 @@ export async function load_server_data({ event, state, node, parent }) { url }); - const data = result ? await unwrap_promises(result, node.server_id) : null; if (__SVELTEKIT_DEV__) { - validate_load_response(data, node.server_id); + validate_load_response(result, node.server_id); } done = true; return { type: 'data', - data, + data: result ?? null, uses, slash: node.server.trailingSlash }; @@ -168,12 +166,11 @@ export async function load_data({ parent }); - const data = result ? await unwrap_promises(result, node.universal_id) : null; if (__SVELTEKIT_DEV__) { - validate_load_response(data, node.universal_id); + validate_load_response(result, node.universal_id); } - return data; + return result ?? null; } /** diff --git a/packages/kit/src/utils/promises.js b/packages/kit/src/utils/promises.js deleted file mode 100644 index ab9286722747..000000000000 --- a/packages/kit/src/utils/promises.js +++ /dev/null @@ -1,61 +0,0 @@ -import { DEV } from 'esm-env'; - -/** @type {Set | null} */ -let warned = null; - -// TODO v2: remove all references to unwrap_promises - -/** - * Given an object, return a new object where all top level values are awaited - * - * @param {Record} object - * @param {string | null} [id] - * @returns {Promise>} - */ -export async function unwrap_promises(object, id) { - if (DEV) { - /** @type {string[]} */ - const promises = []; - - for (const key in object) { - if (typeof object[key]?.then === 'function') { - promises.push(key); - } - } - - if (promises.length > 0) { - if (!warned) warned = new Set(); - - const last = promises.pop(); - - const properties = - promises.length > 0 - ? `${promises.map((p) => `"${p}"`).join(', ')} and "${last}" properties` - : `"${last}" property`; - - const location = id ? `the \`load\` function in ${id}` : 'a `load` function'; - - const description = promises.length > 0 ? 'are promises' : 'is a promise'; - - const message = `The top-level ${properties} returned from ${location} ${description}.`; - - if (!warned.has(message)) { - console.warn( - `\n${message}\n\nIn SvelteKit 2.0, these will longer be awaited automatically. To get rid of this warning, await all promises included as top-level properties in \`load\` return values.\n` - ); - - warned.add(message); - } - } - } - - for (const key in object) { - if (typeof object[key]?.then === 'function') { - return Object.fromEntries( - await Promise.all(Object.entries(object).map(async ([key, value]) => [key, await value])) - ); - } - } - - return object; -} diff --git a/packages/kit/test/apps/basics/src/routes/load/fetch-cache-control/headers-diff/+page.js b/packages/kit/test/apps/basics/src/routes/load/fetch-cache-control/headers-diff/+page.js index b64c9e0a9a68..2122574cd578 100644 --- a/packages/kit/test/apps/basics/src/routes/load/fetch-cache-control/headers-diff/+page.js +++ b/packages/kit/test/apps/basics/src/routes/load/fetch-cache-control/headers-diff/+page.js @@ -3,16 +3,16 @@ export async function load({ fetch, url }) { headers: { 'x-foo': 'a' } - }); + }).then((r) => r.json()); const r2 = await fetch(url.pathname, { headers: { 'x-foo': 'b' } - }); + }).then((r) => r.json()); return { - a: r1.json(), - b: r2.json() + a: r1, + b: r2 }; } diff --git a/packages/kit/test/apps/embed/src/routes/embed/+page.js b/packages/kit/test/apps/embed/src/routes/embed/+page.js index 4d321641c6d4..fb43b1061059 100644 --- a/packages/kit/test/apps/embed/src/routes/embed/+page.js +++ b/packages/kit/test/apps/embed/src/routes/embed/+page.js @@ -1,7 +1,7 @@ /** @type {import('@sveltejs/kit').Load} */ export async function load({ fetch }) { return { - a: fetch('/embed/a').then((x) => x.text()), - b: fetch('/embed/b').then((x) => x.text()) + a: await fetch('/embed/a').then((x) => x.text()), + b: await fetch('/embed/b').then((x) => x.text()) }; } diff --git a/sites/kit.svelte.dev/src/routes/+layout.server.js b/sites/kit.svelte.dev/src/routes/+layout.server.js index 8f2a04f03415..274c04f7f124 100644 --- a/sites/kit.svelte.dev/src/routes/+layout.server.js +++ b/sites/kit.svelte.dev/src/routes/+layout.server.js @@ -8,8 +8,8 @@ export const load = async ({ url, fetch }) => { return { nav_title: get_nav_title(url), - nav_links, - banner + nav_links: await nav_links, + banner: await banner }; };