Reputation: 26212
I'm currently serving all images in my app from CDN. Images are located at app/assets/images
folder in my rails app.
And after the application is precompiled, all the assets are uploaded to CDN, and it works great.
However there are dozen of large image files which are almost never changing, mostly served to users while they're not logged in, as a part of carousel or something else.
So in case that a new application version would come out, those static images would be re-uploaded again, and users would have to re-download them but they never change, so it's kind of redundant, and for large size it's not really meaningless.
My idea was to put the images in the vendor/assets/images
, and directly load them in my app, without the suffix/hash generated by the app. Ex: if you have image in your app named 2.jpg
, after the assets have been precompiled it might have been saved to CDN as 2-49d87927b61510021a3cd0a895502625.jpg
or something along that.
So I would be now adding this above image as :
= image_tag('2.jpg', alt: '')
But since I have this line in my assets.rb
initializer :
Rails.application.config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
The image 2.jpg
will be precompiled with hash on production environment.
So how can I make use of this image that is not precompiled with hash on prod, but rather remained the same?
If I put :
= image_tag('2.jpg', alt: '')
It should render as <image src='http://CDN_URL/assets/2.jpg' />
and not as <image src='CND_URL/assets/2-23123dasdasdsadsasad21312.jpg' />
because this one is under vendor/assets/images
I still want my regular images app/assets/images
to be served with hash. Is this possible? and how?
Update :
In my development environment I don't server static assets. Just a regular development config. But I would prefer solution that works for both(DEV and PROD) environments.
Update II:
Looks like %img{src: 'assets/2.jpg'}
loads the file from the vendor/assets/images
which is fine.
Still the other issue persist, when I precompile the assets and when I have Rails.application.config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
in the assets.rb
initializer the image with hash i.e 2-32131dasdaeasee12eda.jpg
If I remove the Rails.application.config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
from the config, no image 2.jpg
is copied to public/assets
, and therefore not copied to CDN.
Upvotes: 1
Views: 402
Reputation: 239302
If you want to static serve files without the finger printing or any of the other benefits of the asset pipeline, then don't use the asset pipeline.
Put your files in /public
.
Upvotes: 1
Reputation: 35531
The premise for your problem is not correct:
So in case that a new application version would come out, those static images would be re-uploaded again, and users would have to re-download them but they never change, so it's kind of redundant, and for large size it's not really meaningless.
The hashed fingerprint is the MD5 of the file content. If the file hasn't changed, then neither will the fingerprint. There's no reason you need to make an exception for large files - the system should work exactly as you want without customizing the process.
Upvotes: 1