"use strict"; function handleRequest(request, response) { let qs = new URLSearchParams(request.queryString); let asset = qs.get("as"); let hinted = qs.get("hinted") === "1"; let httpCode = qs.get("code"); let uuid = qs.get("uuid"); let cached = qs.get("cached") === "1"; let url = `early_hint_asset.sjs?as=${asset}${uuid ? `&uuid=${uuid}` : ""}${ cached ? "&cached=1" : "" }`; // write to raw socket response.seizePower(); let link = ""; if (hinted) { response.write("HTTP/1.1 103 Early Hint\r\n"); if (asset === "fetch" || asset === "font") { // fetch and font has to specify the crossorigin attribute // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-as link = `Link: <${url}>; rel=preload; as=${asset}; crossorigin=anonymous\r\n`; response.write(link); } else if (asset === "module") { // module preloads are handled differently link = `Link: <${url}>; rel=modulepreload\r\n`; response.write(link); } else { link = `Link: <${url}>; rel=preload; as=${asset}\r\n`; response.write(link); } response.write("\r\n"); } let body = ""; if (asset === "image") { body = ` `; } else if (asset === "style") { body = `

Test preload css

`; } else if (asset === "script") { body = `

Test preload javascript

`; } else if (asset === "module") { // this code assumes that the .sjs for the module is in the same directory var file_name = url.split("/"); file_name = file_name[file_name.length - 1]; body = `

Test preload module

`; } else if (asset === "fetch") { body = `

Test preload fetch

Fetching...

`; } else if (asset === "font") { body = `

Test preload font

`; } if (!httpCode) { response.write(`HTTP/1.1 200 OK\r\n`); } else { response.write(`HTTP/1.1 ${httpCode} Error\r\n`); } response.write(link); response.write("Content-Type: text/html;charset=utf-8\r\n"); response.write("Cache-Control: no-cache\r\n"); response.write(`Content-Length: ${body.length}\r\n`); response.write("\r\n"); response.write(body); response.finish(); }