sprockets-rails
sprockets-rails copied to clipboard
asset_path as javascript helper to resolve path in client
In Single Page Application, asset_path resolving is hardest problem with sprockets. I think it can be resolved by providing asset_path helper.
For example
//=require sprockets_asset_path
// in develpment
Sprockets.asset_path('foo.png') //=> foo.png
// in production
Sprockets.asset_path('foo.png') //=> foo-somethingdigesthash.png
I know js can resolve path by embeded manifest.json in production if client side knows it. assets generation timing is difficult without sprockets.
// pseudocode
Sprockets.asset_path = function(path) {
return reversed_manifest_indexes[path]; //=> path with md5 digest in production
}
By this function, client side template can resolve path as helper without .erb. Today rich javascript application uses gulp and so on to build apps and its environement has no erb helper.
ref. joker1007/gulp-rev-rails-manifest
I feel it's sprockets' role but if not so close please. (If I have enough time, I will implement it later by myself)
Thank you for the issue. I agree we need a way to make easy to resolve paths in JavaScript like we have for SASS, but I don't know if it is good to do it with JavaScript code.
As I'm not seeing any other way to do it, I think we can go with something like this first.