Why does Safari execute fetch() twice when I refresh?

408 Views Asked by At

I'm fiddling around with HTML/JS on Glitch. It's fun! I started playing with the fetch API to make calls so I could interact with a SQLite database, and I noticed something odd as I pecked away. I had logging in my methods, and they were being called twice.

So I've made a project to demonstrate: http://safari-fetchfetch.glitch.me

You can fork it/poke at it to see the code. It logs server-side every time the endpoint /fetch is called. I would expect every access to emit one logging message.

This happens on Safari's first load. However, on every subsequent load the server logs two hits to the endpoint. Why? I've tried in Chrome and can't reproduce it. It doesn't seem to happen in a Safari Private Browsing instance.

I've tried logging more parts of the request, but nothing looks fishy. I've seen some pages that say it might be related to CORS, but this is same-origin, right? Even when I explicitly set options to use same-origin policy, it continues. When I use Safari's inspector to view the headers for that request, I don't see the things that seem to be associated with CORS.

What's happening?

1

There are 1 best solutions below

0
On

Could be this bug.

(Late I know, but for others like me whose search found this SO question but not other relevant pages.)