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.

Should fetching local files always results in a TypeError?

See original GitHub issue

Hi there,

When loading local files I bump into TypeError: Network request failed right away due to the response.status of local requests being 0, thus getting rejected by fetch’s initial status handling.

In the README I see the portion on Handling HTTP error statuses, but that would only work if fetch’s initial xhr.onload didn’t reject with a TypeError first.

Would it be reasonable to change fetch’s initial status handling to something like this so that local fetches could make it through?

if ((status !== 0 && status < 100) || status > 599) {
    reject(new TypeError('Network request failed'))
    return
}

For context. I’m working on a project in which the same codebase runs:

  1. In Browser, loading local files via a local dev server (no problems here)
  2. Ejecta in iOS, loading purely local files (which is where I’m getting my status === 0)

Thanks!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
LMLBcommented, May 2, 2016

Firefox does allow XMLHttpRequest and Fetch on local files if the page is a local file and and the requested file is in the same directory (or any number of subdirectories) relative to the page.

0reactions
mislavcommented, Feb 4, 2016

Even though Safari allows XMLHttpRequest using the file: protocol (and we do nothing in the polyfill to prevent it), Chrome and Firefox access restrictions disallow it. Still, the error thrown as a result of that XHR restriction doesn’t match the TypeError thrown when native fetch is used.

But, we might not want to care about this mismatch. “Fixing” the type of the error thrown would be too much work since detecting file: protocol URLs may be tricky.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting "TypeError: Failed to fetch" when the request hasn't ...
I understand this question might have a React-specific cause, but it shows up first in search results for "Typeerror: Failed to fetch" and...
Read more >
Fetch - The Modern JavaScript Tutorial
Fetch. JavaScript can send network requests to the server and load new information whenever it's needed. For example, we ...
Read more >
How to Read React Errors (fix 'Cannot read property of ...
In this post we'll talk about how to fix this one specifically, and along the way you'll learn how to approach fixing errors...
Read more >
The 10 Most Common JavaScript Issues Developers Face
If you need help figuring out why your JavaScript isn't working, consult this list of the 10 most common JavaScript issues from a...
Read more >
CoffeeScript
If you are using CoffeeScript in a project, you should install it locally for ... which can execute scripts, compile .coffee files into...
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