Serve different assets any given requestKey
, even if those are in the ASSET_MANIFEST
#158
Description
It would be awesome if we could "hijack" requests and serve different assets to that request, even if that filename exists in the ASSET_MANIFEST
. An immediate use-case for this would be to serve newer image formats to clients that support them, such as WebP or AVIF images, even when a JPEG is requested.
The mapRequestToAsset
would be perfect for this use-case, if it allowed this function to entirely override the requestKey
. The code in this file doesn't seem to support this though:
Lines 104 to 110 in 3228cd7
mapRequestToAsset
if a custom one was defined for this request, but that would be a possible solution here.
Pseudo-code example if this were possible:
// handle images rewrite to webp
if(url.pathname.match(imageRegex)){
// request for an image. Check if we can serve webp instead
if(event.request.headers.has('accept') && event.request.headers.get('accept').match(/image\/webp/)){
options.mapRequestToAsset = (request) => {
const parsedUrl = new URL(request.url);
parsedUrl.pathname = parsedUrl.pathname.replace(imageRegex, '.webp');
return new Request(parsedUrl.toString(), request);
};
}
}
Note: This actually used to work in much older versions of kv-asset-handler
:
Line 98 in ce0229f
Let me know if I can provide any more info here!
Activity