@sveltejs/kit 2.63.1 → 2.65.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/package.json +2 -2
  2. package/src/core/postbuild/analyse.js +0 -5
  3. package/src/core/postbuild/prerender.js +2 -0
  4. package/src/exports/public.d.ts +1 -1
  5. package/src/exports/vite/build/build_server.js +47 -57
  6. package/src/exports/vite/build/utils.js +0 -8
  7. package/src/exports/vite/index.js +226 -178
  8. package/src/runtime/app/server/remote/command.js +0 -3
  9. package/src/runtime/app/server/remote/form.js +18 -13
  10. package/src/runtime/app/server/remote/prerender.js +28 -34
  11. package/src/runtime/app/server/remote/query.js +105 -94
  12. package/src/runtime/app/server/remote/requested.js +14 -10
  13. package/src/runtime/app/server/remote/shared.js +25 -18
  14. package/src/runtime/client/client.js +19 -13
  15. package/src/runtime/client/remote-functions/command.svelte.js +7 -32
  16. package/src/runtime/client/remote-functions/form.svelte.js +62 -82
  17. package/src/runtime/client/remote-functions/prerender.svelte.js +14 -6
  18. package/src/runtime/client/remote-functions/query/index.js +6 -14
  19. package/src/runtime/client/remote-functions/query/instance.svelte.js +20 -0
  20. package/src/runtime/client/remote-functions/query/proxy.js +3 -3
  21. package/src/runtime/client/remote-functions/query-batch.svelte.js +59 -68
  22. package/src/runtime/client/remote-functions/query-live/instance.svelte.js +20 -6
  23. package/src/runtime/client/remote-functions/shared.svelte.js +76 -59
  24. package/src/runtime/server/page/render.js +20 -80
  25. package/src/runtime/server/page/server_routing.js +20 -15
  26. package/src/runtime/server/remote.js +296 -204
  27. package/src/runtime/server/respond.js +4 -2
  28. package/src/runtime/shared.js +83 -28
  29. package/src/types/global-private.d.ts +3 -3
  30. package/src/types/internal.d.ts +53 -34
  31. package/src/version.js +1 -1
  32. package/types/index.d.ts +4 -4
  33. package/types/index.d.ts.map +1 -1
@@ -1,7 +1,7 @@
1
1
  /** @import { RemoteResource, RemotePrerenderFunction } from '@sveltejs/kit' */
2
2
  /** @import { RemotePrerenderInputsGenerator, RemotePrerenderInternals, MaybePromise } from 'types' */
3
3
  /** @import { StandardSchemaV1 } from '@standard-schema/spec' */
4
- import { error, json } from '@sveltejs/kit';
4
+ import { json, error } from '@sveltejs/kit';
5
5
  import { DEV } from 'esm-env';
6
6
  import { get_request_store } from '@sveltejs/kit/internal/server';
7
7
  import { stringify, stringify_remote_arg } from '../../../shared.js';
@@ -9,7 +9,6 @@ import { noop } from '../../../../utils/functions.js';
9
9
  import { app_dir, base } from '$app/paths/internal/server';
