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