How are the hashed urls of the asset pipeline files generated?

Hi, I’m trying to get the hashed version of an asset from a controller but I get the non hashed version.

The asset is generated fine when accesed from the javascript_include_tag though, so what I get is something like:

<script src="http://localhost:3000/assets/embed2.self-5196b72c08a71ab759f4282a95bd76431ccc336dedb42de662ad48b5a1a48fa1.js?body=1" data-turbo-track="reload" defer="defer"></script>

which is good :+1:.

But when I try to access the same asset from rails console with javascript_url("embed2.js") what I get is “/javascripts/embed2.js”.

I’ve checked the source of action_view for the javascript_include_tag(*sources) but I can’t find where is the path generated, indeed the *sources already has the hashed version of the file, but I can’t get really where is the hash being generated.

I’m using rails 6 with esbuild right now. on my previous implementation I had webpacked and I’ve used this: asset_pack_path("embed.js"), so I need to migrate that to the asset_pipeline version.

I’m intrigued about where the code that generates the hashed url when I use the javascript_include_tag ?

1 Like

Ah, got it. Sprockets::Rails is the one who generates the hashed file, so to get the hashed file, we must do:

ActionController::Base.helpers.compute_asset_path("embed2.self.js", debug: true)

I will see how this behaves in production environments

Thanks!