Skip to content

adapter-cloudflare-workers: Throws KVError if asked for a nonexisting asset #9288

Closed
@tv42

Description

@tv42

Describe the bug

It seems when deployed to Cloudflare Workers, requests for non-existing assets get a 500 internal error, not a 404.
The page says "Error 1101 Worker threw exception".
wrangler tail shows

GET https://example.com/blah/_app/immutable/assets/REDACTED.css - Exception Thrown @ 3/2/2023, 1:15:01 PM
✘ [ERROR]   Error: KVError: could not find blah/_app/immutable/assets/REDACTED.css in your content namespace

(the "REDACTED" is literal, not my editing; see below)

I've observed this two ways:

  1. ablocking browser extension changing request URL from _app/immutable/assets/GoogleAnalytics.HASH.css to _app/immutable/assets/REDACTED.css
  2. old versions of app still live in browser fetching assets that no longer exist after a deploy

What seems to be happening is that @sveltejs/adapter-cloudflare-workers function get_asset_from_kv calls getAssetFromKV from @cloudflare/kv-asset-handler without catching exceptions, and that function is documented to throw KVError on not found.

It seems like it should catch KVError and throw error(404, {...}) in its place.

https://github.com/cloudflare/kv-asset-handler#getassetfromkv

async function get_asset_from_kv(req, env, context, map = mapRequestToAsset) {

Reproduction

https://github.com/tv42/sveltekit-issue9288-cloudflare-adapter-404

Logs

GET https://example.com/blah/_app/immutable/assets/REDACTED.css - Exception Thrown @ 3/2/2023, 1:15:01 PM
✘ [ERROR]   Error: KVError: could not find blah/_app/immutable/assets/REDACTED.css in your content namespace

System Info

Production is Cloudflare Workers. Dev is


  System:
    OS: Linux 6.1 NixOS 22.11 (Raccoon) 22.11 (Raccoon)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 46.64 GB / 62.60 GB
    Container: Yes
    Shell: 5.1.16 - /nix/store/96ky1zdkpq871h2dlk198fz0zvklr1dr-bash-5.1-p16/bin/bash
  Binaries:
    Node: 16.19.1 - /nix/store/i9xar2vndv4qy0w9jzcscfj05jqgigyk-nodejs-16.19.1/bin/node
    npm: 8.19.3 - /nix/store/i9xar2vndv4qy0w9jzcscfj05jqgigyk-nodejs-16.19.1/bin/npm
  Browsers:
    Firefox: 110.0
  npmPackages:
    @sveltejs/adapter-cloudflare: ^2.1.0 => 2.1.0
    @sveltejs/adapter-cloudflare-workers: ^1.0.6 => 1.0.6
    @sveltejs/kit: ^1.9.2 => 1.9.2
    svelte: ^3.55.1 => 3.55.1
    vite: ^4.1.4 => 4.1.4

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions