Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve caching policy use immutable when loading versionned assets #31141

Merged
merged 1 commit into from
Feb 17, 2022

Conversation

CarlSchwan
Copy link
Member

  • Cache the CSS with the version in the url. This makes most js and CSS requests to
    be cached by the browser

  • Force caching previews, the ETag is in the URL so that if the propfind
    gives a new ETag, we will refresh it otherwise it's no use to try to
    fetch the new ETag and do tons of DB queries

Tested with firefox and 'debug' => false (important so that the js/css
urls are generated with ?v= parameter)

Signed-off-by: Carl Schwan [email protected]

@CarlSchwan CarlSchwan changed the title Improve caching policy Improve caching policy use immutable when loading versionned urls Feb 11, 2022
@CarlSchwan CarlSchwan changed the title Improve caching policy use immutable when loading versionned urls Improve caching policy use immutable when loading versionned assets Feb 11, 2022
@CarlSchwan CarlSchwan self-assigned this Feb 11, 2022
@solracsf
Copy link
Member

solracsf commented Feb 12, 2022

Immutable has very low support/adoption.
https://caniuse.com/mdn-http_headers_cache-control_immutable

image

@CarlSchwan
Copy link
Member Author

@acsfer I just read completely https://bugs.chromium.org/p/chromium/issues/detail?id=611416 and it seems to me like chromium has a heuristic to determine when it should try to see if the server updated the assets or not. According to the comments, it's not as good as the solution in firefox and safari but still ok.

There were also some commenters (e.g. the BBC) who said that even if firefox was used by 10% of their users, it represented 30% of their 304 response code. So a nice improvement overall in my opinion for client responsivity and server load.

@solracsf
Copy link
Member

solracsf commented Feb 12, 2022

@CarlSchwan no problem for Immutable (browser respect it or not, it can not hurt) but put the static assets as Private, I just completely disagree on this. The assets that must be private are already set as-is (example), we should keep it that way. In large deployments, the Proxy cache is very important - this PR just breaks it.

Copy link
Member

@PVince81 PVince81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

.htaccess Outdated Show resolved Hide resolved
.htaccess Outdated Show resolved Hide resolved
@solracsf
Copy link
Member

To fix?

There was 1 failure:
--
156 |  
157 | 1) OCA\Theming\Tests\Controller\IconControllerTest::testGetThemedIcon
158 | Failed asserting that two objects are equal.
159 | --- Expected
160 | +++ Actual
161 | @@ @@
162 | OCP\AppFramework\Http\FileDisplayResponse Object (
163 | 'file' => OC\Files\SimpleFS\SimpleFile Object (...)
164 | 'headers' => Array (
165 | -        'Cache-Control' => 'private, max-age=86400, must-revalidate'
166 | +        'Cache-Control' => 'private, max-age=86400, immutable'
167 | 'X-Request-Id' => 'vQ4PEXfeRM0isjVqrGMN'
168 | 'Content-Security-Policy' => 'default-src 'none';base-uri '...'none''
169 | 'Feature-Policy' => 'autoplay 'none';camera 'none'...'none''
170 |  
171 | /drone/src/apps/theming/tests/Controller/IconControllerTest.php:108

@szaimen szaimen added the pending documentation This pull request needs an associated documentation update label Feb 14, 2022
@CarlSchwan
Copy link
Member Author

Doc PR: nextcloud/documentation#8027 (I didn't test it, can an nginx expert try to see if the syntax is correct?)

@solracsf solracsf removed the pending documentation This pull request needs an associated documentation update label Feb 15, 2022
* Cache css with version in url. This makes most js and css requests to
  be cached by the browser

* Force caching previews, the etag is in the url so that if the propfind
  gives a new etag, we will refresh it otherwise it's no use to try to
  fetch the new etag and do tons of DB queries

Tested with firefox and 'debug' => false (important so that the js/css
urls are generated with ?v= parameter)

Signed-off-by: Carl Schwan <[email protected]>
@CarlSchwan
Copy link
Member Author

For the doc/nginx PR, a user told me it didn't work out for them. Could someone familiar with Nginx double-check? nextcloud/documentation#8027

@CarlSchwan
Copy link
Member Author

New doc PR to fix the issue: nextcloud/documentation#8046

@CarlSchwan CarlSchwan merged commit ac4978e into master Feb 17, 2022
@CarlSchwan CarlSchwan deleted the fix/better-cache-policy branch February 17, 2022 15:58
@CarlSchwan
Copy link
Member Author

backport?

@CarlSchwan
Copy link
Member Author

/backport to stable23

@CarlSchwan
Copy link
Member Author

/backport to stable22

@@ -110,10 +110,10 @@ public function __construct() {
* @return $this
* @since 6.0.0 - return value was added in 7.0.0
*/
public function cacheFor(int $cacheSeconds, bool $public = false) {
public function cacheFor(int $cacheSeconds, bool $public = false, bool $immutable = false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be added to #29914 as it's changing public API (apps might extend Response to implement custom responses)? And backporting yields a new minor for the old stables?

Copy link
Contributor

@PhrozenByte PhrozenByte Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumping this due to the imminent release of the first NC24 beta and patch releases for old stables.

Breaking 3rd-party apps in patch releases is bad 😉

@CarlSchwan @PVince81 @ChristophWurst @solracsf @Pytal

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an example of an app that did extend Response to modify the cacheFor method?

Copy link
Contributor

@PhrozenByte PhrozenByte Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/nextcloud/cms_pico/blob/00c2c4c3a3058b8138ac540b3bb55a30253df3dd/lib/Http/PicoAssetResponse.php#L99-L126

Just to get this straight: I don't really have any issue with this change, it's fine, there must be a way to introduce features like this. However, it needs documentation and it must yield a new minor for the old stables, as app devs don't expect such things to change in patch releases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For others to follow: Also see nextcloud/cms_pico#195 for the ongoing discussion.

@blizzz blizzz added this to the Nextcloud 24 milestone Mar 10, 2022
PhrozenByte added a commit to nextcloud/cms_pico that referenced this pull request Mar 15, 2022
PhrozenByte added a commit to nextcloud/cms_pico that referenced this pull request Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants