question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`env.ASSETS.fetch(request)` throws ` TypeError: Failed to parse URL from [object Object]`

See original GitHub issue

With the latest beta and alpha release, when fetching assets via:

env.ASSETS.fetch(request)

I am receiving the following error for each static asset when using the built-in proxy:

Could not serve static asset: TypeError: Failed to parse URL from [object Object]

(env.ASSETS.fetch(request.url) works)

I think this was introduced with #105 and is caused by this line: https://github.com/cloudflare/wrangler2/blob/main/packages/wrangler/src/pages.tsx#L601

The line looks as follows in the transpiled code:

const request = new import_undici.Request(input, init);

When providing a Request (and not a URL as string), undici checks instanceof Request.

I’ve modified the package to:

+ console.log(input instanceof import_undici.Request);
 const request = new import_undici.Request(input, init);

This logs false, confirming that the Request I am receiving in my onRequest handler is apparently not an instance of Request object undici is expecting.

That is, I believe the error is caused by onRequest and undici using different Request prototypes.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
GregBrimblecommented, Jan 4, 2022

With #186, wrangler@alpha now has the following behavior:

env.ASSETS.fetch('http://fakehost/myasset.jpg') // works
env.ASSETS.fetch(new Request('/myasset.jpg')) // works
env.ASSETS.fetch('/myasset.jpg') // doesn't work

I’ll re-open this ticket to track behavior on Cloudflare Pages production, and also to see if env.ASSETS.fetch('/myasset.jpg') should work (I think, probably yes).

Please open a separate issue for dotfiles/directories if you do believe there is a bug there.

3reactions
rkusacommented, Jan 3, 2022

Edit: env.ASSETS.fetch(request.url) doesn’t work at all on Cloudflare Pages, returns 406 - Not Acceptable HTTP Code.

Can confirm.

wrangler@pagesCloudflare Pages
env.ASSETS.fetch(request)
env.ASSETS.fetch(request.url)

Another thing that I have noticed not working is .dot-folders/ (maybe files too) are not served. Feature or bug? Although it works locally using wrangler2.

Something I’ll have to check too, as I am a heavy user of .well-known directories.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting "TypeError: Failed to fetch" when the request hasn't ...
I was testing the response to a lost session and got this error. The REST call sends back a redirect from a different...
Read more >
TypeError: Failed to fetch and CORS in JavaScript | bobbyhadz
An incorrect or incomplete URL has been passed to the fetch() method. · The server you are making a request to does not...
Read more >
Failed to parse URL from "/..." when fetching files server-side.
TypeError : Failed to parse URL from "/..." when fetching files server-side. I have a JSON file called sections. json and the following...
Read more >
Node.js v19.3.0 Documentation
If message is provided as third argument it will be used as the error message and the other arguments will be stored as...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
The JavaScript exceptions thrown by JSON.parse() occur when string failed to be ... while reading object contents SyntaxError: JSON.parse: expected property ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found