Invalid pathname when stringifying URL

I have the following code in my worker to copy the incoming request so I can mutate it:

const url = new URL(fetchEvent.request.url)
url.hostname = 'some.other.hostname'
request = new Request(url, fetchEvent.request)

That works fine for 99.99999% of requests, but a few cause the exception

Invalid pathname when stringifying URL

Although it’s probably not a valid request, it is a perfectly well-formed URL:

// Firefox:
new URL('').toString()

// Cloudflare:
try {
  new URL('').toString()
} catch (e) {
  e.message // "Invalid pathname when stringifying URL"

Why is Cloudflare complaining?

This only happens in production environments, not when using wrangler dev.

Any solution for this? I’m seeing the same thing


Can you try with the new URL parser? It shouldn’t have these weird issues anymore - Compatibility dates · Cloudflare Workers docs


I just deployed our workers with this update. I’ll post here in a few days when I can see the results.

Yes, updating to the new parser seems to have fixed the problem!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.