Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-donuts-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

chore: deduplicate request hashing for serialized fetch responses
17 changes: 4 additions & 13 deletions packages/kit/src/runtime/client/fetcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BROWSER, DEV } from 'esm-env';
import { noop } from '../../utils/functions.js';
import { hash } from '../../utils/hash.js';
import { hash_request } from '../../utils/hash.js';
import { base64_decode } from '../utils.js';

let loading = 0;
Expand Down Expand Up @@ -162,18 +162,9 @@ function build_selector(resource, opts) {
let selector = `script[data-sveltekit-fetched][data-url=${url}]`;

if (opts?.headers || opts?.body) {
/** @type {import('types').StrictBody[]} */
const values = [];

if (opts.headers) {
values.push([...new Headers(opts.headers)].join(','));
}

if (opts.body && (typeof opts.body === 'string' || ArrayBuffer.isView(opts.body))) {
values.push(opts.body);
}

selector += `[data-hash="${hash(...values)}"]`;
const body =
typeof opts.body === 'string' || ArrayBuffer.isView(opts.body) ? opts.body : undefined;
selector += `[data-hash="${hash_request(opts.headers, body)}"]`;
}

return selector;
Expand Down
15 changes: 2 additions & 13 deletions packages/kit/src/runtime/server/page/serialize_data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { escape_html } from '../../../utils/escape.js';
import { hash } from '../../../utils/hash.js';
import { hash_request } from '../../../utils/hash.js';

/**
* Inside a script element, only `</script` and `<!--` hold special meaning to the HTML parser.
Expand Down Expand Up @@ -78,18 +78,7 @@ export function serialize_data(fetched, filter, prerendering = false) {
}

if (fetched.request_headers || fetched.request_body) {
/** @type {import('types').StrictBody[]} */
const values = [];

if (fetched.request_headers) {
values.push([...new Headers(fetched.request_headers)].join(','));
}

if (fetched.request_body) {
values.push(fetched.request_body);
}

attrs.push(`data-hash="${hash(...values)}"`);
attrs.push(`data-hash="${hash_request(fetched.request_headers, fetched.request_body)}"`);
}

// Compute the time the response should be cached, taking into account max-age and age.
Expand Down
21 changes: 21 additions & 0 deletions packages/kit/src/utils/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,24 @@ export function hash(...values) {

return (hash >>> 0).toString(36);
}

/**
* Hash of the headers and body a `fetch` was called with. The server-side serializer and the
* client-side cache lookup must produce identical values for cached responses to be found.
* @param {HeadersInit | undefined} headers
* @param {import('types').StrictBody | null | undefined} body
*/
export function hash_request(headers, body) {
/** @type {import('types').StrictBody[]} */
const values = [];

if (headers) {
values.push([...new Headers(headers)].join(','));
}

if (body) {
values.push(body);
}

return hash(...values);
}
Loading