Skip to content

Commit

Permalink
Make assets plugin :early_hints option follow Rack 3 SPEC if using Ra…
Browse files Browse the repository at this point in the history
…ck 3

Previously, this used mixed case response headers and separated
multiple headers with a newline in a string instead of using an
array.
  • Loading branch information
jeremyevans committed Jun 4, 2024
1 parent fd90c1d commit e0c9297
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
= master

* Make assets plugin :early_hints option follow Rack 3 SPEC if using Rack 3 (jeremyevans)

* Correctly parse Ruby 3.4 backtraces in exception_page plugin (jeremyevans)

* Support :until and :seconds option in hmac_paths plugin, for paths valid only until a specific time (jeremyevans)
Expand Down
4 changes: 3 additions & 1 deletion lib/roda/plugins/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,9 @@ def assets(type, attrs = OPTS)
paths = assets_paths(type)
if o[:early_hints]
early_hint_as = ltype == :js ? 'script' : 'style'
send_early_hints('Link'=>paths.map{|p| "<#{p}>; rel=preload; as=#{early_hint_as}"}.join("\n"))
early_hints = paths.map{|p| "<#{p}>; rel=preload; as=#{early_hint_as}"}
early_hints = early_hints.join("\n") if Rack.release < '3'
send_early_hints(RodaResponseHeaders::LINK=>early_hints)
end
paths.map{|p| "#{tag_start}#{h(p)}#{tag_end}"}.join("\n")
end
Expand Down
10 changes: 8 additions & 2 deletions spec/plugin/assets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,15 @@ def gunzip(body)
app.plugin :assets, :early_hints=>true
eh = []
html = body('/test', 'rack.early_hints'=>proc{|h| eh << h})
css_eh = ["</assets/css/app.scss>; rel=preload; as=style", "</assets/css/raw.css>; rel=preload; as=style"]
js_eh = ["</assets/js/head/app.js>; rel=preload; as=script"]
if Rack.release < '3'
css_eh = css_eh.join("\n")
js_eh = js_eh.join("\n")
end
eh.must_equal [
{"Link"=>"</assets/css/app.scss>; rel=preload; as=style\n</assets/css/raw.css>; rel=preload; as=style"},
{"Link"=>"</assets/js/head/app.js>; rel=preload; as=script"}
{Roda::RodaResponseHeaders::LINK=>css_eh},
{Roda::RodaResponseHeaders::LINK=>js_eh}
]
html.scan(/<link/).length.must_equal 2
html =~ %r{href="(/assets/css/app\.scss)"}
Expand Down

0 comments on commit e0c9297

Please sign in to comment.