/jekyll-brotli

Generate brotli compressed assets and files for your Jekyll site at build time

Primary LanguageRubyMIT LicenseMIT

Jekyll::Brotli

Generate compressed assets and files for your Jekyll site at build time using Brotli compression.

Gem Version Build status Maintainability Inline docs

API docs | GitHub repo

Why?

Performance in web applications is important. You know that, which is why you have created a static site using Jekyll. But you want a bit more performance. You're serving your assets and files gzipped, but could it be better?

Google's Brotli compressor is a new format with better compression than Zlib's gzip or Zopfli and is supported in all major modern browsers. Compressing files at build time means you can use the highest compression levels with no penalty for dynamically generating the files, so if you're building your site with Jekyll, why not generate the files at the same time?

Jekyll::Brotli does just that. Add the gem to your Jekyll application and when you build your site it will generate brotli files for all text based files (HTML, CSS, JavaScript, etc).

Warning

Brotli is not supported everywhere, so you may want to also gzip your files too. Check out Jekyll::Gzip and Jekyll::Zopfli for options there.

Installation

Add this line to your application's Gemfile:

gem 'jekyll-brotli'

And then execute:

bundle install

Usage

Once you have the gem installed, run JEKYLL_ENV=production bundle exec jekyll build. In your destination directory (_site by default) you will find brotli files.

Jekyll::Brotli only runs when the environment variable JEKYLL_ENV is set to production as dealing with compressing files is unnecessary in development mode and just slows down the site build.

Configuration

Extensions

By default, Jekyll::Brotli will compress all files with the following extensions:

  • '.html'
  • '.css'
  • '.js'
  • '.json'
  • '.txt'
  • '.ttf'
  • '.atom'
  • '.stl'
  • '.xml'
  • '.svg'
  • '.eot'

You can supply your own extensions by adding a brotli key to your site's _config.yml listing the extensions that you want to compress. For example to only compress HTML, CSS and JavaScript files, add the following to _config.yml:

brotli:
  extensions:
    - '.html'
    - '.css'
    - '.js

Compression quality

By default, Jekyll::Brotli uses the maximum compression quality supported by Brotli, which is 11. You can supply your own compression quality by setting a number between 0 and 11 on _config.yml.

brotli:
  quality: 9

Serving pre-compiled gzip files

You will likely need to adjust your web server config to serve these precomputed gzip files. See below for common server configurations:

nginx

For nginx, you need to compile nginx with Google's ngx_brotli module. Follow the steps in this blog post for more detail.

Other web servers

TODO: instructions for other web servers like Apache, HAProxy, etc.

Do you know how to do this for a different server? Please open a pull request or an issue with the details!

Development

After checking out the repo, run bundle install to install dependencies. Then, run bundle exec rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/philnash/jekyll-brotli. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Jekyll::Brotli project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.