@sveltejs/kit 3.0.0-next.2 → 3.0.0-next.21

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 (195) hide show
  1. package/package.json +62 -36
  2. package/src/cli.js +15 -12
  3. package/src/constants.js +6 -1
  4. package/src/core/adapt/builder.js +66 -72
  5. package/src/core/adapt/index.js +17 -6
  6. package/src/core/config/index.js +134 -80
  7. package/src/core/config/options.js +295 -273
  8. package/src/core/config/types.d.ts +1 -1
  9. package/src/core/env.js +146 -16
  10. package/src/core/generate_manifest/index.js +18 -15
  11. package/src/core/postbuild/analyse.js +8 -20
  12. package/src/core/postbuild/crawl.js +22 -6
  13. package/src/core/postbuild/entities.js +8 -2
  14. package/src/core/postbuild/fallback.js +4 -2
  15. package/src/core/postbuild/prerender.js +222 -70
  16. package/src/core/postbuild/queue.js +2 -1
  17. package/src/core/sync/create_manifest_data/conflict.js +1 -1
  18. package/src/core/sync/create_manifest_data/index.js +82 -62
  19. package/src/core/sync/sync.js +40 -31
  20. package/src/core/sync/ts.js +1 -1
  21. package/src/core/sync/utils.js +22 -2
  22. package/src/core/sync/{write_non_ambient.js → write_app_types.js} +107 -58
  23. package/src/core/sync/write_client_manifest.js +14 -29
  24. package/src/core/sync/write_env.js +7 -5
  25. package/src/core/sync/write_server.js +37 -45
  26. package/src/core/sync/write_tsconfig/index.js +274 -0
  27. package/src/core/sync/write_tsconfig/test-app/package.json +7 -0
  28. package/src/core/sync/write_tsconfig/utils.js +77 -0
  29. package/src/core/sync/write_tsconfig/validate.js +128 -0
  30. package/src/core/sync/write_types/index.js +127 -123
  31. package/src/core/utils.js +30 -5
  32. package/src/exports/env/index.js +77 -0
  33. package/src/exports/env/public.d.ts +55 -0
  34. package/src/exports/hooks/index.js +3 -9
  35. package/src/exports/hooks/public.d.ts +195 -0
  36. package/src/exports/hooks/sequence.js +9 -6
  37. package/src/exports/index.js +52 -29
  38. package/src/exports/internal/client.js +5 -0
  39. package/src/exports/internal/env.js +8 -5
  40. package/src/exports/internal/index.js +1 -90
  41. package/src/exports/internal/{event.js → server/event.js} +2 -3
  42. package/src/exports/internal/server/index.js +37 -0
  43. package/src/exports/internal/server/telemetry.js +95 -0
  44. package/src/exports/internal/shared.js +90 -0
  45. package/src/exports/node/index.js +64 -22
  46. package/src/exports/params/index.js +70 -0
  47. package/src/exports/params/public.d.ts +63 -0
  48. package/src/exports/public.d.ts +50 -1733
  49. package/src/exports/remote/index.js +11 -0
  50. package/src/exports/remote/public.d.ts +519 -0
  51. package/src/exports/url.js +86 -0
  52. package/src/exports/vite/build/build_server.js +54 -65
  53. package/src/exports/vite/build/remote.js +24 -19
  54. package/src/exports/vite/build/utils.js +0 -8
  55. package/src/exports/vite/dev/index.js +218 -146
  56. package/src/exports/vite/index.js +1590 -858
  57. package/src/exports/vite/module_ids.js +2 -2
  58. package/src/exports/vite/preview/index.js +40 -32
  59. package/src/exports/vite/public.d.ts +588 -0
  60. package/src/exports/vite/utils.js +84 -46
  61. package/src/pathname.js +55 -0
  62. package/src/runner.js +15 -0
  63. package/src/runtime/app/env/internal.js +4 -4
  64. package/src/runtime/app/env/types.d.ts +1 -1
  65. package/src/runtime/app/environment/index.js +3 -3
  66. package/src/runtime/app/{forms.js → forms/index.js} +73 -42
  67. package/src/runtime/app/forms/public.d.ts +2 -0
  68. package/src/runtime/app/forms/types.d.ts +56 -0
  69. package/src/runtime/app/internal/transport.js +53 -0
  70. package/src/runtime/app/manifest/index.js +1 -0
  71. package/src/runtime/app/{navigation.js → navigation/index.js} +2 -1
  72. package/src/runtime/app/navigation/public.d.ts +237 -0
  73. package/src/runtime/app/paths/client.js +37 -37
  74. package/src/runtime/app/paths/index.js +1 -1
  75. package/src/runtime/app/paths/internal/client.js +34 -2
  76. package/src/runtime/app/paths/internal/server.js +6 -23
  77. package/src/runtime/app/paths/internal.d.ts +3 -0
  78. package/src/runtime/app/paths/public.d.ts +1 -29
  79. package/src/runtime/app/paths/server.js +36 -17
  80. package/src/runtime/app/paths/types.d.ts +11 -19
  81. package/src/runtime/app/server/index.js +2 -2
  82. package/src/runtime/app/server/public.d.ts +201 -0
  83. package/src/runtime/app/server/remote/command.js +13 -11
  84. package/src/runtime/app/server/remote/form.js +61 -39
  85. package/src/runtime/app/server/remote/prerender.js +45 -45
  86. package/src/runtime/app/server/remote/query.js +106 -108
  87. package/src/runtime/app/server/remote/requested.js +27 -19
  88. package/src/runtime/app/server/remote/shared.js +76 -76
  89. package/src/runtime/app/service-worker/index.js +24 -0
  90. package/src/runtime/app/state/client.js +4 -2
  91. package/src/runtime/app/state/index.js +7 -5
  92. package/src/runtime/app/state/public.d.ts +72 -0
  93. package/src/runtime/app/state/server.js +3 -0
  94. package/src/runtime/app/stores.js +15 -78
  95. package/src/runtime/client/bundle.js +1 -1
  96. package/src/runtime/client/client-entry.js +3 -0
  97. package/src/runtime/client/client.js +1336 -648
  98. package/src/runtime/client/constants.js +3 -6
  99. package/src/runtime/client/entry.js +24 -3
  100. package/src/runtime/client/fetcher.js +25 -25
  101. package/src/runtime/client/ndjson.js +1 -1
  102. package/src/runtime/client/parse.js +1 -1
  103. package/src/runtime/client/payload.js +17 -0
  104. package/src/runtime/client/remote-functions/cache.svelte.js +3 -1
  105. package/src/runtime/client/remote-functions/command.svelte.js +37 -61
  106. package/src/runtime/client/remote-functions/form.svelte.js +267 -178
  107. package/src/runtime/client/remote-functions/prerender.svelte.js +32 -11
  108. package/src/runtime/client/remote-functions/query/index.js +10 -17
  109. package/src/runtime/client/remote-functions/query/instance.svelte.js +64 -18
  110. package/src/runtime/client/remote-functions/query/proxy.js +5 -5
  111. package/src/runtime/client/remote-functions/query-batch.svelte.js +61 -70
  112. package/src/runtime/client/remote-functions/query-live/index.js +1 -1
  113. package/src/runtime/client/remote-functions/query-live/instance.svelte.js +63 -21
  114. package/src/runtime/client/remote-functions/query-live/iterator.js +15 -12
  115. package/src/runtime/client/remote-functions/query-live/proxy.js +2 -12
  116. package/src/runtime/client/remote-functions/shared.svelte.js +90 -65
  117. package/src/runtime/client/snapshots.js +147 -0
  118. package/src/runtime/client/state.svelte.js +94 -55
  119. package/src/runtime/client/stream.js +3 -2
  120. package/src/runtime/client/types.d.ts +13 -9
  121. package/src/runtime/client/utils.js +22 -110
  122. package/src/runtime/components/root.svelte +56 -0
  123. package/src/runtime/env/dynamic/private.js +7 -0
  124. package/src/runtime/env/dynamic/public.js +7 -0
  125. package/src/runtime/env/static/private.js +6 -0
  126. package/src/runtime/env/static/public.js +6 -0
  127. package/src/runtime/form-utils.js +172 -61
  128. package/src/runtime/pathname.js +20 -32
  129. package/src/runtime/props.svelte.js +72 -0
  130. package/src/runtime/server/constants.js +0 -3
  131. package/src/runtime/server/cookie.js +72 -55
  132. package/src/runtime/server/csrf.js +65 -0
  133. package/src/runtime/server/data/index.js +49 -53
  134. package/src/runtime/server/dev.js +22 -0
  135. package/src/runtime/server/endpoint.js +6 -7
  136. package/src/runtime/server/env_module.js +0 -5
  137. package/src/runtime/server/errors.js +160 -0
  138. package/src/runtime/server/fetch.js +32 -39
  139. package/src/runtime/server/index.js +90 -64
  140. package/src/runtime/server/internal.js +71 -0
  141. package/src/runtime/server/page/actions.js +85 -64
  142. package/src/runtime/server/page/crypto.js +2 -2
  143. package/src/runtime/server/page/csp.js +88 -104
  144. package/src/runtime/server/page/data_serializer.js +24 -25
  145. package/src/runtime/server/page/index.js +36 -53
  146. package/src/runtime/server/page/load_data.js +50 -57
  147. package/src/runtime/server/page/render.js +173 -239
  148. package/src/runtime/server/page/respond_with_error.js +17 -31
  149. package/src/runtime/server/page/serialize_data.js +2 -13
  150. package/src/runtime/server/page/server_routing.js +85 -26
  151. package/src/runtime/server/remote-functions.js +640 -0
  152. package/src/runtime/server/respond.js +190 -131
  153. package/src/runtime/server/sourcemaps.js +183 -0
  154. package/src/runtime/server/state.js +53 -0
  155. package/src/runtime/server/utils.js +13 -155
  156. package/src/runtime/shared.js +20 -40
  157. package/src/runtime/utils.js +3 -0
  158. package/src/types/ambient-private.d.ts +11 -1
  159. package/src/types/ambient.d.ts +87 -36
  160. package/src/types/global-private.d.ts +25 -25
  161. package/src/types/internal.d.ts +163 -157
  162. package/src/types/private.d.ts +41 -1
  163. package/src/utils/error.js +28 -4
  164. package/src/utils/escape.js +9 -25
  165. package/src/utils/features.js +1 -1
  166. package/src/utils/filesystem.js +1 -23
  167. package/src/utils/fork.js +7 -2
  168. package/src/utils/hash.js +21 -0
  169. package/src/utils/http.js +8 -7
  170. package/src/utils/import.js +9 -2
  171. package/src/utils/imports.js +83 -0
  172. package/src/utils/mime.js +9 -0
  173. package/src/utils/page_nodes.js +6 -7
  174. package/src/utils/params.js +67 -0
  175. package/src/utils/regex.js +9 -0
  176. package/src/utils/routing.js +145 -73
  177. package/src/utils/shared-iterator.js +5 -0
  178. package/src/utils/streaming.js +14 -4
  179. package/src/utils/url.js +33 -2
  180. package/src/version.js +1 -1
  181. package/types/index.d.ts +2616 -2433
  182. package/types/index.d.ts.map +120 -106
  183. package/src/core/sync/write_ambient.js +0 -18
  184. package/src/core/sync/write_root.js +0 -148
  185. package/src/core/sync/write_tsconfig.js +0 -250
  186. package/src/exports/internal/server.js +0 -22
  187. package/src/exports/node/polyfills.js +0 -30
  188. package/src/runtime/server/app.js +0 -9
  189. package/src/runtime/server/remote.js +0 -457
  190. package/src/runtime/shared-server.js +0 -7
  191. package/src/runtime/telemetry/otel.js +0 -21
  192. package/src/runtime/telemetry/record_span.js +0 -65
  193. package/src/types/synthetic/$lib.md +0 -5
  194. /package/src/exports/internal/{remote-functions.js → server/remote-functions.js} +0 -0
  195. /package/src/{runtime/telemetry/noop.js → telemetry.js} +0 -0
