@sveltejs/kit 2.48.3 → 2.48.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/kit",
3
- "version": "2.48.3",
3
+ "version": "2.48.4",
4
4
  "description": "SvelteKit is the fastest way to build Svelte apps",
5
5
  "keywords": [
6
6
  "framework",
@@ -171,6 +171,7 @@ export function write_client_manifest(kit, manifest_data, output, metadata) {
171
171
  };
172
172
 
173
173
  export const decoders = Object.fromEntries(Object.entries(hooks.transport).map(([k, v]) => [k, v.decode]));
174
+ export const encoders = Object.fromEntries(Object.entries(hooks.transport).map(([k, v]) => [k, v.encode]));
174
175
 
175
176
  export const hash = ${s(kit.router.type === 'hash')};
176
177
 
@@ -1,4 +1,3 @@
1
- /** @import { RemoteFunctionResponse } from 'types' */
2
1
  import { app_dir, base } from '$app/paths/internal/client';
3
2
  import { version } from '__sveltekit/environment';
4
3
  import * as devalue from 'devalue';
@@ -7,12 +6,12 @@ import { app, remote_responses } from '../client.js';
7
6
  import { create_remote_function, remote_request } from './shared.svelte.js';
8
7
 
9
8
  // Initialize Cache API for prerender functions
10
- const CACHE_NAME = `sveltekit:${version}`;
9
+ const CACHE_NAME = DEV ? `sveltekit:${Date.now()}` : `sveltekit:${version}`;
11
10
  /** @type {Cache | undefined} */
12
11
  let prerender_cache;
13
12
 
14
- void (async () => {
15
- if (!DEV && typeof caches !== 'undefined') {
13
+ const prerender_cache_ready = (async () => {
14
+ if (typeof caches !== 'undefined') {
16
15
  try {
17
16
  prerender_cache = await caches.open(CACHE_NAME);
18
17
 
@@ -111,53 +110,68 @@ class Prerender {
111
110
  }
112
111
  }
113
112
 
113
+ /**
114
+ * @param {string} url
115
+ * @param {string} encoded
116
+ */
117
+ function put(url, encoded) {
118
+ return /** @type {Cache} */ (prerender_cache)
119
+ .put(
120
+ url,
121
+ // We need to create a new response because the original response is already consumed
122
+ new Response(encoded, {
123
+ headers: {
124
+ 'Content-Type': 'application/json'
125
+ }
126
+ })
127
+ )
128
+ .catch(() => {
129
+ // Nothing we can do here
130
+ });
131
+ }
132
+
114
133
  /**
115
134
  * @param {string} id
116
135
  */
117
136
  export function prerender(id) {
118
137
  return create_remote_function(id, (cache_key, payload) => {
119
138
  return new Prerender(async () => {
120
- if (Object.hasOwn(remote_responses, cache_key)) {
121
- return remote_responses[cache_key];
122
- }
139
+ await prerender_cache_ready;
123
140
 
124
141
  const url = `${base}/${app_dir}/remote/${id}${payload ? `/${payload}` : ''}`;
125
142
 
143
+ if (Object.hasOwn(remote_responses, cache_key)) {
144
+ const data = remote_responses[cache_key];
145
+
146
+ if (prerender_cache) {
147
+ void put(url, devalue.stringify(data, app.encoders));
148
+ }
149
+
150
+ return data;
151
+ }
152
+
126
153
  // Check the Cache API first
127
154
  if (prerender_cache) {
128
155
  try {
129
156
  const cached_response = await prerender_cache.match(url);
157
+
130
158
  if (cached_response) {
131
- const cached_result = /** @type { RemoteFunctionResponse & { type: 'result' } } */ (
132
- await cached_response.json()
133
- );
134
- return devalue.parse(cached_result.result, app.decoders);
159
+ const cached_result = await cached_response.text();
160
+ return devalue.parse(cached_result, app.decoders);
135
161
  }
136
162
  } catch {
137
163
  // Nothing we can do here
138
164
  }
139
165
  }
140
166
 
141
- const result = await remote_request(url);
167
+ const encoded = await remote_request(url);
142
168
 
143
169
  // For successful prerender requests, save to cache
144
170
  if (prerender_cache) {
145
- try {
146
- await prerender_cache.put(
147
- url,
148
- // We need to create a new response because the original response is already consumed
149
- new Response(JSON.stringify(result), {
150
- headers: {
151
- 'Content-Type': 'application/json'
152
- }
153
- })
154
- );
155
- } catch {
156
- // Nothing we can do here
157
- }
171
+ void put(url, encoded);
158
172
  }
159
173
 
160
- return result;
174
+ return devalue.parse(encoded, app.decoders);
161
175
  });
162
176
  });
163
177
  }
@@ -31,7 +31,8 @@ export function query(id) {
31
31
 
32
32
  const url = `${base}/${app_dir}/remote/${id}${payload ? `?payload=${payload}` : ''}`;
33
33
 
34
- return await remote_request(url);
34
+ const result = await remote_request(url);
35
+ return devalue.parse(result, app.decoders);
35
36
  });
36
37
  });
37
38
  }
@@ -162,15 +163,19 @@ export class Query {
162
163
  const p = this.#promise;
163
164
  this.#overrides.length;
164
165
 
165
- return async (resolve, reject) => {
166
- try {
166
+ return (resolve, reject) => {
167
+ const result = (async () => {
167
168
  await p;
168
169
  // svelte-ignore await_reactivity_loss
169
170
  await tick();
170
- resolve?.(/** @type {T} */ (this.#current));
171
- } catch (error) {
172
- reject?.(error);
171
+ return /** @type {T} */ (this.#current);
172
+ })();
173
+
174
+ if (resolve || reject) {
175
+ return result.then(resolve, reject);
173
176
  }
177
+
178
+ return result;
174
179
  };
175
180
  });
176
181
 
@@ -250,8 +255,14 @@ export class Query {
250
255
  this.#then;
251
256
  return (/** @type {any} */ fn) => {
252
257
  return this.#then(
253
- () => fn(),
254
- () => fn()
258
+ (value) => {
259
+ fn();
260
+ return value;
261
+ },
262
+ (error) => {
263
+ fn();
264
+ throw error;
265
+ }
255
266
  );
256
267
  };
257
268
  }
@@ -37,7 +37,7 @@ export async function remote_request(url) {
37
37
  throw new HttpError(result.status ?? 500, result.error);
38
38
  }
39
39
 
40
- return devalue.parse(result.result, app.decoders);
40
+ return result.result;
41
41
  }
42
42
 
43
43
  /**
@@ -50,6 +50,7 @@ export interface SvelteKitApp {
50
50
  decode: (type: string, value: any) => any;
51
51
 
52
52
  decoders: Record<string, (data: any) => any>;
53
+ encoders: Record<string, (data: any) => any>;
53
54
 
54
55
  /**
55
56
  * Whether or not we're using hash-based routing
package/src/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  // generated during release, do not modify
2
2
 
3
3
  /** @type {string} */
4
- export const VERSION = '2.48.3';
4
+ export const VERSION = '2.48.4';