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
761 def assets(type, attrs = OPTS)
762   ltype = type.is_a?(Array) ? type[0] : type
763 
764   o = self.class.assets_opts
765   if o[:compiled] && (algo = o[:sri]) && (hash = _compiled_assets_hash(type))
766     attrs = Hash[attrs]
767     attrs[:integrity] = "#{algo}-#{h([[hash].pack('H*')].pack('m').tr("\n", ''))}"
768   end
769 
770   attributes = attrs.map{|k,v| "#{k}=\"#{h(v)}\""}.join(' ')
771 
772   if ltype == :js
773     tag_start = "<script#{' type="text/javascript"' unless attrs[:type]} #{attributes} src=\""
774     tag_end = "\"></script>"
775   else
776     tag_start = "<link rel=\"stylesheet\" #{attributes} href=\""
777     tag_end = "\" />"
778   end
779 
780   paths = assets_paths(type)
781   if o[:early_hints]
782     early_hint_as = ltype == :js ? 'script' : 'style'
783     early_hints = paths.map{|p| "<#{p}>; rel=preload; as=#{early_hint_as}"}
784     early_hints = early_hints.join("\n") if Rack.release < '3'
785     send_early_hints(RodaResponseHeaders::LINK=>early_hints)
786   end
787   paths.map{|p| "#{tag_start}#{h(p)}#{tag_end}"}.join("\n")
788 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
705 def assets_paths(type)
706   o = self.class.assets_opts
707   if type.is_a?(Array)
708     ltype, *dirs = type
709   else
710     ltype = type
711   end
712   stype = ltype.to_s
713 
714   url_prefix = request.script_name if self.class.opts[:add_script_name]
715   relative_paths = o[:relative_paths]
716 
717   paths = if o[:compiled]
718     relative_paths = false if o[:compiled_asset_host]
719     if ukey = _compiled_assets_hash(type, true)
720       ["#{o[:compiled_asset_host]}#{url_prefix}/#{o[:"compiled_#{stype}_prefix"]}.#{ukey}.#{stype}"]
721     else
722       []
723     end
724   else
725     asset_dir = o[ltype]
726     if dirs && !dirs.empty?
727       dirs.each{|f| asset_dir = asset_dir[f]}
728       prefix = "#{dirs.join('/')}/" if o[:group_subdirs]
729     end
730     Array(asset_dir).map do |f|
731       if ts = o[:timestamp_paths]
732         mtime = asset_last_modified(File.join(o[:"#{stype}_path"], *[prefix, f].compact))
733         mtime = "#{sprintf("%i%06i", mtime.to_i, mtime.usec)}#{ts}"
734       end
735       "#{url_prefix}/#{o[:"#{stype}_prefix"]}#{mtime}#{prefix}#{f}#{o[:"#{stype}_suffix"]}"
736     end
737   end
738 
739   if relative_paths
740     paths.map! do |path|
741       "#{relative_prefix}#{path}"
742     end
743   end
744 
745   paths
746 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
819 def read_asset_file(file, type)
820   o = self.class.assets_opts
821 
822   content = if file.end_with?(".#{type}")
823     ::File.read(file)
824   else
825     render_asset_file(file, :template_opts=>o[:"#{type}_opts"], :dependencies=>o[:expanded_dependencies][file])
826   end
827 
828   o[:postprocessor] ? o[:postprocessor].call(file, type, content) : content
829 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
797 def render_asset(file, type)
798   o = self.class.assets_opts
799   if o[:compiled]
800     file = "#{o[:"compiled_#{type}_path"]}#{file}"
801 
802     if o[:gzip] && env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/
803       @_response[RodaResponseHeaders::CONTENT_ENCODING] = 'gzip'
804       file += '.gz'
805     end
806 
807     check_asset_request(file, type, ::File.stat(file).mtime)
808     ::File.read(file)
809   else
810     file = "#{o[:"#{type}_path"]}#{file}"
811     check_asset_request(file, type, asset_last_modified(file))
812     read_asset_file(file, type)
813   end
814 end