@sveltejs/kit 3.0.0-next.24 → 3.0.0-next.27

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 (71) hide show
  1. package/package.json +16 -10
  2. package/src/constants.js +6 -0
  3. package/src/core/adapt/builder.js +128 -33
  4. package/src/core/adapt/index.js +3 -0
  5. package/src/core/config/index.js +4 -7
  6. package/src/core/env.js +39 -14
  7. package/src/core/generate_manifest/index.js +42 -43
  8. package/src/core/postbuild/analyse.js +4 -4
  9. package/src/core/postbuild/fallback.js +1 -1
  10. package/src/core/postbuild/prerender.js +4 -4
  11. package/src/core/sync/create_manifest_data/index.js +0 -1
  12. package/src/core/sync/write_app_manifest.js +3 -2
  13. package/src/core/sync/write_server.js +3 -17
  14. package/src/core/utils.js +10 -0
  15. package/src/exports/adapter.js +22 -0
  16. package/src/exports/public.d.ts +101 -49
  17. package/src/exports/vite/build/index.js +1173 -0
  18. package/src/exports/vite/build/remote.js +4 -4
  19. package/src/exports/vite/build/service-worker.js +98 -0
  20. package/src/exports/vite/dev/generate_manifest.js +308 -0
  21. package/src/exports/vite/dev/index.js +40 -282
  22. package/src/exports/vite/index.js +156 -1717
  23. package/src/exports/vite/plugins/env-vars.js +53 -34
  24. package/src/exports/vite/plugins/guard.js +217 -0
  25. package/src/exports/vite/plugins/remote.js +237 -0
  26. package/src/exports/vite/preview/index.js +8 -8
  27. package/src/exports/vite/static_analysis/index.js +15 -15
  28. package/src/exports/vite/utils.js +98 -1
  29. package/src/runner.js +4 -3
  30. package/src/runtime/app/paths/server.js +2 -2
  31. package/src/runtime/app/server/index.js +3 -3
  32. package/src/runtime/app/server/public.d.ts +114 -57
  33. package/src/runtime/app/server/remote/query.js +2 -2
  34. package/src/runtime/app/server/remote/requested.js +31 -15
  35. package/src/runtime/app/state/client.svelte.js +3 -1
  36. package/src/runtime/client/client.js +43 -198
  37. package/src/runtime/client/fetcher.js +6 -6
  38. package/src/runtime/client/focus.js +120 -0
  39. package/src/runtime/client/remote-functions/command.svelte.js +20 -9
  40. package/src/runtime/client/remote-functions/form.svelte.js +12 -7
  41. package/src/runtime/client/remote-functions/query/instance.svelte.js +19 -5
  42. package/src/runtime/client/remote-functions/shared.svelte.js +22 -1
  43. package/src/runtime/client/scroll.js +39 -0
  44. package/src/runtime/client/utils.js +28 -0
  45. package/src/runtime/form-utils.js +254 -264
  46. package/src/runtime/server/data/index.js +14 -36
  47. package/src/runtime/server/errors.js +7 -10
  48. package/src/runtime/server/fetch.js +21 -25
  49. package/src/runtime/server/index.js +35 -31
  50. package/src/runtime/server/internal.js +34 -5
  51. package/src/runtime/server/page/actions.js +6 -8
  52. package/src/runtime/server/page/data_serializer.js +6 -10
  53. package/src/runtime/server/page/index.js +18 -28
  54. package/src/runtime/server/page/load_data.js +2 -2
  55. package/src/runtime/server/page/render.js +22 -40
  56. package/src/runtime/server/page/respond_with_error.js +10 -13
  57. package/src/runtime/server/page/server_routing.js +21 -27
  58. package/src/runtime/server/remote-functions.js +136 -136
  59. package/src/runtime/server/respond.js +66 -64
  60. package/src/runtime/server/state.js +2 -0
  61. package/src/runtime/server/utils.js +4 -11
  62. package/src/runtime/shared.js +9 -0
  63. package/src/runtime/utils.js +41 -0
  64. package/src/types/ambient-private.d.ts +1 -2
  65. package/src/types/global-private.d.ts +8 -0
  66. package/src/types/internal.d.ts +56 -17
  67. package/src/utils/filesystem.js +44 -28
  68. package/src/utils/streaming.js +5 -15
  69. package/src/version.js +1 -1
  70. package/types/index.d.ts +235 -93
  71. package/types/index.d.ts.map +9 -2
