Skip to content

fix(static-assets): accept Request | string | URL in fetch()#7

Open
jonasnobile wants to merge 1 commit into
mainfrom
fix/static-assets-accept-string
Open

fix(static-assets): accept Request | string | URL in fetch()#7
jonasnobile wants to merge 1 commit into
mainfrom
fix/static-assets-accept-string

Conversation

@jonasnobile
Copy link
Copy Markdown
Member

Summary

Lopata's StaticAssets.fetch() previously accepted only Request, immediately doing new URL(request.url). Production Cloudflare's ASSETS binding accepts Request | string | URL, and @astrojs/cloudflare's generated worker passes a string:

// dist/_worker.js/index.js, generated by @astrojs/cloudflare
return env.ASSETS.fetch(request.url.replace(/\.html$/, ""))

As a result, any Astro SSR project running on lopata 500s on the first non-static route with:

TypeError: "undefined" cannot be parsed as a URL.
    at fetch (lopata/src/bindings/static-assets.ts:62:19)

(Caught while building nctype.com on Astro 5 + @astrojs/cloudflare@12.6.0. The same shape likely affects other Workers SSR adapters that follow the same pattern.)

Fix

Widen fetch() to accept Request | string | URL. Derive url from any of the three; keep request as an optional Request so serveFile()'s existing conditional-request handling (If-None-Match304) still works when a real Request is passed. When only a string/URL is given, request is undefined and serveFile() skips conditional handling — matches production behavior for that call shape.

Test plan

  • bun test tests/static-assets.test.ts → 63 pass, 0 fail (60 existing + 3 new)
  • bunx biome lint src/bindings/static-assets.ts tests/static-assets.test.ts → clean
  • bunx dprint check ... → clean
  • Manual repro: Astro + @astrojs/cloudflare SSR route on lopata returns 200 instead of 500 after patch

The three new tests cover the contract directly:

  • fetch accepts a URL string
  • fetch accepts a URL object
  • fetch with string input skips conditional-request handling (no If-None-Match)

🤖 Generated with Claude Code

Production Cloudflare's ASSETS binding accepts a Request, a URL string, or
a URL object. `@astrojs/cloudflare`'s generated worker calls
`env.ASSETS.fetch(request.url.replace(...))` (a string), so any Astro SSR
project on lopata 500s with `TypeError: "undefined" cannot be parsed as a URL`
on the first non-static route.

Widen `StaticAssets.fetch` to accept all three forms, derive `url` and an
optional `request` from the input. `serveFile()` already accepted `request?`,
so conditional-request handling (If-None-Match → 304) is naturally skipped
when only a URL is passed — same as production behavior in that case.

Adds three test cases covering the new input forms.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jonasnobile jonasnobile force-pushed the fix/static-assets-accept-string branch from 89377c5 to e6e2dd8 Compare May 15, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant