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

Fix: Ensure assets without extensions are handled correctly #1115

Merged
merged 2 commits into from
Aug 11, 2024

Conversation

d4rky-pl
Copy link
Contributor

@d4rky-pl d4rky-pl commented May 4, 2024

This PR fixes the issues reported after #1094 and related to the asset manifest implementation not looking for the assets properly.


As a side note, this will most likely be my last contribution to this project. The fix itself took me 5 minutes to implement but adding those four simple test cases took... 1.5h. I wish I was joking 🥲

If anyone still has energy it would be great to at least figure out how to upgrade Mocha to the newer version as this one has a bug that prevents stubs from clearing properly (though it would be amazing to get rid of it completely and switch to rspec)

@d4rky-pl
Copy link
Contributor Author

d4rky-pl commented May 8, 2024

@doits @joshuay03 would you like to test this PR in your apps and confirm this solved the issue for you?
@unixmonkey @mathieujobin let me know if you need anything else to land this patch once we get a confirmation it works

@joshuay03
Copy link

joshuay03 commented May 9, 2024

@doits @joshuay03 would you like to test this PR in your apps and confirm this solved the issue for you?

@d4rky-pl I can confirm that this fixes the issue we were having. Thank you for working on this, and the original PR which seems like a super useful feature!

@d4rky-pl
Copy link
Contributor Author

@unixmonkey do you need anything else or can we release this as 2.8.1? :)

Copy link
Contributor

@mathieujobin mathieujobin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a cool fix, and I can throw a CI run with my app running with your branch. but if I only have assets with explicit extensions, it won't change a thing, if my code review is correct.

so I wonder, what if we want extensions to be explicit? I don't know.

@d4rky-pl
Copy link
Contributor Author

@mathieujobin I have no idea either but requiring extensions would be an API breaking change and I wanted to avoid that (especially considering the library is more or less on life support at this point).

My solution is based partially on how Sprockets solve that (in find_asset -> resolve -> resolve_logical_path -> resolve_under_paths) except I didn't add prioritizing which asset to pick based on the mime type because we don't know it and can't assume when using wicked_pdf_asset_base64 (as it's decoupled from the stylesheet / javascript concept).

@d4rky-pl
Copy link
Contributor Author

d4rky-pl commented May 12, 2024

One other thing we could do is make it fail when there's more than one asset with the same name before the extension to avoid unexpected situations (like having fonts.css and fonts.js and then wicked_pdf_asset_base64 picking one but not the other) but that's also going to be a breaking change 🤔

@alessandrostein
Copy link

alessandrostein commented May 22, 2024

Any plans to release 2.8.1? Thank you for all hard work here 🙌

Copy link
Contributor

@mathieujobin mathieujobin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my app tests pass with these changes. I don't have write access, otherwise I would merge and release. 👍🏽

assets = Rails.application.assets_manifest.assets

if File.extname(path).empty?
assets.find { |asset, _v| File.basename(asset, File.extname(asset)) == path }&.last
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this does not work for nested directories of assets.

for example company/logo/some_variant.svg
Here is the the fix:

assets.find do |asset, _v| 
  directory = File.dirname(asset)
  filename_without_extension = File.basename(asset, File.extname(asset))

 (directory == "." ? filename_without_extension : File.join(directory, filename_without_extension)) == path
end&.last

@d4rky-pl
Copy link
Contributor Author

Added more tests and a fix, good catch @chaadow! Let me know if you can think of any other cases where this could break so I can add more tests

Copy link
Contributor

@chaadow chaadow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks!

@d4rky-pl
Copy link
Contributor Author

@unixmonkey let me know if you need anything else to have this released. If there are any further bug reports related to this change, either before or after this PR is merged, feel free to @ me as well

@chaadow
Copy link
Contributor

chaadow commented Aug 10, 2024

@unixmonkey Hi! is it possible to release this fix 🙏🏼

Copy link
Collaborator

@unixmonkey unixmonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@unixmonkey unixmonkey merged commit 9081841 into mileszs:master Aug 11, 2024
12 checks passed
@unixmonkey
Copy link
Collaborator

This is now published in version 2.8.1. Please let me know if you experience any issues after upgrading to this.

Thank you all for your help!

@d4rky-pl d4rky-pl deleted the local-assets-fixes branch August 12, 2024 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants