Skip to content

Commit 45345bd

Browse files
authored
Merge branch 'main' into feat/expose-remote-form-submitted
2 parents efe688b + 102aecf commit 45345bd

File tree

8 files changed

+218
-227
lines changed

8 files changed

+218
-227
lines changed

.changeset/cold-islands-tell.md

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

.changeset/eager-experts-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: wait for commit promise instead of `settled`

packages/kit/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# @sveltejs/kit
22

3+
## 2.48.0
4+
### Minor Changes
5+
6+
7+
- feat: use experimental `fork` API when available ([#14793](https://github.com/sveltejs/kit/pull/14793))
8+
9+
10+
### Patch Changes
11+
12+
13+
- fix: await for `settled` instead of `tick` in navigate ([#14800](https://github.com/sveltejs/kit/pull/14800))
14+
315
## 2.47.3
416
### Patch Changes
517

packages/kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sveltejs/kit",
3-
"version": "2.47.3",
3+
"version": "2.48.0",
44
"description": "SvelteKit is the fastest way to build Svelte apps",
55
"keywords": [
66
"framework",

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

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,14 @@ const invalidated = [];
206206
*/
207207
const components = [];
208208

209-
/** @type {{id: string, token: {}, promise: Promise<import('./types.js').NavigationResult>} | null} */
209+
/** @type {{id: string, token: {}, promise: Promise<import('./types.js').NavigationResult>, fork: Promise<import('svelte').Fork | null> | null} | null} */
210210
let load_cache = null;
211211

212+
function discard_load_cache() {
213+
void load_cache?.fork?.then((f) => f?.discard());
214+
load_cache = null;
215+
}
216+
212217
/**
213218
* @type {Map<string, Promise<URL>>}
214219
* Cache for client-side rerouting, since it could contain async calls which we want to
@@ -382,7 +387,7 @@ async function _invalidate(include_load_functions = true, reset_page_state = tru
382387
// Also solves an edge case where a preload is triggered, the navigation for it
383388
// was then triggered and is still running while the invalidation kicks in,
384389
// at which point the invalidation should take over and "win".
385-
load_cache = null;
390+
discard_load_cache();
386391

387392
// Rerun queries
388393
if (force_invalidation) {
@@ -461,7 +466,7 @@ export async function _goto(url, options, redirect_count, nav_token) {
461466
// Clear preload cache when invalidateAll is true to ensure fresh data
462467
// after form submissions or explicit invalidations
463468
if (options.invalidateAll) {
464-
load_cache = null;
469+
discard_load_cache();
465470
}
466471

467472
await navigate({
@@ -518,11 +523,32 @@ async function _preload_data(intent) {
518523
preload_tokens.delete(preload);
519524
if (result.type === 'loaded' && result.state.error) {
520525
// Don't cache errors, because they might be transient
521-
load_cache = null;
526+
discard_load_cache();
522527
}
523528
return result;
524-
})
529+
}),
530+
fork: null
525531
};
532+
533+
if (svelte.fork) {
534+
const lc = load_cache;
535+
536+
lc.fork = lc.promise.then((result) => {
537+
// if load_cache was discarded before load_cache.promise could
538+
// resolve, bail rather than creating an orphan fork
539+
if (lc === load_cache && result.type === 'loaded') {
540+
try {
541+
return svelte.fork(() => {
542+
root.$set(result.props);
543+
});
544+
} catch {
545+
// if it errors, it's because the experimental flag isn't enabled
546+
}
547+
}
548+
549+
return null;
550+
});
551+
}
526552
}
527553

528554
return load_cache.promise;
@@ -1658,10 +1684,15 @@ async function navigate({
16581684
}
16591685

16601686
// reset preload synchronously after the history state has been set to avoid race conditions
1687+
const load_cache_fork = load_cache?.fork;
16611688
load_cache = null;
16621689

16631690
navigation_result.props.page.state = state;
16641691

1692+
/**
1693+
* @type {Promise<void> | undefined}
1694+
*/
1695+
let commit_promise;
16651696
if (started) {
16661697
const after_navigate = (
16671698
await Promise.all(
@@ -1692,7 +1723,14 @@ async function navigate({
16921723
navigation_result.props.page.url = url;
16931724
}
16941725

1695-
root.$set(navigation_result.props);
1726+
const fork = load_cache_fork && (await load_cache_fork);
1727+
1728+
if (fork) {
1729+
commit_promise = fork.commit();
1730+
} else {
1731+
root.$set(navigation_result.props);
1732+
}
1733+
16961734
update(navigation_result.props.page);
16971735
has_navigated = true;
16981736
} else {
@@ -1704,9 +1742,9 @@ async function navigate({
17041742
const promises = [tick()];
17051743

17061744
// need to render the DOM before we can scroll to the rendered elements and do focus management
1707-
// svelte.settled is only available in Svelte 5
1708-
if (/** @type {any} */ (svelte).settled) {
1709-
promises.push(/** @type {any} */ (svelte).settled());
1745+
// so we wait for the commit if there's one
1746+
if (commit_promise) {
1747+
promises.push(commit_promise);
17101748
}
17111749
// we still need to await tick everytime because if there's no async work settled resolves immediately
17121750
await Promise.all(promises);

packages/kit/src/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// generated during release, do not modify
22

33
/** @type {string} */
4-
export const VERSION = '2.47.3';
4+
export const VERSION = '2.48.0';

0 commit comments

Comments
 (0)