Rails 3.1 & Sprockets & compiled JS files - ruby-on-rails

So just trying out Rails 3.1-rc1 with the Sprockets asset pipeline:
I run rake assets:precompile
and I get the /public/assets directory and the application.js file the MD5 hash:
application-266b6b0b4fbd28fc01145d90a4158b2f.js
But the problem is this:
When I update my JS and run rake assets:precompile I get more JS files and it doesnt delete the old ones.
I'm note sure how it works - the browser only picks up the first one and I have to manually delete the old ones. Which doesnt seem like how it should work.
Just a side gripe: It seems I have to run rake assets:precompile every time I change something. Which is painful.
(I guess there needs to be some docs on how this all works).
Thanks.

The name of js file is <file name>-<hash>.js.
That's made so that when you deploy new version of application to the production server your visitors would have to load new js file as well. The hash ensures that they will not have mixed up new application and old cached js that may break entire application taking into account dynamic nature of the web this days.
In most deployment scenarios you will have your app in new directory on the server and you will not have old compiled js files there.

Related

rails producing separate css files in production

I'm using Rails version 6.0.3.2, but I started to notice that when precompiling asset:
rails assets:precompile RAILS_ENV=production
I get my public/assets filled with all my css files separated (as they are in development) and minified in addition to application.css
Is this something new or there is something wrong? I have no idea why this is happening!!
As far as I know it will always build them independently, then assemble them into a unified file if so requested. It doesn't trim the individual files after. The individual files are just a byproduct of the compilation process.
You only need the combined asset pack for deployment, the individual files can be ignored.

Rails 5 is serving old assets on heroku

My Rails 5 application is behaving weirdly. The application is on heroku.
I am using the gem tinymce-rails. Recently, this gem got updated to version 5 which uses new functions. After deployment I faced an issue where one of the plugins (the link plugin) could not be loaded due to using the old syntax.
I am using chrome to open the website. For some reason the tinymce plugins javascript files are not up to date. They also don't have that fingerprinted name with the hash in the end. All other javascript files do however.
I ran heroku run rake assets:precompile; heroku run rake assets:clobber;. It was no good.
I then tried to open the website in incognito mode and found that the tinymce plugins are the up to date ones. Still though without the fingerprinted file names.
How can I force that the old plugin javascript files be invalidated? I will not have control over my users to ask them to clear their caches.
How can I force using the fingerprinted files? I have inspected the heroku server file system and found assets/tinymce/plugins/link
having both fingerprinted and non-fingerprinted files however the non fingerprinted files were the ones used.

Rails ckeditor extraplugin problems on production

I'm developing a Rails 4.1 application. I use ckeditor
(https://github.com/galetahub/ckeditor), and I'm using some extraplugin
(image2, which also uses widget), stored in app/assets/ckeditor/plugins
directory. I used it with f.cktext_area :extraPlugins => 'image2'. It
works well in development and also in production environment.
Now I decided to add one more extra plugin (syntaxhighlight), stored and
used in the same way (stored in app/assets/ckeditor/plugins , called
with :extraPlugins => 'image2,syntaxhighlight'). In development
environment it works perfectly, but in production ckeditor cannot load
syntaxhighlight plugin, because it try to load not the hashed version of it:
uncaught exception: [CKEDITOR.resourceManager.load] Resource name
"syntaxhighlight" was not found at
"/assets/ckeditor/plugins/syntaxhighlight/plugin.js"
I run "rake assets:precompile" many times, it generated the hashed
version of plugins
(/assets/ckeditor/plugins/syntaxhighlight/plugin-a9ce9661bf06ef29e7ac40baac50c044.js).
But none of the working other (old) extraplugins do not have simple
plugin.js in the public directory, and they work.
Any idea how to make work this new extra plugin?
I had the same problem. To solve this, run:
rake tmp:cache:clear
and after assets:precompile again.
Just to understand what happened: ckeditor gem generate a mapping file (override.js), and it maps the simple and the hashed version of js files. If you insert a new extraplugin, it will not regenerated with assets:precompile, you have to clear the cache to force it.

Rails can't find asset after page reload

I face a problem with a rails (3.2.6) application on our production server (nginx + passenger 3). After a
rake assets:precompile
one static page works like a charm but after a few minutes I receive an asset not found error for an existing image. If I recompile the assets again it works but a few minutes later rails raise the file-not-found error again.
Here you can see production.rb and Gemfile: https://gist.github.com/3937589
Is there any way that your code is doing any manipulation of the /public folder?
Have you verified that the asset in question is indeed in the /public folder?
Are there cron jobs or other processes at work that maybe interfere with your file system?
What the assets:precompile does is simply work through your app/assets and copies these over to /public/assets.
It does so normally in 3 favors: uncompressed original file, original-file + hash that gets used when referenced from a helper (asset_path) and the gzipped file.
You can simply go to your production server and look into /public/assets to verify if anything is missing. If the file is there and the user still gets errors I'd look at my nginx configuration.

Ignore certain files in the asset pipeline when performing assets:precompile

I have a templates directory in my assets folder on my Rails setup which hosts my templated-HTML files. I've implemented a partial system where I can load text snippets into each template so long as it has a .erb file attached to it. The partials are prefixed with an underscore, so /assets/templates/_form.html is a partial which is included inside of /assets/templates/edit.html. This works, but there is an issue when I run assets:precompile.
The assets:precompile command will precompile the _form.html files as well (since they're located inside of the assets/templates folder) and I don't want this to happen since they can't be precompiled (since any erb variables are missing and so on).
Is there any way I can inform the rake assets:precompile command to ignore certain files (in this case files that have an underscore infront of them)? Does someone know how to do this? Or can someone point me to the github repository which has the source for the rake assets:precompile command?
In Rails 3.2.8 you can find the source for the rake task definition here.
What you're asking has been brought up at least once in Sprockets issues tracker
https://github.com/sstephenson/sprockets/pull/86
joshpeek added a new stub directive in Sprockets 2.2 which looks like a promising solution to 'ignoring' certain assets from the pipeline.
Rails 3.2 doesn't support a version of Sprockets anywhere near 2.2 and there doesn't seem to be any intention on doing so before Rails 4 from what I've read (though I'd love to be wrong about that). Here's at least one closed issue (no action taken) requesting an upgrade to 2.2.
https://github.com/rails/rails/pull/5984
For your situation though, it seems to me that you might be using app/assets incorrectly. I can't offer an alternative solution as I don't know enough about your setup/usage to say definitively, but as you said, files with .erb in app/assets are going to be processed by Sprockets.

Resources