Public Instance methods
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.
# 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 send_early_hints('Link'=>paths.map{|p| "<#{p}>; rel=preload; as=#{early_hint_as}"}.join("\n")) 740 end 741 paths.map{|p| "#{tag_start}#{h(p)}#{tag_end}"}.join("\n") 742 end
Return an array of paths for the given asset type and optionally asset group. See the assets function documentation for details.
# 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
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.
# File lib/roda/plugins/assets.rb 773 def read_asset_file(file, type) 774 o = self.class.assets_opts 775 776 content = if file.end_with?(".#{type}") 777 ::File.read(file) 778 else 779 render_asset_file(file, :template_opts=>o[:"#{type}_opts"], :dependencies=>o[:expanded_dependencies][file]) 780 end 781 782 o[:postprocessor] ? o[:postprocessor].call(file, type, content) : content 783 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. 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.
# File lib/roda/plugins/assets.rb 751 def render_asset(file, type) 752 o = self.class.assets_opts 753 if o[:compiled] 754 file = "#{o[:"compiled_#{type}_path"]}#{file}" 755 756 if o[:gzip] && env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/ 757 @_response[RodaResponseHeaders::CONTENT_ENCODING] = 'gzip' 758 file += '.gz' 759 end 760 761 check_asset_request(file, type, ::File.stat(file).mtime) 762 ::File.read(file) 763 else 764 file = "#{o[:"#{type}_path"]}#{file}" 765 check_asset_request(file, type, asset_last_modified(file)) 766 read_asset_file(file, type) 767 end 768 end