@@ -1,10 +1,7 @@
1
1
  export const SNAPSHOT_KEY = 'sveltekit:snapshot';
2
- export const SCROLL_KEY = 'sveltekit:scroll';
3
- export const STATES_KEY = 'sveltekit:states';
4
- export const PAGE_URL_KEY = 'sveltekit:pageurl';
5
-
6
- export const HISTORY_INDEX = 'sveltekit:history';
7
- export const NAVIGATION_INDEX = 'sveltekit:navigation';
2
+ export const NAVIGATION_SNAPSHOT_KEY = 'sveltekit:navigation-snapshot';
3
+ export const HISTORY_INFO_KEY = 'sveltekit:history-info';
4
+ export const HISTORY_METADATA_KEY = 'sveltekit:metadata';
8
5
 
9
6
  export const PRELOAD_PRIORITIES = /** @type {const} */ ({
10
7
  tap: 1,
@@ -1,3 +1,24 @@
1
- // we expose this as a separate entry point (rather than treating client.js as the entry point)
2
- // so that everything other than `start`/`load_css` can be treeshaken
3
- export { start, load_css } from './client.js';
1
+ /* in development or if `bundleStrategy` is 'split', this file is used as the entry point */
2
+
3
+ import { set_payload } from './payload.js';
4
+
5
+ /** @type {Promise<typeof import('./client-entry.js')>} */
6
+ let client;
7
+
8
+ /** @param {import('types').SvelteKitPayload} payload */
9
+ export function init(payload) {
10
+ set_payload(payload);
11
+ // Importing the client after setting the payload ensures that modules such as
12
+ // `$app/paths` can read it during initialization.
13
+ client ??= import('./client-entry.js');
14
+ }
15
+
16
+ /** @param {Parameters<typeof import('./client-entry.js').start>} args */
17
+ export async function start(...args) {
18
+ return (await client).start(...args);
19
+ }
20
+
21
+ /** @param {Parameters<typeof import('./client-entry.js').load_css>} args */
22
+ export async function load_css(...args) {
23
+ return (await client).load_css(...args);
24
+ }
@@ -1,6 +1,6 @@
1
1
  import { BROWSER, DEV } from 'esm-env';
2
2
  import { noop } from '../../utils/functions.js';
3
- import { hash } from '../../utils/hash.js';
3
+ import { hash_request } from '../../utils/hash.js';
4
4
  import { base64_decode } from '../utils.js';
5
5
 
6
6
  let loading = 0;
@@ -89,21 +89,24 @@ const cache = new Map();
89
89
  export function initial_fetch(resource, opts) {
90
90
  const selector = build_selector(resource, opts);
91
91
 
92
- const script = document.querySelector(selector);
93
- if (script?.textContent) {
94
- script.remove(); // In case multiple script tags match the same selector
95
- let { body, ...init } = JSON.parse(script.textContent);
96
-
97
- const ttl = script.getAttribute('data-ttl');
98
- if (ttl) cache.set(selector, { body, init, ttl: 1000 * Number(ttl) });
99
- const b64 = script.getAttribute('data-b64');
100
- if (b64 !== null) {
101
- // Can't use native_fetch('data:...;base64,${body}')
102
- // csp can block the request
103
- body = base64_decode(body);
104
- }
92
+ if (selector) {
93
+ const script = document.querySelector(selector);
94
+ if (script?.textContent) {
95
+ script.remove(); // In case multiple script tags match the same selector
96
+ let { body, ...init } = JSON.parse(script.textContent);
97
+
98
+ const b64 = script.getAttribute('data-b64');
99
+ if (b64 !== null) {
100
+ // Can't use native_fetch('data:...;base64,${body}')
101
+ // csp can block the request
102
+ body = base64_decode(body);
103
+ }
104
+
105
+ const ttl = script.getAttribute('data-ttl');
106
+ if (ttl) cache.set(selector, { body, init, ttl: 1000 * Number(ttl) });
105
107
 
106
- return Promise.resolve(new Response(body, init));
108
+ return Promise.resolve(new Response(body, init));
109
+ }
107
110
  }
108
111
 
109
112
  return DEV ? dev_fetch(resource, opts) : window.fetch(resource, opts);
@@ -118,7 +121,7 @@ export function initial_fetch(resource, opts) {
118
121
  export function subsequent_fetch(resource, resolved, opts) {
119
122
  if (cache.size > 0) {
120
123
  const selector = build_selector(resource, opts);
121
- const cached = cache.get(selector);
124
+ const cached = selector && cache.get(selector);
122
125
  if (cached) {
123
126
  // https://developer.mozilla.org/en-US/docs/Web/API/Request/cache#value
124
127
  if (
@@ -154,6 +157,7 @@ export function dev_fetch(resource, opts) {
154
157
  * Build the cache key for a given request
155
158
  * @param {URL | RequestInfo} resource
156
159
  * @param {RequestInit} [opts]
160
+ * @returns {string | null} `null` for requests the server never serializes
157
161
  */
158
162
  function build_selector(resource, opts) {
159
163
  const url = JSON.stringify(resource instanceof Request ? resource.url : resource);
@@ -161,18 +165,14 @@ function build_selector(resource, opts) {
161
165
  let selector = `script[data-sveltekit-fetched][data-url=${url}]`;
162
166
 
163
167
  if (opts?.headers || opts?.body) {
164
- /** @type {import('types').StrictBody[]} */
165
- const values = [];
166
-
167
- if (opts.headers) {
168
- values.push([...new Headers(opts.headers)].join(','));
169
- }
168
+ const body = opts.body;
170
169
 
171
- if (opts.body && (typeof opts.body === 'string' || ArrayBuffer.isView(opts.body))) {
172
- values.push(opts.body);
170
+ if (body && typeof body !== 'string' && !ArrayBuffer.isView(body)) {
171
+ // the server skips serializing these, so a matching script tag belongs to another request
172
+ return null;
173
173
  }
174
174
 
175
- selector += `[data-hash="${hash(...values)}"]`;
175
+ selector += `[data-hash="${hash_request(opts.headers, body)}"]`;
176
176
  }
177
177
 
178
178
  return selector;
@@ -6,7 +6,7 @@ import { read_stream } from './stream.js';
6
6
  * @param {ReadableStreamDefaultReader<Uint8Array>} reader
7
7
  */
8
8
  export async function* read_ndjson(reader) {
9
- for await (const block of read_stream(reader, '\n')) {
9
+ for await (const block of read_stream(reader, '\n', { fatal: true })) {
10
10
  const line = block.trim();
11
11
  if (line) {
12
12
  yield JSON.parse(line);
@@ -4,7 +4,7 @@ import { exec, parse_route_id } from '../../utils/routing.js';
4
4
  * @param {import('./types.js').SvelteKitApp} app
5
5
  * @returns {import('types').CSRRoute[]}
6
6
  */
7
- export function parse({ nodes, server_loads, dictionary, matchers }) {
7
+ export function parse_routes({ nodes, server_loads, dictionary, matchers }) {
8
8
  const layouts_with_server_load = new Set(server_loads);
9
9
 
10
10
  return Object.entries(dictionary).map(([id, [leaf, layouts, errors]]) => {
@@ -0,0 +1,17 @@
1
+ /** @import {SvelteKitPayload} from 'types'; */
2
+
3
+ /**
4
+ * Code inside the SvelteKit client runtime should only use this, not the global,
5
+ * so that the file hashes stay stable between rebuilds as long as the SvelteKit runtime doesn't change
6
+ */
7
+ export let payload = __SVELTEKIT_PAYLOAD__ ?? /** @type {SvelteKitPayload} */ ({});
8
+
9
+ /** @param {SvelteKitPayload} value */
10
+ export function set_payload(value) {
11
+ payload = value;
12
+ }
13
+
14
+ // this makes Vite inject its dev client code as this module's first dependency
15
+ // so that global constant replacements are done before any other module evaluates.
16
+ // For build, it's inert.
17
+ import.meta.hot;
@@ -1,8 +1,10 @@
1
+ /** @import { Query } from './query/instance.svelte.js' */
2
+ /** @import { LiveQuery } from './query-live/instance.svelte.js' */
1
3
  import { tick } from 'svelte';
2
4
  import { once } from '../../../utils/functions.js';
3
5
 
4
6
  /**
5
- * @template R
7
+ * @template [R=Query<any> | LiveQuery<any>]
6
8
  * @typedef {object} CacheEntry
7
9
  * @property {number} proxy_count The number of live proxy instances referencing this
8
10
  * entry. The entry is eligible for eviction when this hits zero.
@@ -1,16 +1,7 @@
1
- /** @import { RemoteCommand, RemoteQueryUpdate } from '@sveltejs/kit' */
2
- /** @import { RemoteFunctionResponse } from 'types' */
3
- import { app_dir, base } from '$app/paths/internal/client';
4
- import * as devalue from 'devalue';
5
- import { HttpError } from '@sveltejs/kit/internal';
6
- import { app } from '../client.js';
1
+ /** @import { RemoteCommand, RemoteQueryUpdate } from '@sveltejs/kit/remote' */
2
+ import { app_dir, base } from '#app/paths';
7
3
  import { stringify_command_arg } from '../../shared.js';
8
- import {
9
- get_remote_request_headers,
10
- apply_refreshes,
11
- categorize_updates,
12
- apply_reconnections
13
- } from './shared.svelte.js';
4
+ import { get_remote_request_headers, categorize_updates, remote_request } from './shared.svelte.js';
14
5
 
15
6
  /**
16
7
  * Client-version of the `command` function from `$app/server`.
@@ -43,56 +34,41 @@ export function command(id) {
43
34
  ...get_remote_request_headers()
44
35
  };
45
36
 
46
- /** @type {Promise<any> & { updates: (...args: RemoteQueryUpdate[]) => Promise<any> }} */
47
- const promise = (async () => {
48
- try {
49
- // Wait a tick to give room for the `updates` method to be called
50
- await Promise.resolve();
51
-
52
- if (updates_error) {
53
- throw updates_error;
54
- }
55
-
56
- const response = await fetch(`${base}/${app_dir}/remote/${id}`, {
57
- method: 'POST',
58
- body: JSON.stringify({
59
- payload: await stringify_command_arg(arg, app.hooks.transport),
60
- refreshes: Array.from(refreshes ?? [])
61
- }),
62
- headers
63
- });
64
-
65
- if (!response.ok) {
66
- // We only end up here in case of a network error or if the server has an internal error
67
- // (which shouldn't happen because we handle errors on the server and always send a 200 response)
68
- throw new Error('Failed to execute remote function');
69
- }
70
-
71
- const result = /** @type {RemoteFunctionResponse} */ (await response.json());
72
- if (result.type === 'redirect') {
73
- throw new Error(
74
- 'Redirects are not allowed in commands. Return a result instead and use goto on the client'
75
- );
76
- } else if (result.type === 'error') {
77
- throw new HttpError(result.status ?? 500, result.error);
78
- } else {
79
- if (result.refreshes) {
80
- apply_refreshes(result.refreshes);
81
- }
82
-
83
- if (result.reconnects) {
84
- apply_reconnections(result.reconnects);
37
+ const promise =
38
+ /** @type {Promise<any> & { updates: (...args: RemoteQueryUpdate[]) => Promise<any> }} */ (
39
+ (async () => {
40
+ try {
41
+ // Wait a tick to give room for the `updates` method to be called
42
+ await Promise.resolve();
43
+
44
+ if (updates_error) {
45
+ throw updates_error;
46
+ }
47
+
48
+ const response = await remote_request(`${base}/${app_dir}/remote/${id}`, {
49
+ method: 'POST',
50
+ body: JSON.stringify({
51
+ payload: await stringify_command_arg(arg),
52
+ refreshes: Array.from(refreshes ?? [])
53
+ }),
54
+ headers
55
+ });
56
+
57
+ if (response.redirect) {
58
+ throw new Error(
59
+ 'Redirects are not allowed in commands. Return a result instead and use goto on the client'
60
+ );
61
+ }
62
+
63
+ return response._;
64
+ } finally {
65
+ overrides?.forEach((fn) => fn());
66
+
67
+ // Decrement pending count when command completes
68
+ pending_count--;
85
69
  }
86
-
87
- return devalue.parse(result.result, app.decoders);
88
- }
89
- } finally {
90
- overrides?.forEach((fn) => fn());
91
-
92
- // Decrement pending count when command completes
93
- pending_count--;
94
- }
95
- })();
70
+ })()
71
+ );
96
72
 
97
73
  let updates_called = false;
98
74
  promise.updates = (...args) => {