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
780 def assets(type, attrs = OPTS)
781   ltype = type.is_a?(Array) ? type[0] : type
782 
783   o = self.class.assets_config
784   if o.compiled && (algo = o.sri) && (hash = _compiled_assets_hash(type))
785     attrs = Hash[attrs]
786     attrs[:integrity] = "#{algo}-#{h([[hash].pack('H*')].pack('m').tr("\n", ''))}"
787   end
788 
789   attributes = attrs.map{|k,v| "#{k}=\"#{h(v)}\""}.join(' ')
790 
791   if ltype == :js
792     tag_start = "<script#{' type="text/javascript"' unless attrs[:type]} #{attributes} src=\""
793     tag_end = "\"></script>"
794   else
795     tag_start = "<link rel=\"stylesheet\" #{attributes} href=\""
796     tag_end = "\" />"
797   end
798 
799   paths = assets_paths(type)
800   if o.early_hints
801     early_hint_as = ltype == :js ? 'script' : 'style'
802     early_hints = paths.map{|p| "<#{p}>; rel=preload; as=#{early_hint_as}"}
803     early_hints = early_hints.join("\n") if Rack.release < '3'
804     send_early_hints(RodaResponseHeaders::LINK=>early_hints)
805   end
806   paths.map!{|p| "#{tag_start}#{h(p)}#{tag_end}"}.join("\n")
807 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
718 def assets_paths(type)
719   o = self.class.assets_config
720   if type.is_a?(Array)
721     ltype, *dirs = type
722   else
723     ltype = type
724   end
725   stype = ltype.to_s
726 
727   url_prefix = request.script_name if self.class.add_script_name?
728   relative_paths = o.relative_paths
729 
730   paths = if o.compiled
731     relative_paths = false if o.compiled_asset_host
732     if ukey = _compiled_assets_hash(type, true)
733       ["#{o.compiled_asset_host}#{url_prefix}/#{o[:"compiled_#{stype}_prefix"]}.#{ukey}.#{stype}"]
734     else
735       []
736     end
737   else
738     case ltype
739     when :css, :js
740       asset_dir = o[ltype]
741     else
742       RodaPlugins.warn("Invalid asset type given to assets_paths, such be :css or :js: #{ltype}", uplevel: 1)
743     end
744 
745     if dirs && !dirs.empty?
746       dirs.each{|f| asset_dir = asset_dir[f]}
747       prefix = "#{dirs.join('/')}/" if o.group_subdirs
748     end
749     Array(asset_dir).map do |f|
750       if ts = o.timestamp_paths
751         mtime = asset_last_modified(File.join(o[:"#{stype}_path"], *[prefix, f].compact))
752         mtime = "#{sprintf("%i%06i", mtime.to_i, mtime.usec)}#{ts}"
753       end
754       "#{url_prefix}/#{o[:"#{stype}_prefix"]}#{mtime}#{prefix}#{f}#{o[:"#{stype}_suffix"]}"
755     end
756   end
757 
758   if relative_paths
759     paths.map! do |path|
760       "#{relative_prefix}#{path}"
761     end
762   end
763 
764   paths
765 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
838 def read_asset_file(file, type)
839   o = self.class.assets_config
840 
841   content = if file.end_with?(".#{type}")
842     ::File.read(file)
843   else
844     render_asset_file(file, :template_opts=>o[:"#{type}_opts"], :dependencies=>o.expanded_dependencies[file])
845   end
846 
847   o.postprocessor ? o.postprocessor.call(file, type, content) : content
848 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
816 def render_asset(file, type)
817   o = self.class.assets_config
818   if o.compiled
819     file = "#{o[:"compiled_#{type}_path"]}#{file}"
820 
821     if o.gzip && env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/
822       @_response[RodaResponseHeaders::CONTENT_ENCODING] = 'gzip'
823       file += '.gz'
824     end
825 
826     check_asset_request(file, type, ::File.stat(file).mtime)
827     ::File.read(file)
828   else
829     file = "#{o[:"#{type}_path"]}#{file}"
830     check_asset_request(file, type, asset_last_modified(file))
831     read_asset_file(file, type)
832   end
833 end