Skip to content

Commit e583fe9

Browse files
authored
fix(sveltekit): Check for cached requests in client-side fetch instrumentation (#8391)
As outlined in #8174 (comment), our current SvelteKit fetch instrumentation breaks SvelteKit's request caching mechanism. This is problematic as `fetch` requests from universal `load` functions were made again on the client side during hydration although the response was already cached from the initial server-side request. The reason for the cache miss is that in the instrumentation we add our tracing headers to the requests, which lead to a different cache key than the one produced on the server side. This fix vendors in code from [the SvelteKit repo](https://github.com/sveltejs/kit) so that we can perform the same cache lookup in our instrumentation. If the lookup was successful (--> cache hit), we won't attach any headers or create breadcrumbs to 1. let Kit's fetch return the cached response and 2. not add spans for a fetch request that didn't even happen.
1 parent 98d3916 commit e583fe9

File tree

7 files changed

+250
-1
lines changed

7 files changed

+250
-1
lines changed

packages/sveltekit/src/client/load.ts

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { LoadEvent } from '@sveltejs/kit';
1717

1818
import type { SentryWrappedFlag } from '../common/utils';
1919
import { isRedirect } from '../common/utils';
20+
import { isRequestCached } from './vendor/lookUpCache';
2021

2122
type PatchedLoadEvent = LoadEvent & Partial<SentryWrappedFlag>;
2223

@@ -153,6 +154,11 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
153154
return new Proxy(originalFetch, {
154155
apply: (wrappingTarget, thisArg, args: Parameters<LoadEvent['fetch']>) => {
155156
const [input, init] = args;
157+
158+
if (isRequestCached(input, init)) {
159+
return wrappingTarget.apply(thisArg, args);
160+
}
161+
156162
const { url: rawUrl, method } = parseFetchArgs(args);
157163

158164
// TODO: extract this to a util function (and use it in breadcrumbs integration as well)
@@ -196,6 +202,7 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
196202

197203
patchedInit.headers = headers;
198204
}
205+
199206
let fetchPromise: Promise<Response>;
200207

201208
const patchedFetchArgs = [input, patchedInit];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* eslint-disable @sentry-internal/sdk/no-optional-chaining */
2+
3+
// Vendored from https://github.com/sveltejs/kit/blob/1c1ddd5e34fce28e6f89299d6c59162bed087589/packages/kit/src/runtime/client/fetcher.js
4+
// with types only changes.
5+
6+
// The MIT License (MIT)
7+
8+
// Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
9+
10+
// Permission is hereby granted, free of charge, to any person obtaining a copy
11+
// of this software and associated documentation files(the "Software"), to deal
12+
// in the Software without restriction, including without limitation the rights
13+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14+
// copies of the Software, and to permit persons to whom the Software is
15+
// furnished to do so, subject to the following conditions:
16+
17+
// The above copyright notice and this permission notice shall be included in
18+
// all copies or substantial portions of the Software.
19+
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
// THE SOFTWARE.
27+
28+
import { hash } from './hash';
29+
30+
/**
31+
* Build the cache key for a given request
32+
* @param {URL | RequestInfo} resource
33+
* @param {RequestInit} [opts]
34+
*/
35+
export function build_selector(resource: URL | RequestInfo, opts: RequestInit | undefined): string {
36+
const url = JSON.stringify(resource instanceof Request ? resource.url : resource);
37+
38+
let selector = `script[data-sveltekit-fetched][data-url=${url}]`;
39+
40+
if (opts?.headers || opts?.body) {
41+
/** @type {import('types').StrictBody[]} */
42+
const values = [];
43+
44+
if (opts.headers) {
45+
// @ts-ignore - TS complains but this is a 1:1 copy of the original code and apparently it works
46+
values.push([...new Headers(opts.headers)].join(','));
47+
}
48+
49+
if (opts.body && (typeof opts.body === 'string' || ArrayBuffer.isView(opts.body))) {
50+
values.push(opts.body);
51+
}
52+
53+
selector += `[data-hash="${hash(...values)}"]`;
54+
}
55+
56+
return selector;
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-disable no-bitwise */
2+
3+
// Vendored from https://github.com/sveltejs/kit/blob/1c1ddd5e34fce28e6f89299d6c59162bed087589/packages/kit/src/runtime/hash.js
4+
// with types only changes.
5+
6+
// The MIT License (MIT)
7+
8+
// Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
9+
10+
// Permission is hereby granted, free of charge, to any person obtaining a copy
11+
// of this software and associated documentation files(the "Software"), to deal
12+
// in the Software without restriction, including without limitation the rights
13+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14+
// copies of the Software, and to permit persons to whom the Software is
15+
// furnished to do so, subject to the following conditions:
16+
17+
// The above copyright notice and this permission notice shall be included in
18+
// all copies or substantial portions of the Software.
19+
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
// THE SOFTWARE.
27+
28+
import type { StrictBody } from '@sveltejs/kit/types/internal';
29+
30+
/**
31+
* Hash using djb2
32+
* @param {import('types').StrictBody[]} values
33+
*/
34+
export function hash(...values: StrictBody[]): string {
35+
let hash = 5381;
36+
37+
for (const value of values) {
38+
if (typeof value === 'string') {
39+
let i = value.length;
40+
while (i) hash = (hash * 33) ^ value.charCodeAt(--i);
41+
} else if (ArrayBuffer.isView(value)) {
42+
const buffer = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
43+
let i = buffer.length;
44+
while (i) hash = (hash * 33) ^ buffer[--i];
45+
} else {
46+
throw new TypeError('value must be a string or TypedArray');
47+
}
48+
}
49+
50+
return (hash >>> 0).toString(36);
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* eslint-disable no-bitwise */
2+
3+
// Parts of this code are taken from https://github.com/sveltejs/kit/blob/1c1ddd5e34fce28e6f89299d6c59162bed087589/packages/kit/src/runtime/client/fetcher.js
4+
// Attribution given directly in the function code below
5+
6+
// The MIT License (MIT)
7+
8+
// Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
9+
10+
// Permission is hereby granted, free of charge, to any person obtaining a copy
11+
// of this software and associated documentation files(the "Software"), to deal
12+
// in the Software without restriction, including without limitation the rights
13+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14+
// copies of the Software, and to permit persons to whom the Software is
15+
// furnished to do so, subject to the following conditions:
16+
17+
// The above copyright notice and this permission notice shall be included in
18+
// all copies or substantial portions of the Software.
19+
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
// THE SOFTWARE.
27+
28+
import { WINDOW } from '@sentry/svelte';
29+
import { getDomElement } from '@sentry/utils';
30+
31+
import { build_selector } from './buildSelector';
32+
33+
/**
34+
* Checks if a request is cached by looking for a script tag with the same selector as the constructed selector of the request.
35+
*
36+
* This function is a combination of the cache lookups in sveltekit's internal client-side fetch functions
37+
* - initial_fetch (used during hydration) https://github.com/sveltejs/kit/blob/1c1ddd5e34fce28e6f89299d6c59162bed087589/packages/kit/src/runtime/client/fetcher.js#L76
38+
* - subsequent_fetch (used afterwards) https://github.com/sveltejs/kit/blob/1c1ddd5e34fce28e6f89299d6c59162bed087589/packages/kit/src/runtime/client/fetcher.js#L98
39+
*
40+
* Parts of this function's logic is taken from SvelteKit source code.
41+
* These lines are annotated with attribution in comments above them.
42+
*
43+
* @param input first fetch param
44+
* @param init second fetch param
45+
* @returns true if a cache hit was encountered, false otherwise
46+
*/
47+
export function isRequestCached(input: URL | RequestInfo, init: RequestInit | undefined): boolean {
48+
// build_selector call copied from https://github.com/sveltejs/kit/blob/1c1ddd5e34fce28e6f89299d6c59162bed087589/packages/kit/src/runtime/client/fetcher.js#L77
49+
const selector = build_selector(input, init);
50+
51+
const script = getDomElement<HTMLScriptElement>(selector);
52+
53+
if (!script) {
54+
return false;
55+
}
56+
57+
// If the script has a data-ttl attribute, we check if we're still in the TTL window:
58+
try {
59+
// ttl retrieval taken from https://github.com/sveltejs/kit/blob/1c1ddd5e34fce28e6f89299d6c59162bed087589/packages/kit/src/runtime/client/fetcher.js#L83-L84
60+
const ttl = Number(script.getAttribute('data-ttl')) * 1000;
61+
62+
if (isNaN(ttl)) {
63+
return false;
64+
}
65+
66+
if (ttl) {
67+
// cache hit determination taken from: https://github.com/sveltejs/kit/blob/1c1ddd5e34fce28e6f89299d6c59162bed087589/packages/kit/src/runtime/client/fetcher.js#L105-L106
68+
return (
69+
WINDOW.performance.now() < ttl &&
70+
['default', 'force-cache', 'only-if-cached', undefined].includes(init && init.cache)
71+
);
72+
}
73+
} catch {
74+
return false;
75+
}
76+
77+
// Otherwise, we check if the script has a content and return true in that case
78+
return !!script.textContent;
79+
}

packages/sveltekit/test/client/load.test.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ vi.mock('@sentry/svelte', async () => {
2727
};
2828
});
2929

30+
vi.mock('../../src/client/vendor/lookUpCache', () => {
31+
return {
32+
isRequestCached: () => false,
33+
};
34+
});
35+
3036
const mockTrace = vi.fn();
3137

3238
const mockedBrowserTracing = {
@@ -433,7 +439,6 @@ describe('wrapLoadWithSentry', () => {
433439
['is undefined', undefined],
434440
["doesn't have a `getClientById` method", {}],
435441
])("doesn't instrument fetch if the client %s", async (_, client) => {
436-
// @ts-expect-error: we're mocking the client
437442
mockedGetClient.mockImplementationOnce(() => client);
438443

439444
async function load(_event: Parameters<Load>[0]): Promise<ReturnType<Load>> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { JSDOM } from 'jsdom';
2+
import { vi } from 'vitest';
3+
4+
import { isRequestCached } from '../../../src/client/vendor/lookUpCache';
5+
6+
globalThis.document = new JSDOM().window.document;
7+
8+
vi.useFakeTimers().setSystemTime(new Date('2023-06-22'));
9+
vi.spyOn(performance, 'now').mockReturnValue(1000);
10+
11+
describe('isRequestCached', () => {
12+
it('should return true if a script tag with the same selector as the constructed request selector is found', () => {
13+
globalThis.document.body.innerHTML =
14+
'<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1">{"status":200}</script>';
15+
16+
expect(isRequestCached('/api/todos/1', undefined)).toBe(true);
17+
});
18+
19+
it('should return false if a script with the same selector as the constructed request selector is not found', () => {
20+
globalThis.document.body.innerHTML = '';
21+
22+
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
23+
});
24+
25+
it('should return true if a script with the same selector as the constructed request selector is found and its TTL is valid', () => {
26+
globalThis.document.body.innerHTML =
27+
'<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1" data-ttl="10">{"status":200}</script>';
28+
29+
expect(isRequestCached('/api/todos/1', undefined)).toBe(true);
30+
});
31+
32+
it('should return false if a script with the same selector as the constructed request selector is found and its TTL is expired', () => {
33+
globalThis.document.body.innerHTML =
34+
'<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1" data-ttl="1">{"status":200}</script>';
35+
36+
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
37+
});
38+
39+
it("should return false if the TTL is set but can't be parsed as a number", () => {
40+
globalThis.document.body.innerHTML =
41+
'<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1" data-ttl="notANumber">{"status":200}</script>';
42+
43+
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
44+
});
45+
});

packages/sveltekit/test/vitest.setup.ts

+5
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ export function setup() {
1111
};
1212
});
1313
}
14+
15+
if (!globalThis.fetch) {
16+
// @ts-ignore - Needed for vitest to work with SvelteKit fetch instrumentation
17+
globalThis.Request = class Request {};
18+
}

0 commit comments

Comments
 (0)