I have site that is doing a bunch of work in a window.onunload handler. I am working on optimizing it but I don't really understand how it impacts my page performance. It seems like it blocks reloading the page or navigating to another URL on the same domain, but it doesn't seem to block cross domain navigation. It also seems to cause performance issues intermittently on Chrome but is more consistently reproducible on IE. Can someone explain how window.onunload impacts site performance or point me to the spec that does?
How does onunload impact page performance
271 views Asked by Matthew At
2
There are 2 answers
0
jakub.g
On
The main issue with unload handlers is the analytics trackers that do synchronous XHR to send some last portion of tracking data. The synchronous XHR is blocking the navigation to the next page until it completes, hence it's a massive anti-pattern.
Instead, navigator.sendBeacon should be used (https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon). This API was created to address precisely that issue, but since it's new, it's not universally supported (most notably, IE11 lacks support).
(Sync XHR has been used for a long time for this use case to guarantee the execution of the HTTP call; async code in unload handlers will not be executed, and async XHRs issued before navigation might be cancelled upon navigation.)
Related Questions in JAVASCRIPT
- Using Puppeteer to scrape a public API only when the data changes
- inline SVG text (js)
- An array of images and a for loop display the buttons. How to assign each button to open its own block by name?
- Storing the preferred font-size in localStorage
- Simple movie API request not showing up in the console log
- Authenticate Flask rest API
- Deploying sveltekit app with gunjs on vercel throws cannot find module './lib/text-encoding'
- How to request administrator rights?
- mp4 embedded videos within github pages website not loading
- Scrimba tutorial was working, suddenly stopped even trying the default
- In Datatables, start value resets to 0, when column sorting
- How do I link two models in mongoose?
- parameter values only being sent to certain columns in google sheet?
- Run main several times of wasm in browser
- Variable inside a Variable, not updating
Related Questions in GOOGLE-CHROME
- How to tweak the security policy of Chrome, in order to run "unsafe" snippets in the console?
- Is it possible to manipuate 3rd party Chrome Extensions Network Reqeuests?
- undetected_chromedriver urllib.error.URLError
- Load testing k6 browser + docker
- Editor texto estilo WYSIWYG
- NodeJS crashing chrome browser
- Difficulty Accessing HTTP URLs/IP Addresses Due to Browser Redirecting to HTTPS: Seeking Solutions
- Chrome extension MV3: persistent service worker die after wake up from hibernation
- Attempting to Bundle a Require Command For a Chrome Extension
- Launch URL from C# and detect when browser is closed
- Python selenium scrap data from dynamic website table
- Google Chrome is consuming a lot of CPU on a video call?
- Component drawing error React App on Android + Chrome
- Chrome Selenium CDP Bidi API - Next Commands sended to Target Session have no effect while the initial one does work
- Devtools not working when i try to inspect elements for selenium python it goes to previous page
Related Questions in FIREFOX
- (in promise) TypeError: NetworkError when attempting to fetch resource
- Optimizing Selenium script for faster execution
- Apply Firefox policies.json only to Firefox but not to Nightly
- Difficulty Accessing HTTP URLs/IP Addresses Due to Browser Redirecting to HTTPS: Seeking Solutions
- How do I separate emails (from 1 alias) in Thunderbird from invasive websites on Firefox with file browsing scripts? Should I worry?
- Gecko chrome @supports function for strings and numbers
- some CSS property working on chrome's console but not on firefox
- Playwright - Firefox tests time out, but Chromium tests don't
- Notification.permission always return 'denied' on firefox
- Useless horizontal scrollbar in Firefox with absolute positioned div with fixed height
- Why is it impossible to definitively know if your website is running as a PWA or as a website?
- WebRTC from Firefox to Android crashes
- Firefox CSS: Set a variable based on properties of another element
- Getting Negative Time Duration in Sender-to-Client Application using Date.now() api
- Prevent users accessing Firefox menu items
Related Questions in WEB-PERFORMANCE
- 103 Early hints response not sending properly with HTTP/2 request
- TBT but for the whole life of the page
- Why long frame intervals do not the same as long tasks?
- How to measure First Screen Paint?
- High Lighthouse Total Blocking Time despite no long task
- Re-evaluation of scripts in SSR PWAs vs SPA PWAs?
- Does lazy-loading of CSS trigger a recalculation of styles for the whole page and is it important?
- To imitate render blocking and why this attempt has not succeed?
- High-Res profiling using DevTools
- Ask for reasons why a div triggers an LCP and FCP problem
- Chrome dev tools showing weird numbers during performance profiling
- Google tag assistant "No tags found" after switching <Script> strategy from "afterInteractive" to "worker" in Next JS
- Is it possible to reduce the amount of sources in this picture element?
- Discrepancy in Performance Timings between Chrome and Firefox for React App Rendering
- Chrome Dev tool coverage tab for minified css
Related Questions in WINDOW.ONUNLOAD
- Page prevented back/forward cache restoration - Lighthouse, about Facebook comments plugin
- Make window.onbeforeunload executed on page close and not on page refresh
- Async Event on Tab Close
- How to handle window unload event with browser back support in angular?
- Update $(window).on('load', function () for jQuery v3+ | Firefox Issue
- javascript (window).unload in not working for certain chrome versions
- select.onchange in window.onload (JavaScript)
- $(window).unload gets triggered by many events, how can I trigger an event only when a tab is closed?
- JavaScript - send AJAX call on tab close (DELETE request)
- I need to capture the window.onbeforeunload event
- How do I get window onunload event trigger to work in Chrome v73.x?
- Open a Modal when closing browser tab
- Capturing the browser tab reload and tab close functionality
- Preloader Conflicting with Existing Javascript
- Get user's response for beforeunload event if the user leaves the domain
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
It would be very intresting to see a speed test in different browsers using windows.onload vs. other methods. Maybe I’ll test it out tomorrow and get back to you.
Until then, here’s a link you may find useful
stackoverflow.com/questions/20180251/when-to-use-window-onload/…