shakacode / shakapacker

Use Webpack to manage app-like JavaScript modules in Rails
MIT License
430 stars 95 forks source link

Asset_pack_path returns URL #487

Closed afdev82 closed 4 months ago

afdev82 commented 6 months ago

I am using the asset_pack_path from the helper to get the path to my packed bundle, but in production I am getting the full URL instead.

In development and in test environment it works. Has it something to do with the asset host setting in Rails? I have expected that the "_path" method returns the path and the "_url" method returns the URL, as written also in the comment here

Setup environment:

afdev82 commented 6 months ago

According this documentation, it seems that when the asset_host config option is set, the path_to_asset (alias method for asset_path) returns the URL instead of the path of the local file system. That's in my opinion a bit confusing, anyway does someone know how to get the path of the file in the public folder also in production environment? Otherwise an alternative could be to manually compose the path if I would have access to Shakapacker config, using the public_root_path and the public_output_path config options.

tomdracz commented 6 months ago

As you've found yourself, this is just riding on Rails behaviour, which I agree can be confusing. I would be reluctant to make any changes in Shakapacker itself to override this. Assumption here is that if you are using asset_host/CDN then you probably want to reference the paths in relation to that in most usual cases.

Reading through the code, I think you can try and ass few options here, by passing additional details to the request.

Doing something like asset_pack_path('my_asset.js', protocol: :relative, host: '') - should give you a relative path to the asset matching up what it would be without config.asset_host being set. That's the best

afdev82 commented 6 months ago

Thank you,

I will try your hint and let you know.

tomdracz commented 5 months ago

@afdev82 Can this be closed now?

afdev82 commented 5 months ago

Hi @tomdracz,

I am in the middle of a big website update and I wanted to return back after that. Anyway now I have applied only your hint in production and it doesn't work. I have tried all variants:

asset_pack_path('configurator_ssr.js')
asset_pack_path('configurator_ssr.js', protocol: :relative)
asset_pack_path('configurator_ssr.js', protocol: :relative, host: '')

They return always an URL with the asset host.

justin808 commented 4 months ago

Closing for now. Reopen if needed.