-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchUrlRewriter.js
36 lines (32 loc) · 1016 Bytes
/
fetchUrlRewriter.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
function isAbsolutUrl(url) {
return startsWith(url, "http");
}
function startsWith(string, prefix) {
return string.indexOf(prefix) === 0;
}
const urlRewriter={
prefix: null,
rewrite: function(requestedUrl) {
let effectiveUrl = requestedUrl;
let effectivePrefix;
const metaElement=document.querySelector('meta[name="fetchUrlPrefix"]');
if(metaElement) {
effectivePrefix=metaElement.content;
}
if(this.prefix) {
effectivePrefix=this.prefix;
}
if(effectivePrefix) {
if (!isAbsolutUrl(effectiveUrl) && !startsWith(effectiveUrl, effectivePrefix)) {
effectiveUrl = effectivePrefix + "/" + effectiveUrl;
}
}
return effectiveUrl;
}
};
var proxiedFetch = window.fetch ;
window.fetch = function(requestedUrl, init) {
let effectiveUrl = urlRewriter.rewrite(requestedUrl);
return proxiedFetch.apply(this, [effectiveUrl, init]);
};
export {urlRewriter};