Module: Roda::RodaPlugins::Assets::InstanceMethods

Defined in:
lib/roda/plugins/assets.rb

Instance Method Summary collapse

Instance Method Details

#assets(type, attrs = OPTS) ⇒ Object

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.



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'lib/roda/plugins/assets.rb', line 717

def assets(type, attrs = OPTS)
  ltype = type.is_a?(Array) ? type[0] : type

  o = self.class.assets_opts
  if o[:compiled] && (algo = o[:sri]) && (hash = _compiled_assets_hash(type))
    attrs = Hash[attrs]
    attrs[:integrity] = "#{algo}-#{h([[hash].pack('H*')].pack('m').tr("\n", ''))}"
  end

  attributes = attrs.map{|k,v| "#{k}=\"#{h(v)}\""}.join(' ')

  if ltype == :js
    tag_start = "<script#{' type="text/javascript"' unless attrs[:type]} #{attributes} src=\""
    tag_end = "\"></script>"
  else
    tag_start = "<link rel=\"stylesheet\" #{attributes} href=\""
    tag_end = "\" />"
  end

  paths = assets_paths(type)
  if o[:early_hints]
    early_hint_as = ltype == :js ? 'script' : 'style'
    early_hints = paths.map{|p| "<#{p}>; rel=preload; as=#{early_hint_as}"}
    early_hints = early_hints.join("\n") if Rack.release < '3'
    send_early_hints(RodaResponseHeaders::LINK=>early_hints)
  end
  paths.map{|p| "#{tag_start}#{h(p)}#{tag_end}"}.join("\n")
end

#assets_paths(type) ⇒ Object

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



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/roda/plugins/assets.rb', line 661

def assets_paths(type)
  o = self.class.assets_opts
  if type.is_a?(Array)
    ltype, *dirs = type
  else
    ltype = type
  end
  stype = ltype.to_s

  url_prefix = request.script_name if self.class.opts[:add_script_name]
  relative_paths = o[:relative_paths]

  paths = if o[:compiled]
    relative_paths = false if o[:compiled_asset_host]
    if ukey = _compiled_assets_hash(type, true)
      ["#{o[:compiled_asset_host]}#{url_prefix}/#{o[:"compiled_#{stype}_prefix"]}.#{ukey}.#{stype}"]
    else
      []
    end
  else
    asset_dir = o[ltype]
    if dirs && !dirs.empty?
      dirs.each{|f| asset_dir = asset_dir[f]}
      prefix = "#{dirs.join('/')}/" if o[:group_subdirs]
    end
    Array(asset_dir).map do |f|
      if ts = o[:timestamp_paths]
        mtime = asset_last_modified(File.join(o[:"#{stype}_path"], *[prefix, f].compact))
        mtime = "#{sprintf("%i%06i", mtime.to_i, mtime.usec)}#{ts}"
      end
      "#{url_prefix}/#{o[:"#{stype}_prefix"]}#{mtime}#{prefix}#{f}#{o[:"#{stype}_suffix"]}"
    end
  end

  if relative_paths
    paths.map! do |path|
      "#{relative_prefix}#{path}"
    end
  end

  paths
end

#read_asset_file(file, type) ⇒ Object

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.



775
776
777
778
779
780
781
782
783
784
785
# File 'lib/roda/plugins/assets.rb', line 775

def read_asset_file(file, type)
  o = self.class.assets_opts

  content = if file.end_with?(".#{type}")
    ::File.read(file)
  else
    render_asset_file(file, :template_opts=>o[:"#{type}_opts"], :dependencies=>o[:expanded_dependencies][file])
  end

  o[:postprocessor] ? o[:postprocessor].call(file, type, content) : content
end

#render_asset(file, type) ⇒ Object

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.



753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/roda/plugins/assets.rb', line 753

def render_asset(file, type)
  o = self.class.assets_opts
  if o[:compiled]
    file = "#{o[:"compiled_#{type}_path"]}#{file}"

    if o[:gzip] && env['HTTP_ACCEPT_ENCODING'] =~ /\bgzip\b/
      @_response[RodaResponseHeaders::CONTENT_ENCODING] = 'gzip'
      file += '.gz'
    end

    check_asset_request(file, type, ::File.stat(file).mtime)
    ::File.read(file)
  else
    file = "#{o[:"#{type}_path"]}#{file}"
    check_asset_request(file, type, asset_last_modified(file))
    read_asset_file(file, type)
  end
end