Skip to content

Instantly share code, notes, and snippets.

@k4zuki02h4t4
Forked from tomfuertes/http2-server-push.rb
Created September 8, 2016 05:11
Show Gist options
  • Save k4zuki02h4t4/193b1627f15085e9e7a0333185909f8d to your computer and use it in GitHub Desktop.
Save k4zuki02h4t4/193b1627f15085e9e7a0333185909f8d to your computer and use it in GitHub Desktop.
Rails HTTP2 Server Push via Header Link (on Cloudflare)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment