Skip to content

Commit 52639ab

Browse files
midzerLeoColomb
authored andcommitted
Add serving pre-compressed content section (#136)
* added new serving pre-compressed content section * improve section * simplify FilesMatch regex * split each algorithm and move to build config * Add unit tests for pre-compressed content * added br and gz to FileMatchPattern * set utf-8 charset for .gz and .br JavaScript files * capital letters for utf-8 * remove blank * Use native httpd rules for type and charset definition * Fix gz type forced and document it Fix #134 and fix #105 and close #113 as per #105 (comment)
1 parent ce94042 commit 52639ab

10 files changed

+149
-0
lines changed

htaccess.conf

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ enable "src/security/server_software_information.conf"
8585

8686
title "web performance"
8787
enable "src/web_performance/compression.conf"
88+
disable "src/web_performance/pre-compressed_content_brotli.conf"
89+
disable "src/web_performance/pre-compressed_content_gzip.conf"
8890
disable "src/web_performance/content_transformation.conf"
8991
enable "src/web_performance/etags.conf"
9092
enable "src/web_performance/expires_headers.conf"

src/files_match_pattern

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ appcache
77
atom
88
bbaw
99
bmp
10+
br
1011
crx
1112
css
1213
cur
@@ -15,6 +16,7 @@ f4[abpv]
1516
flv
1617
geojson
1718
gif
19+
gz
1820
htc
1921
ic[os]
2022
jpe?g
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# ----------------------------------------------------------------------
2+
# | Brotli pre-compressed content |
3+
# ----------------------------------------------------------------------
4+
5+
# Serve brotli compressed CSS, JS, HTML, SVG, ICS and JSON files
6+
# if they exist and if the client accepts br encoding.
7+
#
8+
# (!) To make this part relevant, you need to generate encoded
9+
# files by your own. Enabling this part will not auto-generate
10+
# brotlied files.
11+
#
12+
# https://httpd.apache.org/docs/current/mod/mod_brotli.html#precompressed
13+
14+
<IfModule mod_headers.c>
15+
16+
RewriteCond %{HTTP:Accept-Encoding} br
17+
RewriteCond %{REQUEST_FILENAME}\.br -f
18+
RewriteRule \.(css|ics|js|json|html|svg)$ %{REQUEST_URI}.br [L]
19+
20+
# Prevent mod_deflate double gzip
21+
RewriteRule \.br$ - [E=no-gzip:1]
22+
23+
<FilesMatch "\.br$">
24+
25+
<IfModule mod_mime.c>
26+
# Serve correct content types
27+
AddType text/css css.br
28+
AddType text/calendar ics.br
29+
AddType text/javascript js.br
30+
AddType application/json json.br
31+
AddType text/html html.br
32+
AddType image/svg+xml svg.br
33+
34+
# Serve correct content charset
35+
AddCharset utf-8 .css.br \
36+
.ics.br \
37+
.js.br \
38+
.json.br
39+
</IfModule>
40+
41+
# Force proxies to cache brotlied and non-brotlied files separately
42+
Header append Vary Accept-Encoding
43+
44+
</FilesMatch>
45+
46+
# Serve correct encoding type
47+
AddEncoding br .br
48+
49+
</IfModule>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# ----------------------------------------------------------------------
2+
# | GZip pre-compressed content |
3+
# ----------------------------------------------------------------------
4+
5+
# Serve gzip compressed CSS, JS, HTML, SVG, ICS and JSON files
6+
# if they exist and if the client accepts gzip encoding.
7+
#
8+
# (!) To make this part relevant, you need to generate encoded
9+
# files by your own. Enabling this part will not auto-generate
10+
# gziped files.
11+
#
12+
# https://httpd.apache.org/docs/current/mod/mod_deflate.html#precompressed
13+
#
14+
# (1)
15+
# Removing default MIME Type for .gz files allowing to add custom
16+
# sub-types.
17+
# You may prefer using less generic extensions such as .html_gz in
18+
# order to keep default behavior regarding .gz files.
19+
# https://httpd.apache.org/docs/current/mod/mod_mime.html#removetype
20+
21+
<IfModule mod_headers.c>
22+
23+
RewriteCond %{HTTP:Accept-Encoding} gzip
24+
RewriteCond %{REQUEST_FILENAME}\.gz -f
25+
RewriteRule \.(css|ics|js|json|html|svg)$ %{REQUEST_URI}.gz [L]
26+
27+
# Prevent mod_deflate double gzip
28+
RewriteRule \.gz$ - [E=no-gzip:1]
29+
30+
<FilesMatch "\.gz$">
31+
32+
# Serve correct content types
33+
<IfModule mod_mime.c>
34+
# (1)
35+
RemoveType gz
36+
37+
# Serve correct content types
38+
AddType text/css css.gz
39+
AddType text/calendar ics.gz
40+
AddType text/javascript js.gz
41+
AddType application/json json.gz
42+
AddType text/html html.gz
43+
AddType image/svg+xml svg.gz
44+
45+
# Serve correct content charset
46+
AddCharset utf-8 .css.gz \
47+
.ics.gz \
48+
.js.gz \
49+
.json.gz
50+
</IfModule>
51+
52+
# Force proxies to cache gzipped and non-gzipped files separately
53+
Header append Vary Accept-Encoding
54+
55+
</FilesMatch>
56+
57+
# Serve correct encoding type
58+
AddEncoding gzip .gz
59+
60+
</IfModule>

test/fixtures/test-pre-brotli.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
raw-content

test/fixtures/test-pre-brotli.js.br

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
brotli-content

test/fixtures/test-pre-gzip.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
raw-content

test/fixtures/test-pre-gzip.js.gz

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gzip-content

test/htaccess_fixture.conf

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ enable "src/security/server_software_information.conf"
8383

8484
title "web performance"
8585
enable "src/web_performance/compression.conf"
86+
enable "src/web_performance/pre-compressed_content_brotli.conf"
87+
enable "src/web_performance/pre-compressed_content_gzip.conf"
8688
enable "src/web_performance/content_transformation.conf"
8789
enable "src/web_performance/etags.conf"
8890
enable "src/web_performance/expires_headers.conf"

test/tests.js

+30
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,36 @@ exports = module.exports = {
841841

842842
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
843843

844+
{
845+
description: 'Test if files are served compressed using precompressed files',
846+
files: {
847+
848+
'test-pre-gzip.js': {
849+
responseHeaders: {
850+
'cache-control': 'max-age=31536000, no-transform',
851+
'content-encoding': 'gzip',
852+
'content-type': 'text/javascript; charset=utf-8'
853+
},
854+
responseBody: 'gzip-content\n'
855+
},
856+
857+
'test-pre-brotli.js': {
858+
requestHeaders: {
859+
'accept-encoding': 'br'
860+
},
861+
responseHeaders: {
862+
'cache-control': 'max-age=31536000, no-transform',
863+
'content-encoding': 'br',
864+
'content-type': 'text/javascript; charset=utf-8'
865+
},
866+
responseBody: 'brotli-content\n'
867+
}
868+
869+
}
870+
},
871+
872+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
873+
844874
{
845875
description: 'Test if filename-based cache busting works',
846876
files: {

0 commit comments

Comments
 (0)