I’m trying to implement Early Hints via service worker to preload images. The images that need to be preloaded are figured out by HTMLRewriter.
Is there any way to add a response header from HTMLRewriter? (to add these images as Link response header)
class ElementHandler {
element(element) {
console.log(`Incoming image: ${element.getAttribute('src')}`);
// Add this image to response header (as link)
}
}
async function handleRequest(req) {
const res = await fetch(req);
return new HTMLRewriter().on('img[loading=eager]', new ElementHandler()).transform(res);
}
You’ll want to have an array that you’re pushing the src attribute into which you can later loop over to construct your link header.
Pseudo:
const imgs: string[] = []
const transformed = new HTMLRewriter()
.on("img", {
// get src attribute - check if it exists
imgs.push(src);
})
imgs won’t actually have anything until you either await the body of transformed or return a response - the joys of async. Since we want to populate imgs in order for us to construct the link header in our response then we can do const body = await transformed.text() which gives us the HTML body of our original request as well as ‘runs’ the ElementHandler so imgs actually has content now.
You can then iterate over imgs using forEach and construct your link header - the link header is comma-separated so you’ll want to take that into consideration and make sure that your final entry doesn’t end in an un-necessary comma.