module Roda::RodaPlugins::Assets::InstanceMethods

  1. lib/roda/plugins/assets.rb

Public Instance methods

assets(type, attrs = OPTS)

Return a string containing html tags for the given asset type. This will use a script tag for the :js type and a link tag for the :css type.

To return the tags for a specific asset group, use an array for the type, such as [:css, :frontend].

You can specify custom attributes for the tag by passing a hash as the attrs argument.

When the assets are not compiled, this will result in a separate tag for each asset file. When the assets are compiled, this will result in a single tag to the compiled asset file.

[show source]
    # File lib/roda/plugins/assets.rb
717 def assets(type, attrs = OPTS)
718   ltype = type.is_a?(Array) ? type[0] : type
719 
720   o = self.class.assets_opts
721   if o[:compiled] && (algo = o[:sri]) && (hash = _compiled_assets_hash(type))
722     attrs = Hash[attrs]
723     attrs[:integrity] = "#{algo}-#{h([[hash].pack('H*')].pack('m').tr("\n", ''))}"
724   end
725 
726   attributes = attrs.map{|k,v| "#{k}=\"#{h(v)}\""}.join(' ')
727 
728   if ltype == :js
729     tag_start = "<script#{' type="text/javascript"' unless attrs[:type]} #{attributes} src=\""
730     tag_end = "\"></script>"
731   else
732     tag_start = "<link rel=\"stylesheet\" #{attributes} href=\""
733     tag_end = "\" />"
734   end
735 
736   paths = assets_paths(type)
737   if o[:early_hints]
738     early_hint_as = ltype == :js ? 'script' : 'style'
739     early_hints = paths.map{|p| "<#{p}>; rel=preload; as=#{early_hint_as}"}
740     early_hints = early_hints.join("\n") if Rack.release < '3'
741     send_early_hints(RodaResponseHeaders::LINK=>early_hints)
742   end
743   paths.map{|p| "#{tag_start}#{h(p)}#{tag_end}"}.join("\n")
744 end
assets_paths(type)

Return an array of paths for the given asset type and optionally asset group. See the assets function documentation for details.

[show source]
    # File lib/roda/plugins/assets.rb
661 def assets_paths(type)
662   o = self.class.assets_opts
663   if type.is_a?(Array)
664     ltype, *dirs = type
665   else
666     ltype = type
667   end
668   stype = ltype.to_s
669 
670   url_prefix = request.script_name if self.class.opts[:add_script_name]
671   relative_paths = o[:relative_paths]
672 
673   paths = if o[:compiled]
674     relative_paths = false if o[:compiled_asset_host]
675     if ukey = _compiled_assets_hash(type, true)
676       ["#{o[:compiled_asset_host]}#{url_prefix}/#{o[:"compiled_#{stype}_prefix"]}.#{ukey}.#{stype}"]
677     else
678       []
679     end
680   else
681     asset_dir = o[ltype]
682     if dirs && !dirs.empty?
683       dirs.each{|f| asset_dir = asset_dir[f]}
684       prefix = "#{dirs.join('/')}/" if o[:group_subdirs]
685     end
686     Array(asset_dir).map do |f|
687       if ts = o[:timestamp_paths]
688         mtime = asset_last_modified(File.join(o[:"#{stype}_path"], *[prefix, f].compact))
689         mtime = "#{sprintf("%i%06i", mtime.to_i, mtime.usec)}#{ts}"
690       end
691       "#{url_prefix}/#{o[:"#{stype}_prefix"]}#{mtime}#{prefix}#{f}#{o[:"#{stype}_suffix"]}"
692     end
693   end
694 
695   if relative_paths
696     paths.map! do |path|
697       "#{relative_prefix}#{path}"
698     end
699   end
700 
701   paths
702 end
read_asset_file(file, type)

Return the content of the file if it is already of the correct type. Otherwise, render the file using the render plugin. file should be the relative path to the file from the current directory.

[show source]
    # File lib/roda/plugins/assets.rb
775 def read_asset_file(file, type)
776   o = self.class.assets_opts
777 
778   content = if file.end_with?(".#{type}")
779     ::File.read(file)
780   else
781     render_asset_file(file, :template_opts=>o[:"#{type}_opts"], :dependencies=>o[:expanded_dependencies][file])
782   end
783 
784   o[:postprocessor] ? o[:postprocessor].call(file, type, content) : content
785 end
render_asset(file, type)

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. When assets are not compiled and the file is not already in the same format, this will render the asset using the render plugin. In both cases, if the file has not been modified since the last request, this will return a 304 response.

[show source]
    # File lib/roda/plugins/assets.rb
753 def render_asset(file, type)
754   o = self.class.assets_opts
755   if o[:compiled]
756     file = "#{o[:"compiled_#{type}_path"]}#{file}"
757 
758     if o[:gzip] && env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/
759       @_response[RodaResponseHeaders::CONTENT_ENCODING] = 'gzip'
760       file += '.gz'
761     end
762 
763     check_asset_request(file, type, ::File.stat(file).mtime)
764     ::File.read(file)
765   else
766     file = "#{o[:"#{type}_path"]}#{file}"
767     check_asset_request(file, type, asset_last_modified(file))
768     read_asset_file(file, type)
769   end
770 end