Last active
February 2, 2019 19:32
-
-
Save tomfuertes/6978b594d34038d763a9ee3c4d4c9b14 to your computer and use it in GitHub Desktop.
Rails HTTP2 Server Push via Header Link (on Cloudflare)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
after_action :server_push_headers # https://twitter.com/connorjshea/status/728472354385317888 | |
def server_push_headers | |
# CLoudflare HTTP2 Server Push: https://blog.cloudflare.com/announcing-support-for-http-2-server-push-2/ | |
if request.format.html? # only on html pages | |
layout = self.send(:_layout) # with the right layout | |
if layout == "main" || layout == "speaker" || layout == "admin" | |
assets = [ | |
"<#{view_context.asset_path('application.css')}>; rel=preload; as=style", | |
"<#{view_context.asset_path('application.js')}>; rel=preload; as=script", | |
"<#{view_context.asset_path("twitter/bootstrap/glyphicons-halflings.png")}>; rel=preload; as=image", | |
"<#{view_context.asset_path("site/#{site.code}/logo.png")}>; rel=preload; as=image", | |
] | |
assets.push([ | |
"<#{view_context.asset_path("ppc/check.png")}>; rel=preload; as=image", | |
"<#{view_context.asset_path("icons/ratings/star-off.png")}>; rel=preload; as=image", | |
]) if params[:controller] == 'speakers' && params[:action] == 'show' | |
response.headers['Link'] = assets | |
end | |
end | |
end | |
end |
Looks interesting! Did you notice any appreciable difference in performance?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some maybe relevant / helpful docs: http://stackoverflow.com/questions/6519141/rails-3-how-to-get-image-path-in-controller