Sha256: 6facac480f70f5aa5f2f4f654a6d8dd51f3a91952ac731a7c3d80533683ca889
Contents?: true
Size: 1.54 KB
Versions: 1
Compression:
Stored size: 1.54 KB
Contents
# frozen_string_literal: true require "action_view/helpers/asset_url_helper" require "active_support/concern" require "action_dispatch" module RailsHttpPreload class Middleware def initialize(app) @app = app end def call(env) response = @app.call(env) response[1]["Link"] = preconnect_header(response) if required?(response, ActionDispatch::Request.new(env)) response end # TODO: Should we just add it to every response anyway and let the browser # figure it out? def required?(response, request) config.asset_host.present? && html?(response) && !already_connected?(request) end # Adding this header to, for example, a JSON response would be pointless # TODO: remove from Turbo frame responses too def html?(response) response[1]["Content-Type"].match?("html") end def current_asset_host ActionController::Base.helpers.compute_asset_host("", host: config.asset_host) end # If the asset host is equal to the request domain, no need to add. def already_connected?(request) request.base_url == current_asset_host end def preconnect_header(response) header = [ current_asset_host, *config.additional_urls ].compact.map { |url| create_link_header(url) }.join(", ") if response[1]["Link"] "#{header}, #{response[1]["Link"]}" else header end end def create_link_header(url) "<#{url}>; rel=preconnect" end def config RailsHttpPreload.config end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rails_http_preload-0.2.0 | lib/rails_http_preload/middleware.rb |