forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.js
126 lines (99 loc) · 3.47 KB
/
fetcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { hash } from '../hash.js';
let loading = 0;
export const native_fetch = window.fetch;
export function lock_fetch() {
loading += 1;
}
export function unlock_fetch() {
loading -= 1;
}
if (import.meta.env.DEV) {
let can_inspect_stack_trace = false;
const check_stack_trace = async () => {
const stack = /** @type {string} */ (new Error().stack);
can_inspect_stack_trace = stack.includes('check_stack_trace');
};
check_stack_trace();
window.fetch = (input, init) => {
const url = input instanceof Request ? input.url : input.toString();
const stack = /** @type {string} */ (new Error().stack);
// check if fetch was called via load_node. the lock method only checks if it was called at the
// same time, but not necessarily if it was called from `load`
// we use just the filename as the method name sometimes does not appear on the CI
const heuristic = can_inspect_stack_trace
? stack.includes('src/runtime/client/client.js')
: loading;
if (heuristic) {
console.warn(
`Loading ${url} using \`window.fetch\`. For best results, use the \`fetch\` that is passed to your \`load\` function: https://kit.svelte.dev/docs/load#making-fetch-requests`
);
}
const method = input instanceof Request ? input.method : init?.method || 'GET';
if (method !== 'GET') {
const url = new URL(input instanceof Request ? input.url : input.toString(), document.baseURI)
.href;
cache.delete(url);
}
return native_fetch(input, init);
};
} else {
window.fetch = (input, init) => {
const method = input instanceof Request ? input.method : init?.method || 'GET';
if (method !== 'GET') {
const url = new URL(input instanceof Request ? input.url : input.toString(), document.baseURI)
.href;
cache.delete(url);
}
return native_fetch(input, init);
};
}
const cache = new Map();
/**
* Should be called on the initial run of load functions that hydrate the page.
* Saves any requests with cache-control max-age to the cache.
* @param {RequestInfo | URL} resource
* @param {RequestInit} [opts]
*/
export function initial_fetch(resource, opts) {
const selector = build_selector(resource, opts);
const script = document.querySelector(selector);
if (script?.textContent) {
const { body, ...init } = JSON.parse(script.textContent);
const ttl = script.getAttribute('data-ttl');
if (ttl) cache.set(selector, { body, init, ttl: 1000 * Number(ttl) });
return Promise.resolve(new Response(body, init));
}
return native_fetch(resource, opts);
}
/**
* Tries to get the response from the cache, if max-age allows it, else does a fetch.
* @param {RequestInfo | URL} resource
* @param {string} resolved
* @param {RequestInit} [opts]
*/
export function subsequent_fetch(resource, resolved, opts) {
if (cache.size > 0) {
const selector = build_selector(resource, opts);
const cached = cache.get(selector);
if (cached) {
if (performance.now() < cached.ttl) {
return new Response(cached.body, cached.init);
}
cache.delete(selector);
}
}
return native_fetch(resolved, opts);
}
/**
* Build the cache key for a given request
* @param {RequestInfo | URL} resource
* @param {RequestInit} [opts]
*/
function build_selector(resource, opts) {
const url = JSON.stringify(resource instanceof Request ? resource.url : resource);
let selector = `script[data-sveltekit-fetched][data-url=${url}]`;
if (opts?.body && (typeof opts.body === 'string' || ArrayBuffer.isView(opts.body))) {
selector += `[data-hash="${hash(opts.body)}"]`;
}
return selector;
}