Skip to content

Commit

Permalink
feat(paths): allow assets to be found using their non digested path (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerborene authored Aug 28, 2022
1 parent 7df6247 commit 3903815
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 27 deletions.
20 changes: 8 additions & 12 deletions lib/propshaft/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
require "action_dispatch/http/mime_type"

class Propshaft::Asset
PREDIGESTED_REGEX = /-([0-9a-zA-Z]{7,128}\.digested)/

attr_reader :path, :logical_path, :version

def initialize(path, logical_path:, version: nil)
@path, @logical_path, @version = path, Pathname.new(logical_path), version
@path = path
@digest = logical_path.to_s[PREDIGESTED_REGEX, 1]
@logical_path = Pathname.new(@digest ? logical_path.sub("-#{@digest}", "") : logical_path)
@version = version
end

def content
Expand All @@ -25,23 +30,14 @@ def digest
end

def digested_path
if already_digested?
logical_path
else
logical_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
end
logical_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
end

def fresh?(digest)
self.digest == digest || already_digested?
self.digest == digest
end

def ==(other_asset)
logical_path.hash == other_asset.logical_path.hash
end

private
def already_digested?
logical_path.to_s =~ /-([0-9a-zA-Z]{7,128})\.digested/
end
end
3 changes: 2 additions & 1 deletion lib/propshaft/load_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def assets_by_path
paths.each do |path|
without_dotfiles(all_files_from_tree(path)).each do |file|
logical_path = file.relative_path_from(path)
mapped[logical_path.to_s] ||= Propshaft::Asset.new(file, logical_path: logical_path, version: version)
asset = Propshaft::Asset.new(file, logical_path: logical_path, version: version)
mapped[asset.logical_path.to_s] ||= asset
end if path.exist?
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/propshaft/output_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def fresh_version_within_limit(mtime, count, expires_at:, limit:)
modified_at = [ 0, Time.now - mtime ].max
modified_at < expires_at || limit < count
end

def remove(path)
FileUtils.rm(@path.join(path))
Propshaft.logger.info "Removed #{path}"
Expand Down
6 changes: 3 additions & 3 deletions lib/propshaft/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def call(env)
if (asset = @assembly.load_path.find(path)) && asset.fresh?(digest)
compiled_content = @assembly.compilers.compile(asset)

[
200,
[
200,
{
"Content-Length" => compiled_content.length.to_s,
"Content-Type" => asset.content_type.to_s,
Expand All @@ -34,7 +34,7 @@ def inspect
private
def extract_path_and_digest(env)
full_path = Rack::Utils.unescape(env["PATH_INFO"].to_s.sub(/^\//, ""))
digest = full_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)[^.]+\z/, 1]
digest = full_path[/-([0-9a-zA-Z]{7,128}(?:\.digested)?)\.[^.]+\z/, 1]
path = digest ? full_path.sub("-#{digest}", "") : full_path

[ path, digest ]
Expand Down

This file was deleted.

6 changes: 2 additions & 4 deletions test/propshaft/asset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Propshaft::AssetTest < ActiveSupport::TestCase
assert find_asset("one.txt").fresh?("f2e1ec14d6856e1958083094170ca6119c529a73")
assert_not find_asset("one.txt").fresh?("e206c34fe404c8e2f25d60dd8303f61c02b8d381")

assert find_asset("file-already-abcdefVWXYZ0123456789.digested.css").fresh?(nil)
assert find_asset("file-already-abcdefVWXYZ0123456789.digested.css").fresh?("abcdefVWXYZ0123456789.digested")
assert_not find_asset("file-already-abcdefVWXYZ0123456789.digested.css").fresh?(nil)
end

test "digested path" do
Expand All @@ -35,9 +36,6 @@ class Propshaft::AssetTest < ActiveSupport::TestCase
assert_equal "file-already-abcdefVWXYZ0123456789.digested.css",
find_asset("file-already-abcdefVWXYZ0123456789.digested.css").digested_path.to_s

assert_equal "file-already-abcdefVWXYZ0123456789.digested.debug.css",
find_asset("file-already-abcdefVWXYZ0123456789.digested.debug.css").digested_path.to_s

assert_equal "file-not.digested-e206c34fe404c8e2f25d60dd8303f61c02b8d381.css",
find_asset("file-not.digested.css").digested_path.to_s
end
Expand Down
1 change: 1 addition & 0 deletions test/propshaft/load_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Propshaft::LoadPathTest < ActiveSupport::TestCase
test "manifest" do
@load_path.manifest.tap do |manifest|
assert_equal "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt", manifest["one.txt"]
assert_equal "file-already-abcdefVWXYZ0123456789.digested.css", manifest["file-already.css"]
assert_equal "nested/three-6c2b86a0206381310375abdd9980863c2ea7b2c3.txt", manifest["nested/three.txt"]
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/propshaft/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Propshaft::ServerTest < ActiveSupport::TestCase
end

test "serve a predigested file" do
asset = @assembly.load_path.find("file-already-abcdefVWXYZ0123456789.digested.css")
asset = @assembly.load_path.find("file-already.css")
get "/#{asset.digested_path}"
assert_equal 200, last_response.status
end
Expand Down

0 comments on commit 3903815

Please sign in to comment.