Skip to content

Commit 120d599

Browse files
committed
Add caching directive for static content
Images and other static assets that are currently not cache-busted will be cached for a week. CSS and JS files (which we currently cache-bust) can be cached for an arbitrarily long time, so we set it to a year. This change fixes F grades on performance test sites in the "Cache static content" category. Now we get an A! NOTE: No particularly strong reason why `mod_headers` was used to set the "max-age" header instead of `mod_expires`.
1 parent ac023c7 commit 120d599

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ RUN rm -rf /var/www/html/ && ln -s "$(pwd)/htdocs" /var/www/html
5353
# Make the upload directory writable by the apache user
5454
RUN chown www-data ./htdocs/upload
5555

56+
# Leverage browser caching of static assets using apache's mod_headers
57+
COPY apache/cache-static.conf /etc/apache2/conf-enabled/cache-static.conf
58+
RUN a2enmod headers
59+
5660
# Store the git commit hash of the repo in the final image
5761
COPY .git/HEAD .git/HEAD
5862
COPY .git/refs .git/refs

apache/cache-static.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Static assets (with no cache-bust), cache for 1 week
2+
<FilesMatch "\.(ico|jpg|png|gif|svg|woff)$">
3+
Header set Cache-Control "max-age=604800, public"
4+
</FilesMatch>
5+
6+
# Static assets (with cache-bust), cache for 1 year
7+
<FilesMatch "\.(js|css)$">
8+
Header set Cache-Control "max-age=31536000, public"
9+
</FilesMatch>

0 commit comments

Comments
 (0)