Worker appends /index.html to some of my files (with some file extension)

Hi there,

I have a simple worker. Pretty much empty default template. One folder, in this folder a .zip file and one more file with .vsto extension. This .vsto file is just a text file with XML inside of it.

When I publish everything and try to download that zip file (e.g. https://.../xyz.zip) - no problem. When I try to navigate to that .vsto file (e.g. https://.../abc.vsto), I get this:

could not find abc.vsto/index.html in your content namespace

I would just need to download it/or display it (it’s a text) as it is. That’s all. There is this index.html appended though and prevents me from reaching this file.

I suppose I need to set the correct content-type to that particular response, but not sure where the index.html gets appended from and how to get rid of it.

Any help is appreciated,
Thanks

OK, I got it figured. For anyone who’s interested…

Cloudflare’s mapRequestToAsset JS method is checking for mime type and if it isn’t recognized it concat the pathname with the /index.html that caused me my problems (check the source code here: https://github.com/cloudflare/kv-asset-handler/blob/d4fa91fae1806e30270dcc5f39f581ea37362a59/src/index.ts#L22).

So, I used the mapRequestToAsset option property of getAssetFromKV to basically replace the /index.html from the url. Like this:

addEventListener('fetch', event => {
  try {
    event.respondWith(handleEvent(event));
   ...

…where handleEvent looks like:

async function handleEvent(event) {
  const url = new URL(event.request.url)
  let options = {}

  options.mapRequestToAsset = customAssetsHandler();
  try {
    return await getAssetFromKV(event, options)
  ...

…and customAssetsHandler looks like:

function customAssetsHandler() {
  return (request) => {
    //return new Request(bugFreeUrl, defaultAssetKey);
    let defaultAssetKey = mapRequestToAsset(request);
    let url = new URL(defaultAssetKey.url);
    return new Request(url.toString().replace('/index.html', ''), defaultAssetKey); //replace here
  }
}

Please note that my way of replacing the /index.html doesn’t have to work for everyone, because it will remove the index.html anywhere in the URL string. In my simple case, it’s sufficient though.

Works well.