lib/roda/plugins/assets.rb in roda-3.5.0 vs lib/roda/plugins/assets.rb in roda-3.6.0
- old
+ new
@@ -265,10 +265,11 @@
# :css_opts :: Template options to pass to the render plugin (via :template_opts) when rendering css assets
# :css_route :: Route under :prefix for css assets (default: :css_dir)
# :dependencies :: A hash of dependencies for your asset files. Keys should be paths to asset files,
# values should be arrays of paths your asset files depends on. This is used to
# detect changes in your asset files.
+ # :early_hints :: Automatically send early hints for all assets. Requires the early_hints plugin.
# :group_subdirs :: Whether a hash used in :css and :js options requires the assets for the
# related group are contained in a subdirectory with the same name (default: true)
# :gzip :: Store gzipped compiled assets files, and serve those to clients who accept gzip encoding.
# :headers :: A hash of additional headers for both js and css rendered files
# :js_compressor :: Compressor to use for compressing javascript, either :yui, :closure, :uglifier, :minjs,
@@ -305,10 +306,11 @@
:css_dir => 'css'.freeze,
:prefix => 'assets'.freeze,
:concat_only => false,
:compiled => false,
:add_suffix => false,
+ :early_hints => false,
:timestamp_paths => false,
:group_subdirs => true,
:compiled_css_dir => nil,
:compiled_js_dir => nil,
:sri => :sha256
@@ -367,10 +369,14 @@
if opts[:precompiled] && !opts[:compiled] && ::File.exist?(opts[:precompiled])
require 'json'
opts[:compiled] = ::JSON.parse(::File.read(opts[:precompiled]))
end
+ if opts[:early_hints]
+ app.plugin :early_hints
+ end
+
DEFAULTS.each do |k, v|
opts[k] = v unless opts.has_key?(k)
end
[
@@ -659,10 +665,15 @@
else
tag_start = "<link rel=\"stylesheet\" #{attrs} href=\""
tag_end = "\" />"
end
- assets_paths(type).map{|p| "#{tag_start}#{h(p)}#{tag_end}"}.join("\n")
+ 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"))
+ end
+ paths.map{|p| "#{tag_start}#{h(p)}#{tag_end}"}.join("\n")
end
# Render the asset with the given filename. When assets are compiled,
# or when the file is already of the given type (no rendering necessary),
# this returns the contents of the compiled file.