summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/preload/resources/prefetch-helper.js
blob: 367d4824c4174b65a77999bae70c26a779edb51e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
async function get_prefetch_info(href) {
    const response = await fetch(`${href}&mode=info`, {mode: "cors"});
    return await response.json();
}

async function prefetch(p = {}, t) {
    const link = document.createElement("link");
    link.rel = "prefetch";
    link.as = p.as;
    if (p.crossOrigin)
        link.setAttribute("crossorigin", p.crossOrigin);
    const uid = token();
    const params = new URLSearchParams();
    params.set("key", uid);
    for (const key in p)
        params.set(key, p[key]);
    const origin = p.origin || '';
    link.href = `${origin}/preload/resources/prefetch-info.py?${params.toString()}`;
    document.head.appendChild(link);
    while (!(await get_prefetch_info(link.href)).length) { }
    return {href: link.href, uid};
}