Why is Safari duplicating GET request but Chrome is not?

时间秒杀一切 提交于 2020-01-13 18:33:27

问题


Update

TL;DR: This is potentially a bug in Safari and/or Webkit.

Longer TL;DR: In Safari, after the Fetch API is used to make a GET request, Safari will automatically (and unintentionally) re-run the the request when the page is reloaded even if the code that makes the request is removed.

Newly discovered minimal reproducible code (courtesy of Kaiido below):

Front end

<script>fetch('/url')</script>

Original Post

I have a javascript web application which uses the fetch API to make a GET request on a Node.js (express) server.

In Safari (where the problem is): The request completes as expected.

BUT

When I reload the page it will resend the GET request and thus cause duplicates.

In Chrome (acting as control): Everything works (ie no duplicates).

HTML

<div id="buttonTarget"></div>

Front End JS

class ErrorReproduce{
     constructor(){}

     makeButton(){
          let button = document.createElement('button');
          button.innerText = 'Send get request';
          button.onclick = ()=>{
               this.asyncMethod();
          };
          buttonTarget.appendChild(button);
     }//end makeButton()

     async asyncMethod(){
          let data = await fetch('path/to/testError', {
               method: 'GET',
               cache:'no-cache',
               credentials: 'same-origin',
               headers: {
                    'Content-Type': 'application/json',
               },
          }).then(response => response.json());
     }//end asyncMethod
}//end ErrorReporduce

let errRepro = new ErrorReproduce();
errRepro.makeButton();

Backend JS

router.get('path/to/testError',(req,res)=>{
     res.send({ok:true});
})

How to reproduce

  1. Click button - see the GET request in the log

  2. Reload page WITHOUT re-clicking the button - see the duplicate request in the log

Expected Behavior

I expect that after clicking the button and reloading the page WITHOUT pressing the button again that there will not be a duplicate request, but the request is indeed duplicated IMMEDIATELY by the browser after the page reloads.

Server log after page reload Safari (error):

GET /path/to/testError 304 3.206 ms - -

... (other normal requests) ...

Server log after page reload Chrome (expected):

... (other normal requests) ...

Edits

I tried setting the type attribute of the to 'button' (bug persists)

I tried using CMD+R and the Reload page button (bug in both)

Link to bug report

Bug Report


回答1:


Bug has been fixed in WebKit. Link to Changeset 247276 in webkit



来源:https://stackoverflow.com/questions/56728800/why-is-safari-duplicating-get-request-but-chrome-is-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!