Class: StyleCapsule::HeadInjectionMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/style_capsule/head_injection_middleware.rb,
sig/style_capsule.rbs

Overview

Injects stylesheets registered during body rendering into <head> on the same request.

Layouts call stylesheet_registry_tags before the body, so register_stylesheet in components runs too late for that call. This middleware appends pending link/style tags immediately before </head> once the full HTML response is available.

Constant Summary collapse

HTML_CONTENT_TYPE =
%r{\Atext/html}i
SUCCESS_STATUS =
(200..299)

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ HeadInjectionMiddleware

Returns a new instance of HeadInjectionMiddleware.

Parameters:

  • app (Object)


13
14
15
# File 'lib/style_capsule/head_injection_middleware.rb', line 13

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ [Integer, Hash[String, String], untyped]

Parameters:

  • env (Hash[Symbol, untyped])

Returns:

  • ([Integer, Hash[String, String], untyped])


17
18
19
20
21
22
23
# File 'lib/style_capsule/head_injection_middleware.rb', line 17

def call(env)
  status, headers, response = @app.call(env)
  return [status, headers, response] unless inject_response?(status, headers, response)
  return [status, headers, response] unless StylesheetRegistry.pending_head_stylesheets?

  maybe_inject_head_styles(status, headers, response, env)
end