|
| 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 | +} |
0 commit comments