A web page may link to external resources such as stylesheets, or images (assets). With HTTP/1.1 the browser parses the HTML and for each link, it downloads the asset and eventually take an action on it: renders an image or evaluate JavaScript code. With HTTP/2 introduced an enhancement: the server can proactively “push” in parallel both the HTML payload and some assets to the browser. This workflow is allowed due to the HTTP/2 TCP connections are multiplexed. That means many communications can happen at the same time.

Unfortunately HTTP/2 adoption is still slow, so the IETF “backported” this workflow to HTTP/1.1 as well, by introducing the HTTP status 103 Early Hints. In this case the server sends one or more HTTP responses for a single request. The last one must be the traditional 200 OK that returns the HTML of the page, whereas the first n can include a special header Link to tell the browser to fetch the asset ahead of time.

Setup

As first thing, you need Puma 3.11.0+ with Early-Hints enabled:

  1. # Gemfile
  2. gem "puma"
  1. # config/puma.rb
  2. early_hints true

Then from the project configuration, you can simply enable the feature:

  1. # config/environment.rb
  2. Hanami.configure do
  3. # ...
  4. early_hints true
  5. end

As last step, you need a web server that supports HTTP/2 and Early Hints like h2o. When you’ll start the server and visit the page, javascripts and stylesheets will be pushed (see Assets helpers section).

Other web servers

As of today, only Puma supports Early Hints.

Assets helpers

In order to automatically push your assets, you have to use our assets helpers. But given to browser limitations (only up to 100 assets can be pushed), Hanami by default sends stylesheets and javascripts only.

HelperEarly Hints asset typePushed by default
javascript:scriptyes
stylesheet:styleyes
favicon:imageno
image:imageno
video:videono
audio:audiono
asset_pathN/Ano
asset_urlN/Ano

You can opt-in/out the following types:

Javascripts

Pushed by default:

  1. <%= javascript "application" %>
  2. <%= javascript "https://somecdn.test/framework.js", "dashboard" %>

Opt-out:

  1. <%= javascript "application", push: false %>
  2. <%= javascript "https://somecdn.test/framework.css", "dashboard", push: false %>

Stylesheets

Pushed by default:

  1. <%= stylesheet "application" %>
  2. <%= stylesheet "https://somecdn.test/framework.css", "dashboard" %>

Opt-out:

  1. <%= stylesheet "application", push: false %>
  2. <%= stylesheet "https://somecdn.test/framework.css", "dashboard", push: false %>

Favicon

Opt-in:

  1. <%= favicon "favicon.ico", push: :image %>

Image

Opt-in:

  1. <%= image "avatar.png", push: :image %>

Audio

Opt-in:

  1. <%= audio "song.ogg", push: true %>

Block syntax (pushes only song.ogg):

  1. <%=
  2. audio do
  3. text "Your browser does not support the audio tag"
  4. source src: asset_path("song.ogg", push: :audio), type: "audio/ogg"
  5. source src: asset_path("song.wav"), type: "audio/wav"
  6. end
  7. %>

Video

Opt-in:

  1. <%= video "movie.mp4", push: true %>

Block syntax (pushes only movie.mp4):

  1. <%=
  2. video do
  3. text "Your browser does not support the video tag"
  4. source src: asset_path("movie.mp4", push: :video), type: "video/mp4"
  5. source src: asset_path("movie.ogg"), type: "video/ogg"
  6. end
  7. %>

Asset path

  1. <%= asset_path "application.js", push: :script %>

Asset URL

  1. <%= asset_url "https://somecdn.test/framework.js", push: :script %>

Demo project

If you’re looking for a full working example, please check this demo project.