Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Serve different assets any given requestKey, even if those are in the ASSET_MANIFEST #158

Closed
@Cherry

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:

if (ASSET_MANIFEST[rawPathKey]) {
requestKey = request
} else if (ASSET_MANIFEST[decodeURIComponent(rawPathKey)]) {
pathIsEncoded = true;
requestKey = request
} else {
requestKey = options.mapRequestToAsset(request)
. I don't really understand what the impact would be by always using the 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:

const requestKey = options.mapRequestToAsset(request)
. This was changed in c97b8dd though.

Let me know if I can provide any more info here!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions