Just wanted to put this out there, in case anyone else wants to do the same. I’m building an SPA with parcel and I wanted to set appropriate caching and pre-compressed file support.
My configuration is as follows:
:8080
root * /srv
log {
format json
}
##################################
# *.html pre-compressed files
##################################
@htmlBr {
path *.html
header Accept-Encoding *br*
file {
try_files {path}.br
}
}
handle @htmlBr {
rewrite {path}.br
header Content-Encoding br
header Content-Type text/html
header Cache-Control max-age=300,public
header Vary "Accept-Encoding"
}
@htmlGzip {
path *.html
header Accept-Encoding *gzip*
file {
try_files {path}.br
}
}
handle @htmlGzip {
rewrite {path}.gz
header Content-Encoding gzip
header Content-Type text/html
header Cache-Control max-age=300,public
header Vary "Accept-Encoding"
}
@html {
path *.html
}
handle @html {
header Cache-Control max-age=300,public
header Vary "Accept-Encoding"
}
##################################
##################################
# *.js pre-compressed files
##################################
@jsBr {
header Accept-Encoding *br*
path *.js
file {
try_files {path}.br
}
}
handle @jsBr {
rewrite {path}.br
header Content-Encoding br
header Content-Type application/javascript
header Cache-Control max-age=2628000,public
header Vary "Accept-Encoding"
}
@jsGzip {
header Accept-Encoding *gzip*
path *.js
file {
try_files {path}.br
}
}
handle @jsGzip {
rewrite {path}.gz
header Content-Encoding gzip
header Content-Type application/javascript
header Cache-Control max-age=2628000,public
header Vary "Accept-Encoding"
}
@js {
path *.js
}
handle @js {
header Cache-Control max-age=2628000,public
header Vary "Accept-Encoding"
}
##################################
##################################
# *.map pre-compressed files
##################################
@mapBr {
header Accept-Encoding *br*
path *.map
file {
try_files {path}.br
}
}
handle @mapBr {
rewrite {path}.br
header Content-Encoding br
header Content-Type application/json
header Cache-Control max-age=2628000,public
header Vary "Accept-Encoding"
}
@mapGzip {
header Accept-Encoding *gzip*
path *.map
file {
try_files {path}.br
}
}
handle @mapGzip {
rewrite {path}.gz
header Content-Encoding gzip
header Content-Type application/json
header Cache-Control max-age=2628000,public
header Vary "Accept-Encoding"
}
@map {
path *.map
}
handle @map {
header Content-Type application/json
header Cache-Control max-age=2628000,public
header Vary "Accept-Encoding"
}
##################################
##################################
# *.css pre-compressed files
##################################
@cssBr {
header Accept-Encoding *br*
path *.css
file {
try_files {path}.br
}
}
handle @cssBr {
rewrite {path}.br
header Content-Encoding br
header Content-Type "text/css; charset=utf-8"
header Cache-Control max-age=2628000,public
header Vary "Accept-Encoding"
}
@cssGzip {
header Accept-Encoding *gzip*
path *.css
file {
try_files {path}.br
}
}
handle @cssGzip {
rewrite {path}.gz
header Content-Encoding gzip
header Content-Type "text/css; charset=utf-8"
header Cache-Control max-age=2628000,public
header Vary "Accept-Encoding"
}
@css {
path *.css
}
handle @css {
header Cache-Control max-age=2628000,public
header Vary "Accept-Encoding"
}
##################################
##################################
# Cache Control
# Note: other handlers set values above
# cache favicon for 1 day
@cacheFavicon {
path_regexp favicon\.
}
handle @cacheFavicon {
header Cache-Control max-age=86400,public
}
# 5 minutes caching by default fallback
header Cache-Control max-age=300,public
##################################
##################################
# Support for SPA.
try_files {path} /index.html
file_server
Edit: add Vary header and default handlers for pre-compressed types