10
10
  import {
11
11
  create_validator,
12
- get_cache,
13
12
  get_response,
14
13
  parse_remote_response,
15
14
  run_remote_function
@@ -89,51 +88,46 @@ export function prerender(validate_or_fn, fn_or_options, maybe_options) {
89
88
 
90
89
  /** @type {RemotePrerenderFunction<Input, Output> & { __: RemotePrerenderInternals }} */
91
90
  const wrapper = (arg) => {
91
+ const { event, state } = get_request_store();
92
+ const payload = stringify_remote_arg(arg, state.transport);
93
+
94
+ // `get_response` (as opposed to bare `get_cache`) also registers the call in the
95
+ // implicit lookup, so that the result is inlined into the page payload (`data.p`)
96
+ // and the client doesn't need to fetch it again upon hydration
92
97
  /** @type {Promise<Output> & Partial<RemoteResource<Output>>} */
93
- const promise = (async () => {
94
- const { event, state } = get_request_store();
95
- const payload = stringify_remote_arg(arg, state.transport);
98
+ const promise = get_response(__, payload, state, async () => {
96
99
  const id = __.id;
97
100
  const url = `${base}/${app_dir}/remote/${id}${payload ? `/${payload}` : ''}`;
98
101
 
99
102
  if (!state.prerendering && !DEV && !event.isRemoteRequest) {
100
103
  try {
101
- return await get_response(__, payload, state, async () => {
102
- const cache = get_cache(__, state);
103
-
104
- // TODO adapters can provide prerendered data more efficiently than
105
- // fetching from the public internet
106
- const promise = (cache[payload] ??= {
107
- serialize: true,
108
- data: fetch(new URL(url, event.url.origin).href).then(async (response) => {
109
- if (!response.ok) {
110
- throw new Error('Prerendered response not found');
111
- }
112
-
113
- const prerendered = await response.json();
114
-
115
- if (prerendered.type === 'error') {
116
- error(prerendered.status, prerendered.error);
117
- }
118
-
119
- return prerendered.result;
120
- })
121
- }).data;
122
-
123
- return parse_remote_response(await promise, state.transport);
124
- });
104
+ // TODO adapters can provide prerendered data more efficiently than
105
+ // fetching from the public internet
106
+ const response = await fetch(new URL(url, event.url.origin).href);
107
+
108
+ if (!response.ok) {
109
+ throw new Error('Prerendered response not found');
110
+ }
111
+
112
+ const prerendered = /** @type {RemoteFunctionResponse} */ await response.json();
113
+
114
+ if (prerendered.type === 'error') {
115
+ error(prerendered.status, prerendered.error);
116
+ }
117
+
118
+ return parse_remote_response(prerendered.data, state.transport)._;
125
119
  } catch {
126
120
  // not available prerendered, fallback to normal function
127
121
  }
128
122
  }
129
123
 
124
+ // during a prerender run, the same function might be invoked while rendering
125
+ // multiple pages — share the result across the entire run
130
126
  if (state.prerendering?.remote_responses.has(url)) {
131
127
  return /** @type {Promise<any>} */ (state.prerendering.remote_responses.get(url));
132
128
  }
133
129
 
134
- const promise = get_response(__, payload, state, () =>
135
- run_remote_function(event, state, false, () => validate(arg), fn)
136
- );
130
+ const promise = run_remote_function(event, state, false, () => validate(arg), fn);
137
131
 
138
132
  if (state.prerendering) {
139
133
  state.prerendering.remote_responses.set(url, promise);
@@ -142,7 +136,7 @@ export function prerender(validate_or_fn, fn_or_options, maybe_options) {
142
136
  const result = await promise;
143
137
 
144
138
  if (state.prerendering) {
145
- const body = { type: 'result', result: stringify(result, state.transport) };
139
+ const body = { type: 'result', data: stringify({ _: result }, state.transport) };
146
140
  state.prerendering.dependencies.set(url, {
147
141
  body: JSON.stringify(body),
148
142
  response: json(body)
@@ -151,7 +145,7 @@ export function prerender(validate_or_fn, fn_or_options, maybe_options) {
151
145
 
152
146
  // TODO this is missing error/loading/current/status
153
147
  return result;
154
- })();
148
+ });
155
149
 
156
150
  promise.catch(noop);
157
151
 
@@ -1,8 +1,8 @@
1
- /** @import { RemoteLiveQuery, RemoteLiveQueryFunction, RemoteQuery, RemoteQueryFunction } from '@sveltejs/kit' */
1
+ /** @import { RemoteLiveQuery, RemoteLiveQueryFunction, RemoteQuery, RemoteQueryFunction, RequestEvent } from '@sveltejs/kit' */
2
2
  /** @import { RemoteInternals, MaybePromise, RequestState, RemoteQueryLiveInternals, RemoteQueryBatchInternals, RemoteQueryInternals, RemoteLiveQueryUserFunctionReturnType } from 'types' */
3
3
  /** @import { StandardSchemaV1 } from '@standard-schema/spec' */
4
4
  import { get_request_store } from '@sveltejs/kit/internal/server';
5
- import { create_remote_key, stringify, stringify_remote_arg } from '../../../shared.js';
5
+ import { create_remote_key, stringify_remote_arg } from '../../../shared.js';
6
6
  import { prerendering } from '$app/env/internal';
7
7
  import {
8
8
  create_validator,
@@ -11,10 +11,10 @@ import {
11
11
  run_remote_function,
12
12
  run_remote_generator
13
13
  } from './shared.js';
14
- import { handle_error_and_jsonify } from '../../../server/utils.js';
15
- import { HttpError, SvelteKitError } from '@sveltejs/kit/internal';
16
14
  import { noop } from '../../../../utils/functions.js';
17
15
  import { SharedIterator } from '../../../../utils/shared-iterator.js';
16
+ import { handle_error_and_jsonify } from '../../../server/utils.js';
17
+ import { HttpError, SvelteKitError } from '@sveltejs/kit/internal';
18
18
 
19
19
  /**
20
20
  * Creates a remote query. When called from the browser, the function will be invoked on the server via a `fetch` call.
@@ -78,8 +78,14 @@ export function query(validate_or_fn, maybe_fn) {
78
78
  bind(payload, validated_arg) {
79
79
  const { event, state } = get_request_store();
80
80
 
81
- return create_query_resource(__, payload, state, () =>
82
- run_remote_function(event, state, false, () => validated_arg, fn)
81
+ return create_query_resource(__, payload, event, state, () =>
82
+ run_remote_function(
83
+ event,
84
+ { ...state, is_in_remote_query: true },
85
+ false,
86
+ () => validated_arg,
87
+ fn
88
+ )
83
89
  );
84
90
  }
85
91
  };
@@ -95,8 +101,14 @@ export function query(validate_or_fn, maybe_fn) {
95
101
  const { event, state } = get_request_store();
96
102
  const payload = stringify_remote_arg(arg, state.transport);
97
103
 
98
- return create_query_resource(__, payload, state, () =>
99
- run_remote_function(event, state, false, () => validate(arg), fn)
104
+ return create_query_resource(__, payload, event, state, () =>
105
+ run_remote_function(
106
+ event,
107
+ { ...state, is_in_remote_query: true },
108
+ false,
109
+ () => validate(arg),
110
+ fn
111
+ )
100
112
  );
101
113
  };
102
114
 
@@ -152,7 +164,14 @@ function live(validate_or_fn, maybe_fn) {
152
164
  * @param {any} get_input
153
165
  */
154
166
  const run = (event, state, get_input) =>
155
- run_remote_generator(event, state, false, get_input, fn, __.name);
167
+ run_remote_generator(
168
+ event,
169
+ { ...state, is_in_remote_query: true },
170
+ false,
171
+ get_input,
172
+ fn,
173
+ __.name
174
+ );
156
175
 
157
176
  /** @type {RemoteQueryLiveInternals} */
158
177
  const __ = {
@@ -164,7 +183,7 @@ function live(validate_or_fn, maybe_fn) {
164
183
  bind(payload, validated_arg) {
165
184
  const { event, state } = get_request_store();
166
185
 
167
- return create_live_query_resource(__, payload, state, event.request.signal, () =>
186
+ return create_live_query_resource(__, payload, event, state, () =>
168
187
  run(event, state, () => validated_arg)
169
188
  );
170
189
  }
@@ -181,7 +200,7 @@ function live(validate_or_fn, maybe_fn) {
181
200
  const { event, state } = get_request_store();
182
201
  const payload = stringify_remote_arg(arg, state.transport);
183
202
 
184
- return create_live_query_resource(__, payload, state, event.request.signal, () =>
203
+ return create_live_query_resource(__, payload, event, state, () =>
185
204
  run(event, state, () => validate(arg))
186
205
  );
187
206
  };
@@ -273,7 +292,7 @@ function batch(validate_or_fn, maybe_fn) {
273
292
  try {
274
293
  return await run_remote_function(
275
294
  event,
276
- state,
295
+ { ...state, is_in_remote_query: true },
277
296
  false,
278
297
  async () => Promise.all(entries.map((entry) => entry.get_validated())),
279
298
  async (input) => {
@@ -316,7 +335,7 @@ function batch(validate_or_fn, maybe_fn) {
316
335
 
317
336
  return run_remote_function(
318
337
  event,
319
- state,
338
+ { ...state, is_in_remote_query: true },
320
339
  false,
321
340
  async () => Promise.all(args.map(validate)),
322
341
  async (/** @type {any[]} */ input) => {
@@ -326,7 +345,7 @@ function batch(validate_or_fn, maybe_fn) {
326
345
  input.map(async (arg, i) => {
327
346
  try {
328
347
  const data = get_result(arg, i);
329
- return { type: 'result', data: stringify(data, state.transport) };
348
+ return { type: 'result', data };
330
349
  } catch (error) {
331
350
  return {
332
351
  type: 'error',
@@ -343,9 +362,11 @@ function batch(validate_or_fn, maybe_fn) {
343
362
  );
344
363
  },
345
364
  bind(payload, validated_arg) {
346
- const { state } = get_request_store();
365
+ const { event, state } = get_request_store();
347
366
 
348
- return create_query_resource(__, payload, state, () => enqueue(payload, () => validated_arg));
367
+ return create_query_resource(__, payload, event, state, () =>
368
+ enqueue(payload, () => validated_arg)
369
+ );
349
370
  }
350
371
  };
351
372
 
@@ -357,10 +378,10 @@ function batch(validate_or_fn, maybe_fn) {
357
378
  );
358
379
  }
359
380
 
360
- const { state } = get_request_store();
381
+ const { event, state } = get_request_store();
361
382
  const payload = stringify_remote_arg(arg, state.transport);
362
383
 
363
- return create_query_resource(__, payload, state, () =>
384
+ return create_query_resource(__, payload, event, state, () =>
364
385
  // Collect all the calls to the same query in the same macrotask,
365
386
  // then execute them as one backend request.
366
387
  enqueue(payload, () => validate(arg))
@@ -372,14 +393,53 @@ function batch(validate_or_fn, maybe_fn) {
372
393
  return wrapper;
373
394
  }
374
395
 
396
+ /**
397
+ * Include this value in the returned payload...
398
+ * @param {RequestEvent} event
399
+ * @param {RequestState} state
400
+ * @param {RemoteInternals} internals
401
+ * @param {string} payload
402
+ * @param {() => Promise<any>} fn
403
+ */
404
+ export function refresh(event, state, internals, payload, fn) {
405
+ if (!internals.id) {
406
+ // unless this is a non-exported (i.e. private) query...
407
+ return;
408
+ }
409
+
410
+ if (!event.isRemoteRequest) {
411
+ // or this is a no-JS form submission
412
+ return;
413
+ }
414
+
415
+ const key = create_remote_key(internals.id, payload);
416
+
417
+ // `fn()` is invoked eagerly here, which starts running the query immediately.
418
+ // The resulting promise is normally awaited (and its rejection handled) in
419
+ // `collect_remote_data`, but some code paths (e.g. a command throwing a
420
+ // non-redirect error) never reach that point. Attach a no-op `catch` to the
421
+ // promise so a rejection is always considered handled and can never become an
422
+ // unhandled promise rejection (which crashes the process on modern Node).
423
+ // We still store the original promise so `collect_remote_data` can serialize
424
+ // either its value or its error as before.
425
+ const promise = fn();
426
+ promise.catch(() => {});
427
+
428
+ (state.remote.explicit ??= new Map()).set(key, {
429
+ internals,
430
+ promise
431
+ });
432
+ }
433
+
375
434
  /**
376
435
  * @param {RemoteInternals} __
377
436
  * @param {string} payload — the stringified raw argument (i.e. the cache key the client will use)
437
+ * @param {RequestEvent} event
378
438
  * @param {RequestState} state
379
439
  * @param {() => Promise<any>} fn
380
440
  * @returns {RemoteQuery<any>}
381
441
  */
382
- function create_query_resource(__, payload, state, fn) {
442
+ function create_query_resource(__, payload, event, state, fn) {
383
443
  /** @type {Promise<any> | null} */
384
444
  let promise = null;
385
445
 
@@ -391,7 +451,11 @@ function create_query_resource(__, payload, state, fn) {
391
451
  // accessing data properties needs to kick off the work
392
452
  // so that it gets seeded in the hydration cache
393
453
  // and becomes available on the client
394
- void (__.id && state.is_in_render && get_promise());
454
+ if (__.id && state.is_in_render) {
455
+ // swallow rejections so they don't crash the server — the error is
456
+ // serialized into the response and surfaced on the client instead
457
+ get_promise().catch(noop);
458
+ }
395
459
  };
396
460
 
397
461
  return {
@@ -420,20 +484,19 @@ function create_query_resource(__, payload, state, fn) {
420
484
  return false;
421
485
  },
422
486
  refresh() {
423
- const { event } = get_request_store();
424
- if (!event.isRemoteRequest) {
425
- // If the form submission is not a remote request, refreshing the data is
426
- // useless, because it can't be returned to the client.
427
- return Promise.resolve();
428
- }
429
- const refresh_context = get_refresh_context(__, 'refresh', payload);
430
- const is_immediate_refresh = !refresh_context.cache[refresh_context.payload];
431
- const value = is_immediate_refresh ? get_promise() : fn();
432
- return update_refresh_value(refresh_context, value, is_immediate_refresh);
487
+ promise = null;
488
+ delete get_cache(__, state)[payload];
489
+
490
+ refresh(event, state, __, payload, get_promise);
491
+
492
+ return Promise.resolve();
433
493
  },
434
494
  /** @param {any} value */
435
495
  set(value) {
436
- return update_refresh_value(get_refresh_context(__, 'set', payload), value);
496
+ const p = (promise = Promise.resolve(value));
497
+ get_cache(__, state)[payload] = p;
498
+
499
+ refresh(event, state, __, payload, () => p);
437
500
  },
438
501
  // TODO 3.0 remove this
439
502
  // @ts-expect-error This method no longer exists
@@ -458,12 +521,12 @@ function create_query_resource(__, payload, state, fn) {
458
521
  /**
459
522
  * @param {RemoteQueryLiveInternals} __
460
523
  * @param {string} payload — the stringified raw argument (i.e. the cache key the client will use)
524
+ * @param {RequestEvent} event
461
525
  * @param {RequestState} state
462
- * @param {AbortSignal} signal — the request signal; aborts in-flight iteration when the client disconnects
463
526
  * @param {() => AsyncGenerator<any, void, void>} get_generator
464
527
  * @returns {RemoteLiveQuery<any>}
465
528
  */
466
- function create_live_query_resource(__, payload, state, signal, get_generator) {
529
+ function create_live_query_resource(__, payload, event, state, get_generator) {
467
530
  /** @type {Promise<any> | null} */
468
531
  let promise = null;
469
532
 
@@ -479,7 +542,11 @@ function create_live_query_resource(__, payload, state, signal, get_generator) {
479
542
  };
480
543
 
481
544
  const populate_hydratable = () => {
482
- void (__.id && state.is_in_render && get_promise());
545
+ if (__.id && state.is_in_render) {
546
+ // swallow rejections so they don't crash the server — the error is
547
+ // serialized into the response and surfaced on the client instead
548
+ get_promise().catch(noop);
549
+ }
483
550
  };
484
551
 
485
552
  return {
@@ -516,15 +583,11 @@ function create_live_query_resource(__, payload, state, signal, get_generator) {
516
583
  return false;
517
584
  },
518
585
  reconnect() {
519
- const reconnects = state.remote.reconnects;
586
+ promise = null;
587
+ delete get_cache(__, state)[payload];
520
588
 
521
- if (!reconnects) {
522
- throw new Error(
523
- `Cannot call reconnect on query.live '${__.name}' because it is not executed in the context of a command/form remote function`
524
- );
525
- }
589
+ refresh(event, state, __, payload, get_promise);
526
590
 
527
- reconnects.set(create_remote_key(__.id, payload), get_promise());
528
591
  return Promise.resolve();
529
592
  },
530
593
  /** @ts-expect-error This method no longer exists */
@@ -542,7 +605,7 @@ function create_live_query_resource(__, payload, state, signal, get_generator) {
542
605
  const cache = (state.remote.live_iterators ??= new Map());
543
606
  let cached = cache.get(key);
544
607
  if (!cached) {
545
- cached = create_shared_live_iterator(signal, get_generator);
608
+ cached = create_shared_live_iterator(event.request.signal, get_generator);
546
609
  cache.set(key, cached);
547
610
  }
548
611
  return cached.subscribe();
@@ -620,55 +683,3 @@ function create_shared_live_iterator(signal, get_generator) {
620
683
  // Add batch as a property to the query function
621
684
  Object.defineProperty(query, 'batch', { value: batch, enumerable: true });
622
685
  Object.defineProperty(query, 'live', { value: live, enumerable: true });
623
-
624
- /**
625
- * @param {RemoteInternals} __
626
- * @param {'set' | 'refresh'} action
627
- * @param {string} payload — the stringified raw argument (i.e. the cache key the client will use)
628
- * @returns {{ __: RemoteInternals; state: any; refreshes: Map<string, Promise<any>>; cache: Record<string, { serialize: boolean; data: any }>; refreshes_key: string; payload: string }}
629
- */
630
- function get_refresh_context(__, action, payload) {
631
- const { state } = get_request_store();
632
- const { refreshes } = state.remote;
633
-
634
- if (!refreshes) {
635
- const name = __.type === 'query_batch' ? `query.batch '${__.name}'` : `query '${__.name}'`;
636
- throw new Error(
637
- `Cannot call ${action} on ${name} because it is not executed in the context of a command/form remote function`
638
- );
639
- }
640
-
641
- const cache = get_cache(__, state);
642
- const refreshes_key = create_remote_key(__.id, payload);
643
-
644
- return { __, state, refreshes, refreshes_key, cache, payload };
645
- }
646
-
647
- /**
648
- * @param {{ __: RemoteInternals; refreshes: Map<string, Promise<any>>; cache: Record<string, { serialize: boolean; data: any }>; refreshes_key: string; payload: string }} context
649
- * @param {any} value
650
- * @param {boolean} [is_immediate_refresh=false]
651
- * @returns {Promise<void>}
652
- */
653
- function update_refresh_value(
654
- { __, refreshes, refreshes_key, cache, payload },
655
- value,
656
- is_immediate_refresh = false
657
- ) {
658
- const promise = Promise.resolve(value);
659
-
660
- if (!is_immediate_refresh) {
661
- cache[payload] = { serialize: true, data: promise };
662
- }
663
-
664
- if (__.id) {
665
- refreshes.set(refreshes_key, promise);
666
- }
667
-
668
- promise.catch(noop);
669
-
670
- // we return an immediately-resolving promise so that the `refresh()` signature is consistent,
671
- // but it doesn't delay anything if awaited inside a command. this way, people aren't
672
- // penalised if they do `await q1.refresh(); await q2.refresh()`
673
- return Promise.resolve();
674
- }
@@ -1,8 +1,11 @@
1
1
  /** @import { RemoteLiveQuery, RemoteLiveQueryFunction, RemoteQuery, RemoteQueryFunction, RequestedResult, QueryRequestedResult, LiveQueryRequestedResult } from '@sveltejs/kit' */
2
2
  /** @import { MaybePromise, RemoteAnyQueryInternals } from 'types' */
3
+ import { HttpError } from '@sveltejs/kit/internal';
3
4
  import { get_request_store } from '@sveltejs/kit/internal/server';
4
- import { create_remote_key, parse_remote_arg } from '../../../shared.js';
5
+ import { parse_remote_arg } from '../../../shared.js';
5
6
  import { noop } from '../../../../utils/functions.js';
7
+ import { get_cache } from './shared.js';
8
+ import { refresh } from './query.js';
6
9
 
7
10
  /**
8
11
  * In the context of a remote `command` or `form` request, returns an iterable
@@ -95,7 +98,7 @@ import { noop } from '../../../../utils/functions.js';
95
98
  * @returns {RequestedResult<Validated, Output>}
96
99
  */
97
100
  export function requested(query, limit) {
98
- const { state } = get_request_store();
101
+ const { event, state } = get_request_store();
99
102
  const internals = /** @type {RemoteAnyQueryInternals | undefined} */ (
100
103
  /** @type {any} */ (query).__
101
104
  );
@@ -115,15 +118,12 @@ export function requested(query, limit) {
115
118
 
116
119
  const requested = state.remote.requested;
117
120
  const payloads = requested?.get(__.id) ?? [];
121
+
118
122
  // note: don't initialize these maps here -- they will be initialized by the
119
123
  // command/form wrapper when we enter them, and if we initialize them here
120
124
  // we will enable requested(...) in contexts where it shouldn't be allowed,
121
125
  // such as load functions or other server functions
122
- const refreshes = state.remote.refreshes;
123
- const reconnects = state.remote.reconnects;
124
- const store = __.type === 'query_live' ? reconnects : refreshes;
125
-
126
- if (!store) {
126
+ if (!state.is_in_remote_form_or_command) {
127
127
  throw new Error(
128
128
  'requested(...) can only be called in the context of a command/form remote function'
129
129
  );
@@ -131,6 +131,9 @@ export function requested(query, limit) {
131
131
  const [selected, skipped] = split_limit(payloads, limit);
132
132
 
133
133
  /**
134
+ * Registers the failure exactly like `.set()` registers a value: the error record
135
+ * is serialized to the client (putting the query there into a failed state), and
136
+ * subsequent server-side calls of the query with the same argument reject with it.
134
137
  * @param {string} payload
135
138
  * @param {unknown} error
136
139
  */
@@ -138,14 +141,15 @@ export function requested(query, limit) {
138
141
  const promise = Promise.reject(error);
139
142
  promise.catch(noop);
140
143
 
141
- const key = create_remote_key(__.id, payload);
142
- store.set(key, promise);
144
+ get_cache(__, state)[payload] = promise;
145
+ refresh(event, state, __, payload, () => promise);
143
146
  };
144
147
 
145
148
  for (const payload of skipped) {
146
149
  record_failure(
147
150
  payload,
148
- new Error(
151
+ new HttpError(
152
+ 400,
149
153
  `Requested refresh was rejected because it exceeded requested(${__.name}, ${limit}) limit`
150
154
  )
151
155
  );
@@ -3,8 +3,6 @@
3
3
  import { parse } from 'devalue';
4
4
  import { error } from '@sveltejs/kit';
5
5
  import { with_request_store, get_request_store } from '@sveltejs/kit/internal/server';
6
- import { noop } from '../../../../utils/functions.js';
7
- import { create_remote_key, stringify, unfriendly_hydratable } from '../../../shared.js';
8
6
 
9
7
  /**
10
8
  * @param {any} validate_or_fn
@@ -75,24 +73,13 @@ export async function get_response(internals, payload, state, get_result) {
75
73
  await 0;
76
74
 
77
75
  const cache = get_cache(internals, state);
78
- const entry = (cache[payload] ??= {
79
- serialize: false,
80
- data: get_result()
81
- });
82
76
 
83
- entry.serialize ||= !!state.is_in_universal_load;
84
-
85
- if (state.is_in_render && internals.id) {
86
- const remote_key = create_remote_key(internals.id, payload);
87
-
88
- Promise.resolve(entry.data)
89
- .then((value) => {
90
- void unfriendly_hydratable(remote_key, () => stringify(value, state.transport));
91
- })
92
- .catch(noop);
77
+ if (!state.is_in_remote_query) {
78
+ // if this is a top-level (not nested) `await myQuery()`, include it in the serialized response
79
+ get_implicit_lookup(internals, state)[payload] = get_result;
93
80
  }
94
81
 
95
- return entry.data;
82
+ return (cache[payload] ??= get_result());
96
83
  }
97
84
 
98
85
  /**
@@ -238,10 +225,15 @@ function to_iterator(source, name) {
238
225
  }
239
226
 
240
227
  /**
228
+ * Note that `state` is deliberately not optional: resources that capture the request
229
+ * state at creation must pass it explicitly, because reading it from the request store
230
+ * at call time is only equivalent on runtimes with `AsyncLocalStorage` support.
231
+ * Callers without a captured state (such as the module-level `form` instance getters)
232
+ * should pass `get_request_store().state` themselves.
241
233
  * @param {RemoteInternals} internals
242
234
  * @param {RequestState} state
243
235
  */
244
- export function get_cache(internals, state = get_request_store().state) {
236
+ export function get_cache(internals, state) {
245
237
  let cache = state.remote.data?.get(internals);
246
238
 
247
239
  if (cache === undefined) {
@@ -251,3 +243,18 @@ export function get_cache(internals, state = get_request_store().state) {
251
243
 
252
244
  return cache;
253
245
  }
246
+
247
+ /**
248
+ * @param {RemoteInternals} internals
249
+ * @param {RequestState} state
250
+ */
251
+ export function get_implicit_lookup(internals, state) {
252
+ let cache = state.remote.implicit?.get(internals);
253
+
254
+ if (cache === undefined) {
255
+ cache = {};
256
+ (state.remote.implicit ??= new Map()).set(internals, cache);
257
+ }
258
+
259
+ return cache;
260
+ }
@@ -1,4 +1,4 @@
1
- /** @import { ServerNodesResponse, ServerRedirectNode } from 'types' */
1
+ /** @import { RemoteFunctionDataNode, ServerNodesResponse, ServerRedirectNode } from 'types' */
2
2
  /** @import { CacheEntry } from './remote-functions/cache.svelte.js' */
3
3
  /** @import { Query } from './remote-functions/query/instance.svelte.js' */
4
4
  /** @import { LiveQuery } from './remote-functions/query-live/instance.svelte.js' */
@@ -202,18 +202,20 @@ let target;
202
202
  export let app;
203
203
 
204
204
  /**
205
- * Data that was serialized during SSR for queries/forms/commands.
206
- * This is cleared before client-side loads run.
207
- * @type {Record<string, any>}
205
+ * Data that was serialized during SSR for queries/forms/commands, stored as
206
+ * `{ v }` (value) or `{ e }` (error) nodes so that failed states survive hydration.
207
+ * Entries are deleted as they are consumed (when the corresponding resource is created).
208
+ * @type {Record<string, RemoteFunctionDataNode>}
208
209
  */
209
- export let query_responses = {};
210
+ export const query_responses = {};
210
211
 
211
212
  /**
212
- * Data that was serialized during SSR for prerender functions.
213
+ * Data that was serialized during SSR for prerender functions, stored as
214
+ * `{ v }` (value) or `{ e }` (error) nodes.
213
215
  * This persists across client-side navigations.
214
- * @type {Record<string, any>}
216
+ * @type {Record<string, RemoteFunctionDataNode>}
215
217
  */
216
- export let prerender_responses = {};
218
+ export const prerender_responses = {};
217
219
 
218
220
  /** @type {Array<((url: URL) => boolean)>} */
219
221
  const invalidated = [];
@@ -328,9 +330,15 @@ export async function start(_app, _target, hydrate) {
328
330
  );
329
331
  }
330
332
 
331
- if (__SVELTEKIT_PAYLOAD__) {
332
- query_responses = __SVELTEKIT_PAYLOAD__.query ?? {};
333
- prerender_responses = __SVELTEKIT_PAYLOAD__.prerender ?? {};
333
+ if (__SVELTEKIT_PAYLOAD__.data) {
334
+ const { q = {}, p = {}, l = {}, f = {} } = __SVELTEKIT_PAYLOAD__.data;
335
+
336
+ // store the whole nodes — error records seed the corresponding
337
+ // resources in a failed state when they are created during hydration
338
+ for (const k in q) query_responses[k] = q[k];
339
+ for (const k in l) query_responses[k] = l[k];
340
+ for (const k in f) query_responses[k] = f[k];
341
+ for (const k in p) prerender_responses[k] = p[k];
334
342
  }
335
343
 
336
344
  // detect basic auth credentials in the current URL
@@ -3028,8 +3036,6 @@ async function _hydrate(
3028
3036
 
3029
3037
  target.textContent = '';
3030
3038
  hydrate = false;
3031
- } finally {
3032
- query_responses = {};
3033
3039
  }
3034
3040
 
3035
3041
  if (result.props.page) {