@@ -48,11 +48,11 @@ async function prerender({
48
48
  vite_config_file,
49
49
  is_tty
50
50
  }) {
51
- /** @type {import('@sveltejs/kit').SSRManifest} */
51
+ /** @type {import('types').SSRManifest} */
52
52
  const manifest = (await import(pathToFileURL(manifest_path).href)).manifest;
53
53
 
54
54
  /** @type {import('types').ServerInternalModule} */
55
- const { set_building, set_prerendering, set_manifest, set_read_implementation, log_response } =
55
+ const { set_building, set_prerendering, set_manifest, set_read_implementation, format_response } =
56
56
  await import(pathToFileURL(`${out}/server/internal.js`).href);
57
57
 
58
58
  // configure `import { building } from `$app/env` —
@@ -424,7 +424,7 @@ async function prerender({
424
424
  }
425
425
 
426
426
  if (response.status >= 400) {
427
- log_response(response.status, request);
427
+ console.log(format_response(response.status, request));
428
428
  }
429
429
 
430
430
  const body = Buffer.from(await response.arrayBuffer());
@@ -659,7 +659,7 @@ async function prerender({
659
659
  /** @type {Array<import('types').RemotePrerenderInternals>} */
660
660
  const prerender_functions = [];
661
661
 
662
- for (const loader of Object.values(manifest._.remotes)) {
662
+ for (const loader of Object.values(manifest.remotes)) {
663
663
  const module = await loader();
664
664
 
665
665
  for (const fn of Object.values(module.default)) {
@@ -82,7 +82,6 @@ export function create_assets(config) {
82
82
 
83
83
  return [...walk(config.files.assets)].map((file) => ({
84
84
  file,
85
- size: fs.statSync(path.resolve(config.files.assets, file)).size,
86
85
  type: lookup(file) || null
87
86
  }));
88
87
  }
@@ -1,4 +1,5 @@
1
1
  /** @import { Asset, ManifestData, RouteData } from 'types' */
2
+ /** @import { ManifestRoute } from '$app/manifest' */
2
3
 
3
4
  import { s } from '../../utils/misc.js';
4
5
  import { is_app_route, is_endpoint_route, is_page_route } from './create_manifest_data/index.js';
@@ -60,11 +61,11 @@ function stringify_assets(assets) {
60
61
 
61
62
  /**
62
63
  * @param {RouteData[] | undefined} routes
63
- * @returns {Array<{ id: string; page: boolean; endpoint: boolean }>}
64
+ * @returns {ManifestRoute[]}
64
65
  */
65
66
  export function get_manifest_routes(routes) {
66
67
  return (
67
- routes?.filter(is_app_route).map((route) => ({
68
+ routes?.filter(is_app_route).map((route) => /** @type {ManifestRoute} */ ({
68
69
  id: route.id,
69
70
  page: is_page_route(route),
70
71
  endpoint: is_endpoint_route(route)
@@ -1,5 +1,4 @@
1
1
  import path from 'node:path';
2
- import { hash } from '../../utils/hash.js';
3
2
  import { resolve_entry } from '../../utils/filesystem.js';
4
3
  import { posixify } from '../../utils/os.js';
5
4
  import { s } from '../../utils/misc.js';
@@ -13,7 +12,6 @@ import { runtime_directory } from '../utils.js';
13
12
  * server_hooks: string | null;
14
13
  * universal_hooks: string | null;
15
14
  * config: import('types').ValidatedConfig;
16
- * has_service_worker: boolean;
17
15
  * template: string;
18
16
  * runtime_directory: string;
19
17
  * }} opts
@@ -22,26 +20,18 @@ const server_template = ({
22
20
  config,
23
21
  server_hooks,
24
22
  universal_hooks,
25
- has_service_worker,
26
23
  template,
27
24
  runtime_directory
28
25
  }) => `
29
26
  import { set_building, set_prerendering } from '$app/env/server';
30
27
  import { set_assets } from '$app/paths/internal/server';
31
- import { set_fix_stack_trace, set_manifest, set_read_implementation, log_response } from '${runtime_directory}/server/internal.js';
28
+ import { set_fix_stack_trace, set_manifest, set_read_implementation, format_response } from '${runtime_directory}/server/internal.js';
32
29
  import error from './shared/error-template.js';
33
30
 
34
31
  export const options = {
35
32
  app_template_contains_nonce: ${template.includes('%sveltekit.nonce%')},
36
33
  csp: ${s(config.csp)},
37
- csrf_check_origin: ${s(!config.csrf.trustedOrigins.includes('*'))},
38
34
  csrf_trusted_origins: ${s(config.csrf.trustedOrigins)},
39
- embedded: ${config.embedded},
40
- hash_routing: ${s(config.router.type === 'hash')},
41
- hooks: null, // added lazily, via \`get_hooks\`
42
- link_header_preload: ${s(config.output.linkHeaderPreload)},
43
- paths_origin: ${s(config.paths.origin)},
44
- service_worker: ${has_service_worker},
45
35
  service_worker_options: ${config.serviceWorker.register ? s(config.serviceWorker.options) : 'null'},
46
36
  templates: {
47
37
  app: ({ head, body, assets, nonce, env }) => ${s(template)
@@ -55,9 +45,7 @@ export const options = {
55
45
  (_match, capture) => `" + (env[${s(capture)}] ?? "") + "`
56
46
  )},
57
47
  error
58
- },
59
- version: ${s(config.version.name)},
60
- version_hash: ${s(hash(config.version.name))}
48
+ }
61
49
  };
62
50
 
63
51
  export async function get_hooks() {
@@ -81,7 +69,7 @@ export async function get_hooks() {
81
69
  };
82
70
  }
83
71
 
84
- export { set_assets, set_building, set_fix_stack_trace, set_manifest, set_prerendering, set_read_implementation, log_response };
72
+ export { set_assets, set_building, set_fix_stack_trace, set_manifest, set_prerendering, set_read_implementation, format_response };
85
73
  `;
86
74
 
87
75
  /**
@@ -125,8 +113,6 @@ export function write_server(config, output, root) {
125
113
  runtime_directory: relative(runtime_directory),
126
114
  server_hooks: server_hooks_file ? relative(server_hooks_file) : null,
127
115
  universal_hooks: universal_hooks_file ? relative(universal_hooks_file) : null,
128
- has_service_worker:
129
- config.serviceWorker.register && !!resolve_entry(config.files.serviceWorker),
130
116
  template: load_template(root, config)
131
117
  })
132
118
  );
package/src/core/utils.js CHANGED
@@ -4,6 +4,7 @@ import { styleText } from 'node:util';
4
4
  import { to_fs } from '../utils/vite.js';
5
5
  import { noop } from '../utils/functions.js';
6
6
  import { posixify } from '../utils/os.js';
7
+ import { hash } from '../utils/hash.js';
7
8
 
8
9
  /**
9
10
  * Resolved path of the `runtime` directory posix-ified
@@ -15,6 +16,15 @@ import { posixify } from '../utils/os.js';
15
16
  */
16
17
  export const runtime_directory = posixify(fileURLToPath(new URL('../runtime', import.meta.url)));
17
18
 
19
+ /**
20
+ * The name of the `globalThis.__sveltekit_xxx` object the app's payload is attached to
21
+ * @param {string} version_name
22
+ * @param {boolean} dev
23
+ */
24
+ export function get_global_name(version_name, dev) {
25
+ return dev ? '__sveltekit_dev' : `__sveltekit_${hash(version_name)}`;
26
+ }
27
+
18
28
  /**
19
29
  * This allows us to import SvelteKit internals that aren't exposed via `pkg.exports` in a
20
30
  * way that works whether `@sveltejs/kit` is installed inside the project's `node_modules`
@@ -0,0 +1,22 @@
1
+ import { REROUTED_URL_HEADER } from '../constants.js';
2
+
3
+ /**
4
+ * Helps a catch-all request handler pass the request to a different handler if
5
+ * the `reroute` hook has returned a URL pathname that's different from the
6
+ * incoming request.
7
+ *
8
+ * If your adapter is capable of deploying multiple serverless functions, it's a
9
+ * good idea to also deploy a "catch-all" one to handle uncaught requests.
10
+ * Running this in that function allows the app's `reroute` hook to rewrite
11
+ * the request URL and invoke the next appropriate serverless function, if any.
12
+ * @param {Response} response The response returned from the SvelteKit `server.respond` function
13
+ * @param {(url: URL) => Response | Promise<Response>} next Your platform-specific implementation for invoking the next handler with a different request URL
14
+ * @returns {Response | Promise<Response>}
15
+ * @since 3.0.0
16
+ */
17
+ export function applyReroute(response, next) {
18
+ const rerouted_url = response.headers.get(REROUTED_URL_HEADER);
19
+ if (!rerouted_url) return response;
20
+
21
+ return next(new URL(rerouted_url));
22
+ }
@@ -12,11 +12,11 @@ import {
12
12
  RequestOptions,
13
13
  RouteSegment
14
14
  } from '../types/private.js';
15
- import { BuildData, SSRNodeLoader, SSRRoute, ValidatedConfig } from 'types';
15
+ import { ValidatedConfig } from 'types';
16
16
  import { Plugin } from 'vite';
17
17
  import { RouteId as AppRouteId, LayoutParams as AppLayoutParams } from '$app/types';
18
- import { ParamMatcher } from '@sveltejs/kit/params';
19
18
  import { StandardSchemaV1 } from '@standard-schema/spec';
19
+ import { getRequest, setResponse } from '@sveltejs/kit/node';
20
20
 
21
21
  export { PrerenderOption } from '../types/private.js';
22
22
 
@@ -57,20 +57,47 @@ export interface Adapter {
57
57
  * during dev, build and prerendering.
58
58
  */
59
59
  emulate?: () => MaybePromise<Emulator>;
60
- vite?: {
61
- plugins?: {
62
- /**
63
- * Vite plugins placed before any of SvelteKit's own plugins.
64
- * @since 3.0.0
65
- */
66
- pre?: Plugin[];
67
- /**
68
- * Vite plugins placed after any of SvelteKit's own plugins.
69
- * @since 3.0.0
70
- */
71
- post?: Plugin[];
72
- };
73
- };
60
+ /**
61
+ * Options for configuring and interacting with Vite
62
+ * @since 3.0.0
63
+ */
64
+ vite?: AdapterViteConfig | ((ctx: { config: ValidatedConfig }) => AdapterViteConfig);
65
+ }
66
+
67
+ export interface AdapterViteConfig {
68
+ /**
69
+ * This function overrides the default behavior during Vite's dev and preview modes
70
+ * to convert an `http.IncomingMessage` to a `Request` object.
71
+ * To call the original `setRequest` function, import it from `@sveltejs/kit/node`.
72
+ * @since 3.0.0
73
+ */
74
+ getRequest?: typeof getRequest;
75
+ /**
76
+ * This function overrides the default behavior in Vite's dev and preview modes
77
+ * to write a `Response` object to a `http.ServerResponse`.
78
+ * To call the original `setResponse` function, import it from `@sveltejs/kit/node`.
79
+ * @since 3.0.0
80
+ */
81
+ setResponse?: typeof setResponse;
82
+ /**
83
+ * Vite plugins injected by the adapter. By default,
84
+ * they are placed before SvelteKit's plugins.
85
+ * @since 3.0.0
86
+ */
87
+ plugins?:
88
+ | Plugin[]
89
+ | {
90
+ /**
91
+ * Vite plugins placed before any of SvelteKit's own plugins.
92
+ * @since 3.0.0
93
+ */
94
+ pre?: Plugin[];
95
+ /**
96
+ * Vite plugins placed after any of SvelteKit's own plugins.
97
+ * @since 3.0.0
98
+ */
99
+ post?: Plugin[];
100
+ };
74
101
  }
75
102
 
76
103
  export type LoadProperties<input extends Record<string, any> | void> = input extends void
@@ -139,6 +166,17 @@ export interface Builder {
139
166
  prerendered: Prerendered;
140
167
  /** An array of all routes (including prerendered) */
141
168
  routes: RouteDefinition[];
169
+ /**
170
+ * The value of the `$app/manifest` module.
171
+ * The only difference is `manifest.assets` also includes the service worker, if it exists.
172
+ * @since 3.0.0
173
+ */
174
+ manifest: typeof import('$app/manifest');
175
+ /**
176
+ * A record of file extensions to MIME types
177
+ * @since 3.0.0
178
+ */
179
+ mimeTypes: Record<string, string>;
142
180
 
143
181
  /**
144
182
  * Create separate functions that map to one or more routes of your app.
@@ -165,9 +203,10 @@ export interface Builder {
165
203
  /**
166
204
  * Generate a server-side manifest to initialise the SvelteKit [server](https://svelte.dev/docs/kit/@sveltejs-kit#Server) with.
167
205
  * @param opts
168
- * @param opts.relativePath A relative path to the base directory of the server build output
206
+ * @param opts.relativePath A relative path to the base directory of the server build output
207
+ * @deprecated removed in 3.0. Use `builder.generateServerInstance` or `builder.manifest` instead
169
208
  */
170
- generateManifest: (opts: { relativePath: string; routes?: RouteDefinition[] }) => string;
209
+ generateManifest?: (opts: { relativePath: string; routes?: RouteDefinition[] }) => string;
171
210
 
172
211
  /**
173
212
  * Resolve a path to the `name` directory inside `outDir`, e.g. `/path/to/.svelte-kit/my-adapter`.
@@ -181,6 +220,20 @@ export interface Builder {
181
220
  /** Get the application path including any configured `base` path, e.g. `my-base-path/_app`. */
182
221
  getAppPath: () => string;
183
222
 
223
+ /**
224
+ * Generates a module exposing a SvelteKit [Server](https://svelte.dev/docs/kit/@sveltejs-kit#Server) instance.
225
+ * @param dest
226
+ * @param opts.routes A subset of the routes to include in the server's manifest
227
+ * @param opts.serverDirectory The directory containing the server code. Defaults to `getServerDirectory()`.
228
+ * @since 3.0.0
229
+ */
230
+ generateServerInstance: (
231
+ dest: string,
232
+ opts?: {
233
+ routes?: RouteDefinition[];
234
+ serverDirectory?: string;
235
+ }
236
+ ) => void;
184
237
  /**
185
238
  * Write client assets to `dest`.
186
239
  * @param dest the destination folder
@@ -199,6 +252,23 @@ export interface Builder {
199
252
  * @returns an array of files written to `dest`
200
253
  */
201
254
  writeServer: (dest: string) => string[];
255
+
256
+ /**
257
+ * Generate an initializer that populates `$env/dynamic/private` before server instrumentation
258
+ * runs. Include the returned module in any subsequent bundling or tracing step.
259
+ * @param options an object containing the following properties:
260
+ * @param options.outputDirectory the directory in which to create the initializer.
261
+ * @param options.environment the contents of a module whose default export contains the platform's environment variables. If omitted, `process.env` is used.
262
+ * @param options.serverDirectory the directory containing the server build output. Defaults to `getServerDirectory()`.
263
+ * @returns the filesystem path to the generated initializer.
264
+ * @since 3.0.0
265
+ */
266
+ createInstrumentationInitializer: (options: {
267
+ outputDirectory: string;
268
+ environment?: string;
269
+ serverDirectory?: string;
270
+ }) => string;
271
+
202
272
  /**
203
273
  * Copy a file or directory.
204
274
  * @param from the source file or directory
@@ -230,6 +300,9 @@ export interface Builder {
230
300
  * `entrypoint` which imports `instrumentation` and then dynamically imports `start`. This allows
231
301
  * the module hooks necessary for instrumentation libraries to be loaded prior to any application code.
232
302
  *
303
+ * `initializer` is a module generated by `createInstrumentationInitializer`. It must be included
304
+ * in any bundling or tracing step before calling this method.
305
+ *
233
306
  * Caveats:
234
307
  * - "Live exports" will not work. If your adapter uses live exports, your users will need to manually import the server instrumentation on startup.
235
308
  * - If `tla` is `false`, OTEL auto-instrumentation may not work properly. Use it if your environment supports it.
@@ -239,21 +312,27 @@ export interface Builder {
239
312
  * @param options.entrypoint the path to the entrypoint to trace.
240
313
  * @param options.instrumentation the path to the instrumentation file.
241
314
  * @param options.start the name of the start file. This is what `entrypoint` will be renamed to.
315
+ * @param options.initializer the filesystem path to the bundled or copied instrumentation initializer.
242
316
  * @param options.module configuration for the resulting entrypoint module.
243
317
  * @param options.module.exports
244
- * @param options.module.generateText a function that receives the relative paths to the instrumentation and start files, and generates the text of the module to be traced. If not provided, the default implementation will be used, which uses top-level await.
245
- * @since 2.31.0
318
+ * @param options.module.generateText a function that receives the relative paths to the initializer, instrumentation and start files, and generates the text of the module to be traced. It must import `initializer` before `instrumentation`, and dynamically import `start` after instrumentation has run. If not provided, the default implementation will be used, which uses top-level await.
319
+ * @since 3.0.0
246
320
  */
247
321
  instrument: (args: {
248
322
  entrypoint: string;
249
323
  instrumentation: string;
250
324
  start?: string;
325
+ initializer: string;
251
326
  module?:
252
327
  | {
253
328
  exports: string[];
254
329
  }
255
330
  | {
256
- generateText: (args: { instrumentation: string; start: string }) => string;
331
+ generateText: (args: {
332
+ instrumentation: string;
333
+ start: string;
334
+ initializer: string;
335
+ }) => string;
257
336
  };
258
337
  }) => void;
259
338
 
@@ -656,8 +735,7 @@ export interface RouteDefinition<Config = any> {
656
735
  config: Config;
657
736
  }
658
737
 
659
- export class Server {
660
- constructor(manifest: SSRManifest);
738
+ export interface Server {
661
739
  init(options: ServerInitOptions): Promise<void>;
662
740
  respond(request: Request, options: RequestOptions): Promise<Response>;
663
741
  }
@@ -669,32 +747,6 @@ export interface ServerInitOptions {
669
747
  read?: (file: string) => MaybePromise<ReadableStream | null>;
670
748
  }
671
749
 
672
- /**
673
- * Information required to instantiate a new `Server` instance.
674
- */
675
- export interface SSRManifest {
676
- /** The directory where SvelteKit keeps its stuff, including static assets (such as JS and CSS) and internally-used routes. */
677
- appDir: string;
678
- /** The `base` and `appDir` settings combined without a leading slash. */
679
- appPath: string;
680
- /** Static files from `config.files.assets` and the service worker (if any). */
681
- assets: Set<string>;
682
- mimeTypes: Record<string, string>;
683
-
684
- /** @internal private fields */
685
- _: {
686
- client: BuildData['client'];
687
- nodes: SSRNodeLoader[];
688
- /** hashed filename -> import to that file */
689
- remotes: Record<string, () => Promise<{ default: Record<string, any> }>>;
690
- routes: SSRRoute[];
691
- prerendered_routes: Set<string>;
692
- matchers: () => Promise<Record<string, ParamMatcher>>;
693
- /** A `[file]: size` map of all assets imported by server code. */
694
- server_assets: Record<string, number>;
695
- };
696
- }
697
-
698
750
  /**
699
751
  * The generic form of `PageServerLoad` and `LayoutServerLoad`. You should import those from `./$types` (see [generated types](https://svelte.dev/docs/kit/types#Generated-types))
700
752
  * rather than using `ServerLoad` directly.