Sandro Turriate

Coder, cook, explorer

How to fix failed XHR/Fetch requests in Safari

Apr 26, 2022

If you're seeing errors in Safari's Network panel such as "The resource was requested insecurely." or "An error occurred trying to load the resource." for XHR or fetch() requests that normally work, you might be running into an unexpected window.stop() issue.

the resource was requested insecurely.
The resource was requested insecurely.
An error occurred trying to load the resource.
An error occurred trying to load the resource.

I ran into this while debugging a React component. Network requests would randomly fail after pressing the browser's Back button in Safari, even though everything worked perfectly in Chrome.

My first assumption was that Safari's back/forward cache (bfcache) was interfering with the page state. After a lot of digging, though, Safari wasn't the culprit—it was my own cleanup code.

During componentWillUnmount, I call window.stop() to cancel any pending network activity, particularly image requests that may still be queued. I also use an AbortController to cancel in-flight fetch requests. Since I wanted the abort handlers to run before stopping everything else, I deferred the window.stop() call with requestAnimationFrame().

That sequence worked fine in Chrome. In Safari, however, it had an unexpected side effect: after navigating back, the delayed window.stop() call would cancel network requests from the newly mounted component. Instead of an obvious cancellation error, Safari reported the much more confusing message:

"The resource was requested insecurely."

If you're seeing this error and your requests aren't actually being made over HTTP, search your codebase for window.stop(). A delayed call—even one scheduled during cleanup—can inadvertently cancel requests belonging to the next page or component in Safari.

In my case, removing the window.stop() call resolved the issue completely. Since I was already canceling requests with AbortController, window.stop() wasn't providing any additional benefit.