-
-
Save k4zuki02h4t4/193b1627f15085e9e7a0333185909f8d 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment