Skip to content

Commit

Permalink
Add serving pre-compressed content section (#136)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
midzer authored and LeoColomb committed Apr 14, 2018
1 parent ce94042 commit 52639ab
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 0 deletions.
2 changes: 2 additions & 0 deletions htaccess.conf
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ enable "src/security/server_software_information.conf"

title "web performance"
enable "src/web_performance/compression.conf"
disable "src/web_performance/pre-compressed_content_brotli.conf"
disable "src/web_performance/pre-compressed_content_gzip.conf"
disable "src/web_performance/content_transformation.conf"
enable "src/web_performance/etags.conf"
enable "src/web_performance/expires_headers.conf"
Expand Down
2 changes: 2 additions & 0 deletions src/files_match_pattern
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ appcache
atom
bbaw
bmp
br
crx
css
cur
Expand All @@ -15,6 +16,7 @@ f4[abpv]
flv
geojson
gif
gz
htc
ic[os]
jpe?g
Expand Down
49 changes: 49 additions & 0 deletions src/web_performance/pre-compressed_content_brotli.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# ----------------------------------------------------------------------
# | Brotli pre-compressed content |
# ----------------------------------------------------------------------

# Serve brotli compressed CSS, JS, HTML, SVG, ICS and JSON files
# if they exist and if the client accepts br encoding.
#
# (!) To make this part relevant, you need to generate encoded
# files by your own. Enabling this part will not auto-generate
# brotlied files.
#
# https://httpd.apache.org/docs/current/mod/mod_brotli.html#precompressed

<IfModule mod_headers.c>

RewriteCond %{HTTP:Accept-Encoding} br
RewriteCond %{REQUEST_FILENAME}\.br -f
RewriteRule \.(css|ics|js|json|html|svg)$ %{REQUEST_URI}.br [L]

# Prevent mod_deflate double gzip
RewriteRule \.br$ - [E=no-gzip:1]

<FilesMatch "\.br$">

<IfModule mod_mime.c>
# Serve correct content types
AddType text/css css.br
AddType text/calendar ics.br
AddType text/javascript js.br
AddType application/json json.br
AddType text/html html.br
AddType image/svg+xml svg.br

# Serve correct content charset
AddCharset utf-8 .css.br \
.ics.br \
.js.br \
.json.br
</IfModule>

# Force proxies to cache brotlied and non-brotlied files separately
Header append Vary Accept-Encoding

</FilesMatch>

# Serve correct encoding type
AddEncoding br .br

</IfModule>
60 changes: 60 additions & 0 deletions src/web_performance/pre-compressed_content_gzip.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# ----------------------------------------------------------------------
# | GZip pre-compressed content |
# ----------------------------------------------------------------------

# Serve gzip compressed CSS, JS, HTML, SVG, ICS and JSON files
# if they exist and if the client accepts gzip encoding.
#
# (!) To make this part relevant, you need to generate encoded
# files by your own. Enabling this part will not auto-generate
# gziped files.
#
# https://httpd.apache.org/docs/current/mod/mod_deflate.html#precompressed
#
# (1)
# Removing default MIME Type for .gz files allowing to add custom
# sub-types.
# You may prefer using less generic extensions such as .html_gz in
# order to keep default behavior regarding .gz files.
# https://httpd.apache.org/docs/current/mod/mod_mime.html#removetype

<IfModule mod_headers.c>

RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -f
RewriteRule \.(css|ics|js|json|html|svg)$ %{REQUEST_URI}.gz [L]

# Prevent mod_deflate double gzip
RewriteRule \.gz$ - [E=no-gzip:1]

<FilesMatch "\.gz$">

# Serve correct content types
<IfModule mod_mime.c>
# (1)
RemoveType gz

# Serve correct content types
AddType text/css css.gz
AddType text/calendar ics.gz
AddType text/javascript js.gz
AddType application/json json.gz
AddType text/html html.gz
AddType image/svg+xml svg.gz

# Serve correct content charset
AddCharset utf-8 .css.gz \
.ics.gz \
.js.gz \
.json.gz
</IfModule>

# Force proxies to cache gzipped and non-gzipped files separately
Header append Vary Accept-Encoding

</FilesMatch>

# Serve correct encoding type
AddEncoding gzip .gz

</IfModule>
1 change: 1 addition & 0 deletions test/fixtures/test-pre-brotli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
raw-content
1 change: 1 addition & 0 deletions test/fixtures/test-pre-brotli.js.br
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
brotli-content
1 change: 1 addition & 0 deletions test/fixtures/test-pre-gzip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
raw-content
1 change: 1 addition & 0 deletions test/fixtures/test-pre-gzip.js.gz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gzip-content
2 changes: 2 additions & 0 deletions test/htaccess_fixture.conf
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ enable "src/security/server_software_information.conf"

title "web performance"
enable "src/web_performance/compression.conf"
enable "src/web_performance/pre-compressed_content_brotli.conf"
enable "src/web_performance/pre-compressed_content_gzip.conf"
enable "src/web_performance/content_transformation.conf"
enable "src/web_performance/etags.conf"
enable "src/web_performance/expires_headers.conf"
Expand Down
30 changes: 30 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,36 @@ exports = module.exports = {

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

{
description: 'Test if files are served compressed using precompressed files',
files: {

'test-pre-gzip.js': {
responseHeaders: {
'cache-control': 'max-age=31536000, no-transform',
'content-encoding': 'gzip',
'content-type': 'text/javascript; charset=utf-8'
},
responseBody: 'gzip-content\n'
},

'test-pre-brotli.js': {
requestHeaders: {
'accept-encoding': 'br'
},
responseHeaders: {
'cache-control': 'max-age=31536000, no-transform',
'content-encoding': 'br',
'content-type': 'text/javascript; charset=utf-8'
},
responseBody: 'brotli-content\n'
}

}
},

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

{
description: 'Test if filename-based cache busting works',
files: {
Expand Down

0 comments on commit 52639ab

Please sign in to comment.