"fetch" supports a "keepAlive" option to make it outlive page navigations (#tilPost)

This site uses Umami as a self-hosted and privacy-first analytics tool. An item in Umami’s changelog caught my eye – "Update tracker/index.js: SendBeacon() to Fetch API".
sendBeacon is a JavaScript method to send requests …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

This site uses Umami as a self-hosted and privacy-first analytics tool. An item in Umami's changelog caught my eye – "Update tracker/index.js: SendBeacon() to Fetch API".

sendBeacon is a JavaScript method to send requests to an analytics server. These requests are supposed to be async, not be canceled and outlive the current navigation. But apparently, sendBeacon is sometimes blocked by ad blockers.

That's not a big deal, though, because today I learned I can drop sendBeacon from my memory entirely and use the fetch method with a keepalive option. 👇

fetch(`${root}/api/collect`, {
  method: 'POST',
  body: data,
  // note the `keepalive` option
  keepalive: true,
});

fetch with a keepalive option has the same characteristics as sendBeacon and acts as its replacement. This is good to know!

Don't confuse the keepalive fetch attribute with the Keep-Alive HTTP header.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2022-06-11T22:00:00+00:00) "fetch" supports a "keepAlive" option to make it outlive page navigations (#tilPost). Retrieved from https://www.scien.cx/2022/06/11/fetch-supports-a-keepalive-option-to-make-it-outlive-page-navigations-tilpost/

MLA
" » "fetch" supports a "keepAlive" option to make it outlive page navigations (#tilPost)." Stefan Judis | Sciencx - Saturday June 11, 2022, https://www.scien.cx/2022/06/11/fetch-supports-a-keepalive-option-to-make-it-outlive-page-navigations-tilpost/
HARVARD
Stefan Judis | Sciencx Saturday June 11, 2022 » "fetch" supports a "keepAlive" option to make it outlive page navigations (#tilPost)., viewed ,<https://www.scien.cx/2022/06/11/fetch-supports-a-keepalive-option-to-make-it-outlive-page-navigations-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » "fetch" supports a "keepAlive" option to make it outlive page navigations (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/11/fetch-supports-a-keepalive-option-to-make-it-outlive-page-navigations-tilpost/
CHICAGO
" » "fetch" supports a "keepAlive" option to make it outlive page navigations (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2022/06/11/fetch-supports-a-keepalive-option-to-make-it-outlive-page-navigations-tilpost/
IEEE
" » "fetch" supports a "keepAlive" option to make it outlive page navigations (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2022/06/11/fetch-supports-a-keepalive-option-to-make-it-outlive-page-navigations-tilpost/. [Accessed: ]
rf:citation
» "fetch" supports a "keepAlive" option to make it outlive page navigations (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2022/06/11/fetch-supports-a-keepalive-option-to-make-it-outlive-page-navigations-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.