Should fetching local files always results in a TypeError?
See original GitHub issueHi 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:
- In Browser, loading local files via a local dev server (no problems here)
- Ejecta in iOS, loading purely local files (which is where I’m getting my
status === 0)
Thanks!
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.
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.