TypeError: i.mapRequestToAsset is not a function

I am playing with Angular & Workers and have run into a problem I can’t seem to figure out.
This is my worker code:

import { getAssetFromKV, serveSinglePageApp } from ‘@cloudflare/kv-asset-handler’

addEventListener(“fetch”, event =>
{
event.respondWith(handleEvent(event));
})

async function handleEvent(event)
{
try { return await getAssetFromKV(event, { mapRequestToAsset: serveSinglePageApp }); }
catch (err) { return new Response(err.stack || err); }
}

Visiting the deployed site, it shows this:

TypeError: i.mapRequestToAsset is not a function
    at worker.js:1:35947
    at worker.js:1:36975
    at worker.js:1:37048

My code seems to match the example here.

If I remove the options from getAssetFromKV, everything works fine, so what is the problem with specifying options in this case?

Solved. In workers-site\package.json, you need to set @cloudflare/kv-asset-handler to latest. By default, it uses 0.0.5, which does not have serveSinglePageApp. This was confusing because running npm update or outdated in my Angular project did not show anything about it.

I was surprised at 2 things:

  1. The template is using an old version.
  2. The Deploy a React App tutorial says nothing about setting up updates for the sub-project. Correct instruction would probably include something like this: npm update && cd "workers-site" && npm update
2 Likes