Skip to content

Commit 236cb9e

Browse files
committed
fix(sveltekit): Check for cached requests in client-side fetch instrumentation
1 parent 54e091e commit 236cb9e

File tree

7 files changed

+275
-0
lines changed

7 files changed

+275
-0
lines changed

packages/sveltekit/src/client/load.ts

Lines changed: 7 additions & 0 deletions
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];
Lines changed: 57 additions & 0 deletions
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+
}
Lines changed: 51 additions & 0 deletions
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+
}
Lines changed: 79 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { addTracingExtensions, Scope } from '@sentry/svelte';
22
import { baggageHeaderToDynamicSamplingContext } from '@sentry/utils';
3+
import * as utils from '@sentry/utils';
34
import type { Load } from '@sveltejs/kit';
45
import { redirect } from '@sveltejs/kit';
56
import { vi } from 'vitest';
@@ -27,6 +28,12 @@ vi.mock('@sentry/svelte', async () => {
2728
};
2829
});
2930

31+
vi.spyOn(utils, 'getDomElement').mockImplementation(() => {
32+
return {
33+
textContent: 'test',
34+
};
35+
});
36+
3037
const mockTrace = vi.fn();
3138

3239
const mockedBrowserTracing = {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as utils from '@sentry/utils';
2+
import { vi } from 'vitest';
3+
4+
import { isRequestCached } from '../../../src/client/vendor/lookUpCache';
5+
6+
let scriptElement: {
7+
textContent: string;
8+
getAttribute: (name: string) => string | null;
9+
} | null;
10+
11+
vi.spyOn(utils, 'getDomElement').mockImplementation(() => {
12+
return scriptElement;
13+
});
14+
15+
describe('isRequestCached', () => {
16+
it('should return true if a script tag with the same selector as the constructed request selector is found', () => {
17+
scriptElement = {
18+
textContent: 'test',
19+
getAttribute: () => null,
20+
};
21+
22+
expect(isRequestCached('/api/todos/1', undefined)).toBe(true);
23+
});
24+
25+
it('should return false if a script with the same selector as the constructed request selector is not found', () => {
26+
scriptElement = null;
27+
28+
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
29+
});
30+
31+
it('should return true if a script with the same selector as the constructed request selector is found and its TTL is valid', () => {
32+
scriptElement = {
33+
textContent: 'test',
34+
getAttribute: () => (performance.now() / 1000 + 1).toString(),
35+
};
36+
37+
expect(isRequestCached('/api/todos/1', undefined)).toBe(true);
38+
});
39+
40+
it('should return false if a script with the same selector as the constructed request selector is found and its TTL is expired', () => {
41+
scriptElement = {
42+
textContent: 'test',
43+
getAttribute: () => (performance.now() / 1000 - 1).toString(),
44+
};
45+
46+
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
47+
});
48+
49+
it("should return false if the TTL is set but can't be parsed", () => {
50+
scriptElement = {
51+
textContent: 'test',
52+
getAttribute: () => 'NotANumber',
53+
};
54+
55+
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
56+
});
57+
58+
it('should return false if an error was thrown turing TTL evaluation', () => {
59+
scriptElement = {
60+
textContent: 'test',
61+
getAttribute: () => {
62+
throw new Error('test');
63+
},
64+
};
65+
66+
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
67+
});
68+
});

packages/sveltekit/test/vitest.setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ export function setup() {
1111
};
1212
});
1313
}
14+
15+
if (!globalThis.fetch) {
16+
// @ts-ignore dfsf
17+
globalThis.Request = class Request {};
18+
}
19+
console.log(globalThis.fetch, globalThis.Response, globalThis.Request);

0 commit comments

Comments
 (0)