Problem/Motivation

Testing Drupal 9 Websites with Google Pagespeed or GTMetrix

If your static files do not change (or you have an appropriate strategy for cache busting), then we recommend setting your cache policy to 6 months or 1 year.

For completed websites, things like global CSS/JS files, logos, images, etc., generally do not change much, and so 6 months or 1 year is a good cache expiry to work with.

(https://gtmetrix.com/serve-static-assets-with-an-efficient-cache-policy....)

We recommend a minimum cache time of one week and preferably up to one year for static assets, or assets that change infrequently. If you need precise control over when resources are invalidated, we recommend using a URL fingerprinting or versioning technique - see invalidating and updating cached responses link above.

https://developers.google.com/speed/docs/insights/LeverageBrowserCaching

Steps to reproduce

Test a Drupal 9 website using Apache (.htaccess) with tools like

and see the complaints about the too low expiration

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

The Drupal root .htaccess file now caches all files for 1 year instead of two weeks. This bring the value in line with industry standards.

CommentFileSizeAuthor
#20 2788.diff1.14 KBanybody
#19 2861.diff1.14 KBanybody
#9 2788.diff517 bytesanybody

Issue fork drupal-3311406

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Anybody created an issue. See original summary.

anybody’s picture

Version: 9.5.x-dev » 10.1.x-dev
anybody’s picture

Status: Active » Needs review

I decided to use
ExpiresDefault "access plus 1 year" notation, as it's easier to read than the seconds syntax used before, but of course we can also use that.

I think this is a lot more self-explaining.

anybody’s picture

Title: .htaccess ExpiresDefault (2W) is much too low » .htaccess ExpiresDefault (2W) is much too low. Should be ~1Y
cilefen’s picture

Category: Bug report » Feature request

This is a behavioral change where nothing is actually “broken”.

anybody’s picture

Thanks @cilefen you're right! Feature request is better here.

wim leers’s picture

anybody’s picture

Related issues: +#3159964: Improve example for https redirect in .htaccess
StatusFileSize
new517 bytes

Here's the patch to review, so we don't have to rebase this all the time ;)

#3159964: Improve example for https redirect in .htaccess fixed the same in the dropsolid_rocketship profile, so we're getting closer to RTBC as this was tested on many sites before.

anybody’s picture

Status: Needs review » Needs work

Needs a reroll, the .htaccess has been changed in the meantime, as it seems.

Grevil made their first commit to this issue’s fork.

grevil’s picture

Status: Needs work » Needs review

Rebased!

anybody’s picture

Status: Needs review » Needs work

Thanks @Grevil. Having a closer look at the failing tests, 7 of them seem unrelated to me, but this one has to be fixed:

1	Composer.Composer
	1	Composer.Drupal\Tests\ComposerIntegrationTest
	✗	
Drupal\Tests\ComposerIntegrationTest

fail: [Other] Line 0 of sites/default/files/simpletest/phpunit-117.xml:
PHPUnit Test failed to complete; Error: PHPUnit 9.5.24 �[44;37m#StandWith�[0m�[43;30mUkraine�[0m

Testing Drupal\Tests\ComposerIntegrationTest
........S.................................F.................      60 / 60 (100%)

Time: 00:00.102, Memory: 6.00 MB

There was 1 failure:

1) Drupal\Tests\ComposerIntegrationTest::testExpectedScaffoldFiles with data set #6 ('.htaccess', 'assets/scaffold/files/htaccess')
Scaffold source and destination files must have the same contents.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
   # Enable expirations.\n
   ExpiresActive On\n
 \n
-  # Cache all files for 2 weeks after access (A).\n
-  ExpiresDefault A1209600\n
+  # Cache all files for 1 year after access.\n
+  ExpiresDefault "access plus 1 year"\n
 \n
   \n
     # Do not allow PHP scripts to be cached unless they explicitly send cache\n

