@sveltejs/kit 3.0.0-next.10 → 3.0.0-next.11

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 (51) hide show
  1. package/package.json +1 -1
  2. package/src/constants.js +2 -0
  3. package/src/core/adapt/builder.js +3 -6
  4. package/src/core/config/index.js +6 -1
  5. package/src/core/config/options.js +11 -69
  6. package/src/core/postbuild/fallback.js +2 -1
  7. package/src/core/postbuild/prerender.js +75 -28
  8. package/src/core/postbuild/queue.js +2 -1
  9. package/src/core/sync/write_server.js +2 -2
  10. package/src/core/utils.js +28 -3
  11. package/src/exports/env/index.js +68 -3
  12. package/src/exports/internal/env.js +6 -3
  13. package/src/exports/params.js +9 -4
  14. package/src/exports/public.d.ts +41 -20
  15. package/src/exports/vite/dev/index.js +50 -6
  16. package/src/exports/vite/index.js +130 -125
  17. package/src/exports/vite/module_ids.js +0 -2
  18. package/src/exports/vite/preview/index.js +3 -2
  19. package/src/exports/vite/utils.js +20 -0
  20. package/src/runtime/app/paths/server.js +1 -1
  21. package/src/runtime/app/server/index.js +1 -1
  22. package/src/runtime/app/server/remote/query.js +1 -1
  23. package/src/runtime/client/ndjson.js +1 -1
  24. package/src/runtime/client/stream.js +3 -2
  25. package/src/runtime/server/data/index.js +1 -1
  26. package/src/runtime/server/errors.js +135 -0
  27. package/src/runtime/server/fetch.js +1 -1
  28. package/src/runtime/server/index.js +20 -10
  29. package/src/runtime/server/internal.js +25 -0
  30. package/src/runtime/server/page/actions.js +2 -1
  31. package/src/runtime/server/page/data_serializer.js +10 -10
  32. package/src/runtime/server/page/index.js +3 -2
  33. package/src/runtime/server/page/load_data.js +1 -1
  34. package/src/runtime/server/page/render.js +3 -7
  35. package/src/runtime/server/page/respond_with_error.js +6 -5
  36. package/src/runtime/server/{remote.js → remote-functions.js} +1 -1
  37. package/src/runtime/server/respond.js +3 -7
  38. package/src/runtime/server/sourcemaps.js +183 -0
  39. package/src/runtime/server/utils.js +2 -157
  40. package/src/types/ambient-private.d.ts +2 -0
  41. package/src/types/global-private.d.ts +0 -5
  42. package/src/types/internal.d.ts +6 -6
  43. package/src/types/private.d.ts +8 -0
  44. package/src/utils/escape.js +9 -25
  45. package/src/utils/features.js +1 -1
  46. package/src/utils/fork.js +7 -2
  47. package/src/utils/page_nodes.js +3 -5
  48. package/src/version.js +1 -1
  49. package/types/index.d.ts +72 -22
  50. package/types/index.d.ts.map +3 -1
  51. package/src/runtime/shared-server.js +0 -7
@@ -58,7 +58,7 @@ export interface Adapter {
58
58
  * Test support for `read` from `$app/server`.
59
59
  * @param details.config The merged adapter-specific route config exported from the route with `export const config`
60
60
  */
61
- read?: (details: { config: any; route: { id: string } }) => boolean;
61
+ read?: (details: { config: Record<string, any>; route: { id: string } }) => boolean;
62
62
 
63
63
  /**
64
64
  * Test support for `instrumentation.server.js`. To pass, the adapter must support running `instrumentation.server.js` prior to the application code.
@@ -435,7 +435,7 @@ export interface KitConfig {
435
435
  *
436
436
  * > [!NOTE] When `mode` is `'auto'`, SvelteKit will use nonces for dynamically rendered pages and hashes for prerendered pages. Using nonces with prerendered pages is insecure and therefore forbidden.
437
437
  *
438
- * If this level of configuration is insufficient and you have more dynamic requirements, you can use the [`handle` hook](https://svelte.dev/docs/kit/hooks#Server-hooks-handle) to roll your own CSP.
438
+ * If this level of configuration is insufficient and you have more dynamic requirements, you can use the [`handle` hook](https://svelte.dev/docs/kit/hooks#handle) to roll your own CSP.
439
439
  */
440
440
  csp?: {
441
441
  /**
@@ -833,8 +833,10 @@ export interface KitConfig {
833
833
  * This has several advantages:
834
834
  * - The client does not need to load the routing manifest upfront, which can lead to faster initial page loads
835
835
  * - The list of routes is hidden from public view
836
- * - The server has an opportunity to intercept each navigation (for example through a middleware), enabling (for example) A/B testing opaque to SvelteKit
837
-
836
+ * - The server has an opportunity to intercept each navigation (for example through middleware in front of SvelteKit, such as a reverse proxy or your platform's edge functions), enabling (for example) A/B testing opaque to SvelteKit
837
+ *
838
+ * Route resolution requests are answered as soon as the route has been looked up, before the `handle` hook is invoked. To intercept them within SvelteKit itself, use the `reroute` hook, which runs for these requests too.
839
+ *
838
840
  * The drawback is that for unvisited paths, resolution will take slightly longer (though this is mitigated by [preloading](https://svelte.dev/docs/kit/link-options#data-sveltekit-preload-data)).
839
841
  *
840
842
  * > [!NOTE] When using server-side route resolution and prerendering, the resolution is prerendered along with the route itself.
@@ -876,7 +878,7 @@ export interface KitConfig {
876
878
  */
877
879
  tracing?: {
878
880
  /**
879
- * Enables server-side [OpenTelemetry](https://opentelemetry.io/) span emission for SvelteKit operations including the [`handle` hook](https://svelte.dev/docs/kit/hooks#Server-hooks-handle), [`load` functions](https://svelte.dev/docs/kit/load), [form actions](https://svelte.dev/docs/kit/form-actions), and [remote functions](https://svelte.dev/docs/kit/remote-functions). Tracing — and more significantly, observability instrumentation — can have a nontrivial overhead, so consider whether you really need it, or if it might be more appropriate to turn it on in development and preview environments only.
881
+ * Enables server-side [OpenTelemetry](https://opentelemetry.io/) span emission for SvelteKit operations including the [`handle` hook](https://svelte.dev/docs/kit/hooks#handle), [`load` functions](https://svelte.dev/docs/kit/load), [form actions](https://svelte.dev/docs/kit/form-actions), and [remote functions](https://svelte.dev/docs/kit/remote-functions). Tracing — and more significantly, observability instrumentation — can have a nontrivial overhead, so consider whether you really need it, or if it might be more appropriate to turn it on in development and preview environments only.
880
882
  * @default false
881
883
  */
882
884
  server?: boolean;
@@ -947,7 +949,7 @@ export interface KitConfig {
947
949
  }
948
950
 
949
951
  /**
950
- * The [`handle`](https://svelte.dev/docs/kit/hooks#Server-hooks-handle) hook runs every time the SvelteKit server receives a [request](https://svelte.dev/docs/kit/web-standards#Fetch-APIs-Request) and
952
+ * The [`handle`](https://svelte.dev/docs/kit/hooks#handle) hook runs every time the SvelteKit server receives a [request](https://svelte.dev/docs/kit/web-standards#Fetch-APIs-Request) and
951
953
  * determines the [response](https://svelte.dev/docs/kit/web-standards#Fetch-APIs-Response).
952
954
  * It receives an `event` object representing the request and a function called `resolve`, which renders the route and generates a `Response`.
953
955
  * This allows you to modify response headers or bodies, or bypass SvelteKit entirely (for implementing routes programmatically, for example).
@@ -958,7 +960,7 @@ export type Handle = (input: {
958
960
  }) => MaybePromise<Response>;
959
961
 
960
962
  /**
961
- * The server-side [`handleError`](https://svelte.dev/docs/kit/hooks#Shared-hooks-handleError) hook runs when an unexpected error is thrown while responding to a request.
963
+ * The server-side [`handleError`](https://svelte.dev/docs/kit/hooks#handleError) hook runs when an unexpected error is thrown while responding to a request.
962
964
  *
963
965
  * If an unexpected error is thrown during loading or rendering, this function will be called with the error and the event.
964
966
  * Make sure that this function _never_ throws an error.
@@ -974,7 +976,7 @@ export type HandleServerError = (input: {
974
976
  }) => MaybePromise<void | AppErrorWithOptionalStatus>;
975
977
 
976
978
  /**
977
- * The [`handleValidationError`](https://svelte.dev/docs/kit/hooks#Server-hooks-handleValidationError) hook runs when the argument to a remote function fails validation.
979
+ * The [`handleValidationError`](https://svelte.dev/docs/kit/hooks#handleValidationError) hook runs when the argument to a remote function fails validation.
978
980
  *
979
981
  * It will be called with the validation issues and the event, and must return an object shape that matches `App.Error`.
980
982
  */
@@ -982,7 +984,7 @@ export type HandleValidationError<Issue extends StandardSchemaV1.Issue = Standar
982
984
  (input: { issues: Issue[]; event: RequestEvent }) => MaybePromise<AppErrorWithOptionalStatus>;
983
985
 
984
986
  /**
985
- * The client-side [`handleError`](https://svelte.dev/docs/kit/hooks#Shared-hooks-handleError) hook runs when an unexpected error is thrown while navigating.
987
+ * The client-side [`handleError`](https://svelte.dev/docs/kit/hooks#handleError) hook runs when an unexpected error is thrown while navigating.
986
988
  *
987
989
  * If an unexpected error is thrown during loading or the following render, this function will be called with the error and the event.
988
990
  * Make sure that this function _never_ throws an error.
@@ -998,7 +1000,7 @@ export type HandleClientError = (input: {
998
1000
  }) => MaybePromise<void | AppErrorWithOptionalStatus>;
999
1001
 
1000
1002
  /**
1001
- * The [`handleFetch`](https://svelte.dev/docs/kit/hooks#Server-hooks-handleFetch) hook allows you to modify (or replace) the result of an [`event.fetch`](https://svelte.dev/docs/kit/load#Making-fetch-requests) call that runs on the server (or during prerendering) inside an endpoint, `load`, `action`, `handle`, `handleError` or `reroute`.
1003
+ * The [`handleFetch`](https://svelte.dev/docs/kit/hooks#handleFetch) hook allows you to modify (or replace) the result of an [`event.fetch`](https://svelte.dev/docs/kit/load#Making-fetch-requests) call that runs on the server (or during prerendering) inside an endpoint, `load`, `action`, `handle`, `handleError` or `reroute`.
1002
1004
  */
1003
1005
  export type HandleFetch = (input: {
1004
1006
  event: RequestEvent;
@@ -1007,25 +1009,25 @@ export type HandleFetch = (input: {
1007
1009
  }) => MaybePromise<Response>;
1008
1010
 
1009
1011
  /**
1010
- * The [`init`](https://svelte.dev/docs/kit/hooks#Shared-hooks-init) will be invoked before the server responds to its first request
1012
+ * The [`init`](https://svelte.dev/docs/kit/hooks#init) will be invoked before the server responds to its first request
1011
1013
  * @since 2.10.0
1012
1014
  */
1013
1015
  export type ServerInit = () => MaybePromise<void>;
1014
1016
 
1015
1017
  /**
1016
- * The [`init`](https://svelte.dev/docs/kit/hooks#Shared-hooks-init) will be invoked once the app starts in the browser
1018
+ * The [`init`](https://svelte.dev/docs/kit/hooks#init) will be invoked once the app starts in the browser
1017
1019
  * @since 2.10.0
1018
1020
  */
1019
1021
  export type ClientInit = () => MaybePromise<void>;
1020
1022
 
1021
1023
  /**
1022
- * The [`reroute`](https://svelte.dev/docs/kit/hooks#Universal-hooks-reroute) hook allows you to modify the URL before it is used to determine which route to render.
1024
+ * The [`reroute`](https://svelte.dev/docs/kit/hooks#reroute) hook allows you to modify the URL before it is used to determine which route to render.
1023
1025
  * @since 2.3.0
1024
1026
  */
1025
1027
  export type Reroute = (event: { url: URL; fetch: typeof fetch }) => MaybePromise<void | string>;
1026
1028
 
1027
1029
  /**
1028
- * The [`transport`](https://svelte.dev/docs/kit/hooks#Universal-hooks-transport) hook allows you to transport custom types across the server/client boundary.
1030
+ * The [`transport`](https://svelte.dev/docs/kit/hooks#transport) hook allows you to transport custom types across the server/client boundary.
1029
1031
  *
1030
1032
  * Each transporter has a pair of `encode` and `decode` functions. On the server, `encode` determines whether a value is an instance of the custom type and, if so, returns a non-falsy encoding of the value which can be an object or an array (or `false` otherwise).
1031
1033
  *
@@ -1051,7 +1053,7 @@ export type Reroute = (event: { url: URL; fetch: typeof fetch }) => MaybePromise
1051
1053
  export type Transport = Record<string, Transporter>;
1052
1054
 
1053
1055
  /**
1054
- * A member of the [`transport`](https://svelte.dev/docs/kit/hooks#Universal-hooks-transport) hook.
1056
+ * A member of the [`transport`](https://svelte.dev/docs/kit/hooks#transport) hook.
1055
1057
  */
1056
1058
  export interface Transporter<
1057
1059
  T = any,
@@ -1089,7 +1091,7 @@ export interface LoadEvent<
1089
1091
  * - It can be used to make credentialed requests on the server, as it inherits the `cookie` and `authorization` headers for the page request.
1090
1092
  * - It can make relative requests on the server (ordinarily, `fetch` requires a URL with an origin when used in a server context).
1091
1093
  * - Internal requests (e.g. for `+server.js` routes) go directly to the handler function when running on the server, without the overhead of an HTTP call.
1092
- * - During server-side rendering, the response will be captured and inlined into the rendered HTML by hooking into the `text` and `json` methods of the `Response` object. Note that headers will _not_ be serialized, unless explicitly included via [`filterSerializedResponseHeaders`](https://svelte.dev/docs/kit/hooks#Server-hooks-handle)
1094
+ * - During server-side rendering, the response will be captured and inlined into the rendered HTML by hooking into the `text` and `json` methods of the `Response` object. Note that headers will _not_ be serialized, unless explicitly included via [`filterSerializedResponseHeaders`](https://svelte.dev/docs/kit/hooks#handle)
1093
1095
  * - During hydration, the response will be read from the HTML, guaranteeing consistency and preventing an additional network request.
1094
1096
  *
1095
1097
  * You can learn more about making credentialed requests with cookies [here](https://svelte.dev/docs/kit/load#Cookies)
@@ -1596,7 +1598,7 @@ export interface RequestEvent<
1596
1598
  * - It can be used to make credentialed requests on the server, as it inherits the `cookie` and `authorization` headers for the page request.
1597
1599
  * - It can make relative requests on the server (ordinarily, `fetch` requires a URL with an origin when used in a server context).
1598
1600
  * - Internal requests (e.g. for `+server.js` routes) go directly to the handler function when running on the server, without the overhead of an HTTP call.
1599
- * - During server-side rendering, the response will be captured and inlined into the rendered HTML by hooking into the `text` and `json` methods of the `Response` object. Note that headers will _not_ be serialized, unless explicitly included via [`filterSerializedResponseHeaders`](https://svelte.dev/docs/kit/hooks#Server-hooks-handle)
1601
+ * - During server-side rendering, the response will be captured and inlined into the rendered HTML by hooking into the `text` and `json` methods of the `Response` object. Note that headers will _not_ be serialized, unless explicitly included via [`filterSerializedResponseHeaders`](https://svelte.dev/docs/kit/hooks#handle)
1600
1602
  * - During hydration, the response will be read from the HTML, guaranteeing consistency and preventing an additional network request.
1601
1603
  *
1602
1604
  * You can learn more about making credentialed requests with cookies [here](https://svelte.dev/docs/kit/load#Cookies).
@@ -1607,7 +1609,7 @@ export interface RequestEvent<
1607
1609
  */
1608
1610
  getClientAddress: () => string;
1609
1611
  /**
1610
- * Contains custom data that was added to the request within the [`server handle hook`](https://svelte.dev/docs/kit/hooks#Server-hooks-handle).
1612
+ * Contains custom data that was added to the request within the [`server handle hook`](https://svelte.dev/docs/kit/hooks#handle).
1611
1613
  */
1612
1614
  locals: App.Locals;
1613
1615
  /**
@@ -2454,16 +2456,35 @@ export interface EnvVarConfig<T> {
2454
2456
  static?: boolean;
2455
2457
  /**
2456
2458
  * A [Standard Schema](https://standardschema.dev/) validator that is applied to the value when the app starts.
2459
+ * Alternatively, a function that returns the (possibly transformed) value, or throws an error explaining
2460
+ * the problem. Returning `undefined` is valid, so a function can describe an optional variable.
2457
2461
  * The validator can output any value — not necessarily a string — but public, non-static values must be
2458
2462
  * serializable by [devalue](https://github.com/sveltejs/devalue) so that they can be sent to the browser.
2459
2463
  *
2460
- * If omitted, the value must be a non-empty string.
2464
+ * If omitted, the value must be set, but may be an empty string.
2461
2465
  */
2462
- schema?: StandardSchemaV1<string | undefined, T>;
2466
+ schema?: StandardSchemaV1<string | undefined, T> | ((value: string | undefined) => T | undefined);
2463
2467
  /**
2464
2468
  * A description of the variable that will be used for inline documentation on hover.
2465
2469
  */
2466
2470
  description?: string;
2467
2471
  }
2468
2472
 
2473
+ /**
2474
+ * The return type of [`defineEnvVars`](https://svelte.dev/docs/kit/@sveltejs-kit-env#defineEnvVars).
2475
+ */
2476
+ export type DefinedEnvVars<T extends Record<string, EnvVarConfig<any>>> = {
2477
+ readonly [K in keyof T]: EnvVarEntry<T[K]>;
2478
+ };
2479
+
2480
+ /**
2481
+ * Normalizes an environment variable config's schema (standard schema or function) to standard schema.
2482
+ */
2483
+ type EnvVarEntry<C extends EnvVarConfig<any>> =
2484
+ C['schema'] extends StandardSchemaV1<any, any>
2485
+ ? C
2486
+ : C['schema'] extends (value: any) => infer R
2487
+ ? Omit<C, 'schema'> & { schema: StandardSchemaV1<string | undefined, R> }
2488
+ : C;
2489
+
2469
2490
  export * from './index.js';
@@ -1,5 +1,6 @@
1
1
  /** @import { RequestEvent } from '@sveltejs/kit' */
2
2
  /** @import { PrerenderOption, UniversalNode } from 'types' */
3
+ import process from 'node:process';
3
4
  import fs from 'node:fs';
4
5
  import path from 'node:path';
5
6
  import { URL } from 'node:url';
@@ -14,12 +15,17 @@ import { load_and_validate_params } from '../../../utils/params.js';
14
15
  import { from_fs, to_fs } from '../../../utils/vite.js';
15
16
  import { posixify } from '../../../utils/os.js';
16
17
  import { load_error_page } from '../../../core/config/index.js';
17
- import { SVELTE_KIT_ASSETS } from '../../../constants.js';
18
+ import { SRC_ROOT, SVELTE_KIT_ASSETS } from '../../../constants.js';
18
19
  import * as sync from '../../../core/sync/sync.js';
19
20
  import { get_mime_lookup, get_runtime_base } from '../../../core/utils.js';
20
21
  import '../../../utils/mime.js'; // extend mrmime with additional types (affects sirv too)
21
22
  import { compact } from '../../../utils/array.js';
22
- import { is_chrome_devtools_request, not_found } from '../utils.js';
23
+ import {
24
+ is_chrome_devtools_request,
25
+ log_response,
26
+ not_found,
27
+ remote_module_pattern
28
+ } from '../utils.js';
23
29
  import { SCHEME } from '../../../utils/url.js';
24
30
  import { check_feature } from '../../../utils/features.js';
25
31
  import { escape_html } from '../../../utils/escape.js';
@@ -324,13 +330,48 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
324
330
 
325
331
  /** @param {Error} error */
326
332
  function fix_stack_trace(error) {
333
+ if (!error.stack) {
334
+ return;
335
+ }
336
+
327
337
  try {
328
338
  vite.ssrFixStacktrace(error);
329
339
  } catch {
330
340
  // ssrFixStacktrace can fail on StackBlitz web containers and we don't know why
331
341
  // by ignoring it the line numbers are wrong, but at least we can show the error
332
342
  }
333
- return error.stack?.replaceAll('\0', ''); // remove null bytes from e.g. virtual module IDs, or the response will fail
343
+
344
+ if (error.stack) {
345
+ let end = 0;
346
+
347
+ error.stack = error.stack
348
+ .replaceAll('\0', '') // remove null bytes from e.g. virtual module IDs, or the response will fail
349
+ .split('\n')
350
+ .map((line, i) => {
351
+ const match = /^ {4}at (?:[^ ]+ \((.+)\)|(.+))$/.exec(line);
352
+ if (!match) {
353
+ end = i + 1;
354
+ return line;
355
+ }
356
+
357
+ const loc = match[1] ?? match[2];
358
+ const file = loc.replace(/:\d+:\d+$/, '');
359
+
360
+ if (fs.existsSync(file)) {
361
+ if (!file.includes('node_modules') && !file.includes(SRC_ROOT)) {
362
+ end = i + 1;
363
+ }
364
+
365
+ return line.replace(file, path.posix.relative(process.cwd(), file));
366
+ }
367
+
368
+ return line;
369
+ })
370
+ .slice(0, end)
371
+ .join('\n');
372
+
373
+ return error.stack;
374
+ }
334
375
  }
335
376
 
336
377
  await update_manifest();
@@ -346,7 +387,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
346
387
  if (
347
388
  file.startsWith(svelte_config.kit.files.routes + path.sep) ||
348
389
  (params_file && file === params_file) ||
349
- svelte_config.kit.moduleExtensions.some((ext) => file.endsWith(`.remote${ext}`)) ||
390
+ remote_module_pattern.test(file) ||
350
391
  // in contrast to server hooks, client hooks are written to the client manifest
351
392
  // and therefore need rebuilding when they are added/removed
352
393
  file.startsWith(svelte_config.kit.files.hooks.client)
@@ -511,7 +552,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
511
552
  );
512
553
 
513
554
  const { set_fix_stack_trace } = await vite.ssrLoadModule(
514
- `${get_runtime_base(root)}/shared-server.js`
555
+ `${get_runtime_base(root)}/server/internal.js`
515
556
  );
516
557
  set_fix_stack_trace(fix_stack_trace);
517
558
 
@@ -576,15 +617,18 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
576
617
  if (rendered.status === 404) {
577
618
  // @ts-expect-error
578
619
  serve_static_middleware.handle(req, res, () => {
620
+ log_response(rendered.status, request);
579
621
  setResponse(res, rendered);
580
622
  });
581
623
  } else {
624
+ log_response(rendered.status, request);
582
625
  setResponse(res, rendered);
583
626
  }
584
627
  } catch (e) {
585
628
  const error = coalesce_to_error(e);
586
629
  res.statusCode = 500;
587
- res.end(fix_stack_trace(error) || error.message); // handle `stackless` errors
630
+ fix_stack_trace(error);
631
+ res.end(error.stack || error.message); // handle `stackless` errors
588
632
  }
589
633
  });
590
634
  };