3 function handleRequest(request, response) {
7 const qs = new URLSearchParams(request.queryString);
10 let new_header = true;
11 for (const [imgUrl, uuid] of qs.entries()) {
13 // we need to write a new header
15 response.write("HTTP/1.1 103 Early Hint\r\n");
18 // next hint in new early hint response when empty string is passed
20 if (uuid === "new_response") {
22 response.write("\r\n");
23 } else if (uuid === "non_link_header") {
24 response.write("Content-Length: 25\r\n");
26 response.write("\r\n");
28 // either append link in new header or in same header
31 response.write("Link: ");
35 // add query string to make request unique this has the drawback that
36 // the preloaded image can't accept query strings on it's own / or has
37 // to strip the appended "?uuid" from the query string before parsing
38 imgs.push(`<img src="${imgUrl}?${uuid}" width="100px">`);
39 response.write(`<${imgUrl}?${uuid}>; rel=preload; as=image`);
43 // add separator to main document
44 response.write("\r\n\r\n");
47 const body = `<!DOCTYPE html>
54 // main document response
55 response.write("HTTP/1.1 200 OK\r\n");
56 response.write("Content-Type: text/html;charset=utf-8\r\n");
57 response.write("Cache-Control: no-cache\r\n");
58 response.write(`Content-Length: ${body.length}\r\n`);
59 response.write("\r\n");