Skip to content

Commit 6ca2fdc

Browse files
gtm-nayanbenmccanndummdidummRich-Harris
authored
chore: remove top level promise await behavior (#11176)
* 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]>
1 parent c4efc26 commit 6ca2fdc

File tree

9 files changed

+24
-94
lines changed

9 files changed

+24
-94
lines changed

.changeset/calm-brooms-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': major
3+
---
4+
5+
breaking: remove top-level promise awaiting

packages/kit/src/core/sync/write_types/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ function process_node(node, outdir, is_page, proxies, all_pages_have_load = true
480480
const from = proxy.modified
481481
? `./proxy${replace_ext_with_js(path.basename(file_path))}`
482482
: path_to_original(outdir, file_path);
483-
const type = `Kit.AwaitedProperties<Awaited<ReturnType<typeof import('${from}').load>>>`;
483+
const type = `Kit.LoadProperties<Awaited<ReturnType<typeof import('${from}').load>>>`;
484484
return expand ? `Expand<OptionalUnion<EnsureDefined<${type}>>>` : type;
485485
} else {
486486
return fallback;

packages/kit/src/exports/public.d.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,11 @@ export interface Adapter {
3939
adapt(builder: Builder): MaybePromise<void>;
4040
}
4141

42-
type AwaitedPropertiesUnion<input extends Record<string, any> | void> = input extends void
42+
export type LoadProperties<input extends Record<string, any> | void> = input extends void
4343
? undefined // needs to be undefined, because void will break intellisense
4444
: input extends Record<string, any>
45-
? {
46-
[key in keyof input]: Awaited<input[key]>;
47-
}
48-
: {} extends input // handles the any case
49-
? input
50-
: unknown;
51-
52-
export type AwaitedProperties<input extends Record<string, any> | void> =
53-
AwaitedPropertiesUnion<input> extends Record<string, any>
54-
? OptionalUnion<AwaitedPropertiesUnion<input>>
55-
: AwaitedPropertiesUnion<input>;
45+
? input
46+
: unknown;
5647

5748
export type AwaitedActions<T extends Record<string, (...args: any) => any>> = OptionalUnion<
5849
{

packages/kit/src/runtime/client/client.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ import {
2222
get_link_info,
2323
get_router_options,
2424
is_external_url,
25-
scroll_state,
26-
origin
25+
origin,
26+
scroll_state
2727
} from './utils.js';
2828

2929
import { base } from '__sveltekit/paths';
3030
import * as devalue from 'devalue';
3131
import { compact } from '../../utils/array.js';
3232
import { validate_page_exports } from '../../utils/exports.js';
33-
import { unwrap_promises } from '../../utils/promises.js';
3433
import { HttpError, Redirect } from '../control.js';
3534
import { INVALIDATED_PARAM, TRAILING_SLASH_PARAM, validate_depends } from '../shared.js';
3635
import { INDEX_KEY, PRELOAD_PRIORITIES, SCROLL_KEY, SNAPSHOT_KEY } from './constants.js';
@@ -555,7 +554,6 @@ export function create_client(app, target) {
555554
} else {
556555
data = (await node.universal.load.call(null, load_input)) ?? null;
557556
}
558-
data = data ? await unwrap_promises(data, route.id) : null;
559557
}
560558

561559
return {

packages/kit/src/runtime/server/page/load_data.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { disable_search, make_trackable } from '../../../utils/url.js';
2-
import { unwrap_promises } from '../../../utils/promises.js';
31
import { DEV } from 'esm-env';
2+
import { disable_search, make_trackable } from '../../../utils/url.js';
43
import { validate_depends } from '../../shared.js';
54

65
/**
@@ -112,16 +111,15 @@ export async function load_server_data({ event, state, node, parent }) {
112111
url
113112
});
114113

115-
const data = result ? await unwrap_promises(result, node.server_id) : null;
116114
if (__SVELTEKIT_DEV__) {
117-
validate_load_response(data, node.server_id);
115+
validate_load_response(result, node.server_id);
118116
}
119117

120118
done = true;
121119

122120
return {
123121
type: 'data',
124-
data,
122+
data: result ?? null,
125123
uses,
126124
slash: node.server.trailingSlash
127125
};
@@ -168,12 +166,11 @@ export async function load_data({
168166
parent
169167
});
170168

171-
const data = result ? await unwrap_promises(result, node.universal_id) : null;
172169
if (__SVELTEKIT_DEV__) {
173-
validate_load_response(data, node.universal_id);
170+
validate_load_response(result, node.universal_id);
174171
}
175172

176-
return data;
173+
return result ?? null;
177174
}
178175

179176
/**

packages/kit/src/utils/promises.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

packages/kit/test/apps/basics/src/routes/load/fetch-cache-control/headers-diff/+page.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ export async function load({ fetch, url }) {
33
headers: {
44
'x-foo': 'a'
55
}
6-
});
6+
}).then((r) => r.json());
77

88
const r2 = await fetch(url.pathname, {
99
headers: {
1010
'x-foo': 'b'
1111
}
12-
});
12+
}).then((r) => r.json());
1313

1414
return {
15-
a: r1.json(),
16-
b: r2.json()
15+
a: r1,
16+
b: r2
1717
};
1818
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @type {import('@sveltejs/kit').Load} */
22
export async function load({ fetch }) {
33
return {
4-
a: fetch('/embed/a').then((x) => x.text()),
5-
b: fetch('/embed/b').then((x) => x.text())
4+
a: await fetch('/embed/a').then((x) => x.text()),
5+
b: await fetch('/embed/b').then((x) => x.text())
66
};
77
}

sites/kit.svelte.dev/src/routes/+layout.server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export const load = async ({ url, fetch }) => {
88

99
return {
1010
nav_title: get_nav_title(url),
11-
nav_links,
12-
banner
11+
nav_links: await nav_links,
12+
banner: await banner
1313
};
1414
};
1515

0 commit comments

Comments
 (0)