Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat(paths): allow assets to be found using their non digested path (#100)" #144

Merged
merged 1 commit into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions lib/propshaft/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
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 = path
@digest = logical_path.to_s[PREDIGESTED_REGEX, 1]
@logical_path = Pathname.new(@digest ? logical_path.sub("-#{@digest}", "") : logical_path)
@version = version
@path, @logical_path, @version = path, Pathname.new(logical_path), version
end

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

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

def fresh?(digest)
self.digest == digest
self.digest == digest || already_digested?
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: 1 addition & 2 deletions lib/propshaft/load_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ 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)
asset = Propshaft::Asset.new(file, logical_path: logical_path, version: version)
mapped[asset.logical_path.to_s] ||= asset
mapped[logical_path.to_s] ||= Propshaft::Asset.new(file, logical_path: logical_path, version: version)
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* this is css */

.btn {
background-image: asset-path("archive.svg");
}
6 changes: 4 additions & 2 deletions test/propshaft/asset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ 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?("abcdefVWXYZ0123456789.digested")
assert_not find_asset("file-already-abcdefVWXYZ0123456789.digested.css").fresh?(nil)
assert find_asset("file-already-abcdefVWXYZ0123456789.digested.css").fresh?(nil)
end

test "digested path" do
Expand All @@ -36,6 +35,9 @@ 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: 0 additions & 1 deletion test/propshaft/load_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ 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.css")
asset = @assembly.load_path.find("file-already-abcdefVWXYZ0123456789.digested.css")
get "/#{asset.digested_path}"
assert_equal 200, last_response.status
end
Expand Down