Make WordPress Core

Opened 8 years ago

Last modified 5 days ago

#40602 assigned enhancement

Implement immutable cache headers

Reported by: programmin's profile programmin Owned by: pbearne's profile pbearne
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Script Loader Keywords: good-first-bug has-patch has-unit-tests
Focuses: performance Cc:

Description

When you have js files that will not change in a certain version of Wordpress, why not set the immutable cache that is enabled on various browsers?

https://hacks.mozilla.org/2017/01/using-immutable-caching-to-speed-up-the-web/

For example this url should never change output unless user changed the wordpress version, in which case the ver= will change, forcing browser to load that url.

/wp-admin/load-scripts.php?c=gzip&load%5B%5D=jquery-core,jquery-migrate,jquery-ui-core,jquery-ui-widget,jquery-ui-mouse,underscore,mediaelement,jquery-ui-sortable&ver=4.7.4

Change History (9)

#1 @dd32
8 years ago

  • Component changed from General to Script Loader
  • Keywords needs-patch added

I have no issue setting this flag, I like the suggestion after reading a little about it.

I think the only scenario we should set it though, is in the case where $_GET['ver'] is present and it does not contain a - character (in the ver param) to avoid caching issues while testing beta's, RC's, and primarily nightlies (where in, the ver may not be unique if operating from the /src directory)

#2 @ocean90
8 years ago

Previously: #28722

#3 @johnbillion
8 years ago

  • Summary changed from Why doesn't Wordpress use Immutable Cache? to Implement immutable cache headers
  • Version trunk deleted

This ticket was mentioned in Slack in #core-performance by swissspidy. View the logs.


5 days ago

#5 @swissspidy
5 days ago

  • Keywords good-first-bug added
  • Milestone changed from Awaiting Review to Future Release

#6 @pbearne
5 days ago

  • Owner set to pbearne
  • Status changed from new to assigned

#7 @samiamnot
5 days ago

Interesting and likely no harm in adding it now, but as per CanIUse.com Cache-Control: immutable, only 19.66% of global users are on supporting browsers. Chromium never implemented it. The good news is that it looks like someone at Microsoft just accepted Chromium bug 41253661 - Implement Cache-Control: immutable that was filed in 2016.

This ticket was mentioned in PR #8307 on WordPress/wordpress-develop by @pbearne.


5 days ago
#8

  • Keywords has-patch has-unit-tests added; needs-patch removed

…p and modifies load-scripts.php to include the immutable directive in the Cache-Control` header.

Explanation of Changes:

  • tests/phpunit/tests/dependencies/scripts.php: A new test, test_load_scripts_headers(), is added. This test enqueues a couple of scripts ('jquery' and 'underscore'), simulates a request to load-scripts.php, and then asserts that the response has the correct headers. Specifically, it checks for Content-Type, Cache-Control, Expires, and Etag. It also checks that the response body isn't empty and contains a string from one of the enqueued scripts. This ensures that the scripts are actually being concatenated and served.
  • src/wp-admin/load-scripts.php: The Cache-Control header is modified to include the immutable directive. This tells browsers and caching proxies that the resource will not change, allowing them to cache it more aggressively. The max-age directive is already present and sets the cache expiration time.

Benefits of the Change:

  • Improved Caching: The immutable directive enhances browser caching. Once a browser caches a script marked as immutable, it won't even check for updates until the cache expires (defined by max-age). This reduces the number of requests to the server, leading to faster page load times for returning visitors.
  • Reduced Server Load: Fewer requests mean less work for the server, improving overall performance and scalability.
  • Better Test Coverage: The added test ensures that the headers are being set correctly and that the concatenation process is working as expected. This helps prevent regressions in the future.

Potential Considerations:

  • Versioning: Because immutable tells the browser not to check for updates, it's crucial to incorporate versioning into your script URLs whenever you make changes to the scripts. This can be done by appending a query parameter (e.g., ?ver=1.2.3) or by using a content hash in the filename. Without versioning, users might be stuck with outdated cached versions of your scripts.

The changes are well-justified and improve caching behavior. The test ensures the change works and prevents future regressions. Just remember the crucial point about versioning when using the immutable directive.

#9 @pbearne
5 days ago

Some of the new tests are failing. I am not sure we do the new test

Happy to pull if it will help

Note: See TracTickets for help on using tickets.