lib/roda/plugins/assets.rb in roda-3.29.0 vs lib/roda/plugins/assets.rb in roda-3.30.0
- old
+ new
@@ -289,10 +289,12 @@
# :precompiled :: Path to the compiled asset metadata file. If the file exists, will use compiled
# mode using the metadata in the file. If the file does not exist, will use
# non-compiled mode, but will write the metadata to the file if compile_assets is called.
# :public :: Path to your public folder, in which compiled files are placed (default: 'public'). Relative
# paths will be considered relative to the application's :root option.
+ # :relative_paths :: Use relative paths instead of absolute paths when setting up link and script tags for
+ # assets.
# :sri :: Enables subresource integrity when setting up references to compiled assets. The value should be
# :sha256, :sha384, or :sha512 depending on which hash algorithm you want to use. This changes the
# hash algorithm that Roda will use when naming compiled asset files. The default is :sha256, you
# can use nil to disable subresource integrity.
# :timestamp_paths :: Include the timestamp of assets in asset paths in non-compiled mode. Doing this can
@@ -619,12 +621,14 @@
ltype = type
end
stype = ltype.to_s
url_prefix = request.script_name if self.class.opts[:add_script_name]
+ relative_paths = o[:relative_paths]
- if o[:compiled]
+ 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
@@ -640,9 +644,18 @@
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 && (slash_count = request.path.count('/')) >= 1
+ relative_prefix = "../" * (slash_count - 1)
+ paths.map! do |path|
+ "#{relative_prefix}#{path.slice(1,100000000)}"
+ end
+ end
+
+ paths
end
# 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.