Skip to content

Commit

Permalink
chore: remove top level promise await behavior (#11176)
Browse files Browse the repository at this point in the history
* chore: deprecate top level promise await behaviour

* breaking: remove top level promises await behavior

* types.... err... some of it

* format

* fix changeset

* add back reduced version of the utility type

* fix test

* fix test

* update

* changeset

---------

Co-authored-by: Ben McCann <[email protected]>
Co-authored-by: Simon Holthausen <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
4 people authored Dec 10, 2023
1 parent c4efc26 commit 6ca2fdc
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 94 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-brooms-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
---

breaking: remove top-level promise awaiting
2 changes: 1 addition & 1 deletion packages/kit/src/core/sync/write_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Awaited<ReturnType<typeof import('${from}').load>>>`;
const type = `Kit.LoadProperties<Awaited<ReturnType<typeof import('${from}').load>>>`;
return expand ? `Expand<OptionalUnion<EnsureDefined<${type}>>>` : type;
} else {
return fallback;
Expand Down
15 changes: 3 additions & 12 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,11 @@ export interface Adapter {
adapt(builder: Builder): MaybePromise<void>;
}

type AwaitedPropertiesUnion<input extends Record<string, any> | void> = input extends void
export type LoadProperties<input extends Record<string, any> | void> = input extends void
? undefined // needs to be undefined, because void will break intellisense
: input extends Record<string, any>
? {
[key in keyof input]: Awaited<input[key]>;
}
: {} extends input // handles the any case
? input
: unknown;

export type AwaitedProperties<input extends Record<string, any> | void> =
AwaitedPropertiesUnion<input> extends Record<string, any>
? OptionalUnion<AwaitedPropertiesUnion<input>>
: AwaitedPropertiesUnion<input>;
? input
: unknown;

export type AwaitedActions<T extends Record<string, (...args: any) => any>> = OptionalUnion<
{
Expand Down
6 changes: 2 additions & 4 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 5 additions & 8 deletions packages/kit/src/runtime/server/page/load_data.js
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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;
}

/**
Expand Down
61 changes: 0 additions & 61 deletions packages/kit/src/utils/promises.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
4 changes: 2 additions & 2 deletions packages/kit/test/apps/embed/src/routes/embed/+page.js
Original file line number Diff line number Diff line change
@@ -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())
};
}
4 changes: 2 additions & 2 deletions sites/kit.svelte.dev/src/routes/+layout.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
};

Expand Down

0 comments on commit 6ca2fdc

Please sign in to comment.