/var/www/html/vendor/phpunit/phpunit/src/Framework/Constraint/Equality/IsEqual.php:96
/var/www/html/core/tests/Drupal/Tests/ComposerIntegrationTest.php:203
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:726
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php:672
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php:672
/var/www/html/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:673
/var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php:143
/var/www/html/vendor/phpunit/phpunit/src/TextUI/Command.php:96

FAILURES!
Tests: 60, Assertions: 398, Failures: 1, Skipped: 1.
anybody’s picture

Status: Needs work » Needs review

Here we go! :)

grevil’s picture

I think the "\n" will throw an error.

anybody’s picture

Much traffic at 10.1.x-dev while I think the patch / MR itself is fine now. Should we better target 10.0.x for review?

anybody’s picture

Version: 10.1.x-dev » 10.0.x-dev

anybody’s picture

StatusFileSize
new1.14 KB
anybody’s picture

StatusFileSize
new1.14 KB
grevil’s picture

Version: 10.0.x-dev » 10.1.x-dev

I don't know, creating separate merge requests and changing the version will just create chaos. Setting version back to 10.1.x.

Status: Needs review » Needs work

The last submitted patch, 20: 2788.diff, failed testing. View results

grevil’s picture

Status: Needs work » Needs review
nod_’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

test seem like one of the random failures, MR needs reroll

anybody’s picture

Status: Needs work » Needs review

Rerolled, thx. As this is a "tiny" (LOC) change, we should perhaps try to decide what to do here for not having to rebase this easy day? :)

nod_’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs reroll

not sure I see any changes as to what happened previously but it might be a config issue on my end.

nod_’s picture

Status: Reviewed & tested by the community » Needs work

might want to be more specific in what gets cached a year. usually it's for CSS/JS/images not redirects, that might be an issue to cache those for a year instead of 2 weeks.

also need change to the web.config file maybe?

anybody’s picture

Status: Needs work » Needs review

Thanks @_nod! Looking at #3054821: Include Cache-Control header on 301 redirects. and thinking about explicit rules per mime type I think we should keep this as-is and let the redirect do its own thing in the other issue.

Otherwise, this will end up for us in having to update the mime types for every new image type etc. - I don't thing that's a good thing.
For such an example see https://www.supertechcrew.com/htaccess-rules-security-cache-redirect/ > "Caching rules"

Do you agree?

Setting this back to "Needs review" for that reason. I still think the changes are good.

anybody’s picture

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Seems fine to me. lets see what the committers say.

nod_’s picture

Looked at the other issue and I agree with #29, that makes sense to me RTBC +1

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record, +Needs release note

Changes to .htaccess need a release note and a CR because they can require users to make updates since this can be hand edited. See #2854817: Duplicate X-Content-Type-Options headers both with the value nosniff as an example of the things needed.

grevil’s picture

Issue summary: View changes
Status: Needs work » Needs review

I created a CR draft here: https://www.drupal.org/node/3349094 and a release note snippet got added in the issue summary.

Please correct me if either the snippet, or the CR draft is incorrect, as I don't have enough insights.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record

Took a look at the change record and change makes sense. Examples of why the change happened should be useful to others.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 67357a3 and pushed to 10.1.x. Thanks!

  • alexpott committed 67357a32 on 10.1.x
    Issue #3311406 by Anybody, Grevil, nod_: .htaccess ExpiresDefault (2W)...
alexpott’s picture

Discussed with @catch we agreed that js and image styles all have cache busting logic. This will change how long logos and favicons for. Even today if you change these it would be recommended to use a different file name because they are already cached for 2 weeks.

anybody’s picture

Funny question, I know. But should we backport this to Drupal 7? Also 14d there...

longwave’s picture

@Anybody I would open a separate issue to discuss - D7 is somewhat more conservative around changes and the considerations there might be different. If someone was already affected by this on D7 they likely would have already made this change to their site; nobody should be building new sites on D7.

quietone’s picture

Tweaked the release note snippet and changing tags.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

quietone’s picture

Change record published.