Unfortunately HTTP/2 adoption is still slow, so the IETF “backported” this workflow to HTTP/1.1 as well, by introducing the HTTP status . In this case the server sends one or more HTTP responses for a single request. The last one must be the traditional 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.

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

  1. 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 section).

As of today, only Puma supports Early Hints.

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:

Opt-out:

  1. <%= stylesheet "application", push: false %>
  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):

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

    Asset URL