jquery / jquery/jquery

animate() in Chrome triggers hundreds of HTTP requests

Open
#4,968 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted
Dominant language
JavaScript
Stars
59.8k
Forks
20.4k
Avg merge
17h 32m
Merged PRs (30d)
4

Description

Description

Using .animate() on an element with a background image causes Chrome to re-request the background image 60 times per second, but only if:

  • Chrome is in Incognito mode.
  • You refresh the page after the initial pageload.
  • The CSS that sets the background image is in an external CSS file.

The avalanche of requests does not happen on the initial pageload -- only if you reload the page -- and the requests are always served from the in-memory cache of the image, so it's not actually placing load on the Web server, but the behavior seems... bad.

I've only been able to check this behavior in Chrome 95.0.4638.69 on Mac OS 11.6. The example uses jQuery 3.6.0, but I originally noticed it in a project that uses jQuery 2.2.4. Besides jQuery itself, the only JavaScript being run is

jQuery( function ( $ ) {
	$('#animate-me').animate({
	    'width' : '+=100',
	}, 5000, 'linear' );
} );
Link to test case

There is an example here: https://www.chrisfinke.com/files/jquery-animate-bug/

To reproduce:

  1. Open a Chrome Incognito window.
  2. Open the Developer Tools and select the Network tab.
  3. Load the URL https://www.chrisfinke.com/files/jquery-animate-bug/ Note that the 5-second animation completes, and there are a total of four HTTP requests -- the HTML, the stylesheet, jQuery, and the background image (plus the favicon, potentially).
  4. Reload the page by clicking the reload button. Note that this time, there are 300+ requests -- the same four as the original pageload, but an additional 300 requests for the background image.

This behavior persists even if the animation does not visibly affect the element on the page. For example, if instead of changing the width, it increases the font size (when there is no text in the element).

There are a couple of workarounds for anyone who is experiencing this bug in their own code:

  1. Set the background image via an inline style attribute.
  2. Set the background image via a <style> block in the HTML.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked test case and the provided jQuery animate() call, reproducing the reload behavior in Chrome Incognito with the Network panel open. Compare the external stylesheet setup with the inline style and HTML style-block workarounds; done means determining whether jQuery can prevent the repeated requests or documenting that the behavior is browser-specific.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.