I have a rails 6 app using esbuild to bundle all my js files to app/assets/build. I have to manually run rails assets:precompile in order for the changes that are in app/assets/build to actually be loaded from sprockets. I'll make a change to a file, I can see that esbuild has bundled the changes in app/assets/build but refreshing the page does not reflect the changes, unless I stop the server, precompile assets and then start server again. This is on my windows laptop running WSL
I just tested the exact same app on my macbook and the changes are picked up by the asset pipeline without having to manually precompile assets.
Any ideas on what the issue with windows/WSL is on this would be greatly appreciated!
The issue was a public/assets folder being present. Removing that made assets get served from assets/builds
Related
I pre-compiled my assets i.e CSS,javascript and fonts files in order to reduce the file size. Using RAILS_ENV=production rake assets:precompile.
I already have compressed assets on the server in the following path:
shared/public/assets
But anyway I went ahead and pre-compiled them again on my local and they got generated inside public/assets folder. I noticed that the compressed files are exactly as same as the ones on server shared/public/assets. But the tester in my team is been testing it on some online tools. And they all say "Your java script files need to be minified." So, would this "minifying" issue be solved if I push these locally pre-compiled assets to production source code?
You should not push locally pre-compiled assets to production source code, no.
Your build process should include precompiling assets for production during a deploy. If you're using Rails 5 then that is already turned on by default. As long as the production environment has a proper environment variable (again, by default, the environment variable 'production' takes care of all these things), the assets will be precompiled.
You say that your compressed files, after running rake assets:precompile, are exactly the same as the ones on the server. That means your javascript should have and should be minified and uglified (again, default for Rails 5). To confirm that's the case, open dev tools in chrome, hard-reload (ctrl+shift+r) and check with JS files are being loaded under "network" tab. If the asset pipeline was used as it should be, you should only see minified and uglified js files here. If you open them up, they should already be minified and uglified. If that's not the case then either the asset pipeline wasn't used for fetching the JS files or your build process has been changed.
If most of these JS files are minified and uglified with - check those which aren't. Is some library being added outside of asset pipeline? Are those files minified?
If all of your JS files are actually minified, then tell your tester to use a different tool ;)
i have a Rails application deployed on Openshift. I added marker for hot deploing and hot deploying itself works fine, but during the time application is hot deployed css and js files are not served. When hot deployment ends these files work fine again. I also use Bootstrap and Sass in this application (gem 'bootstrap-sass'). Do you have any idea why this happens?
The files are being served by Apache via the Passenger module. The files are being replaced "in place" which causes them to be removed/rebuilt, which is causing them to not be served during that time, and since they are static assets, they are not stored in memory. Unfortunately there is currently no way to make hot deploy work fully with Rails to keep the site 100% working while it is deployed.
One solution is to have your assets in a separate running project, since there is no easy way to have them available as all times as #developercorey explains..
It's probably not the best solution, but would be a simple patch-solution which is not tightly coupled to one particular hosting platform.
I fixed this issue, and it works now. I will explain what I did, maybe it will help somebody.
Basically there is need to precompile your assets locally, and commit and push them. This is done byrake assets:precompile RAILS_ENV=production
But there is a gotcha!!! Locally precompiled assets doesn't match those that are generated on Openshift. How is this possible? There is a bug on Openshift, that assets are generated on production with RAILS_ENV=development :/ More info here:
https://github.com/openshift/origin-community-cartridges/issues/8
so there is need to add environmental variable on your application:
rhc set-env RAILS_ENV=production -a app_name
then generated assets match.
So after fixing it, when during changes to assets, we need to precompile them again. And to make them work during hot deploying there is need to have both old precompiled assets and new precompiled assets in repo. For example:
If you have old file:
application-10770925dc8abd4ceab34119af4032163cc5a94f3523d60d321f33a999171d58.cssand new precoimpiled file:
application-82f6fca47056cbda52cb32086051f031b880e2630a137f0e41e96cb2eef923ee.css
they both have to be in repository. During hot deploying old asset is still referenced, so it has to be in repository. After hot deploying ends, new asset is referenced. In the next commit and push old asset may be removed.
So basically this issue is fixed for me, and hot deploying works fine now.
Setup: Rails 4.2.5, Ubuntu 12.04, nginx 1.6, unicorn 5, capistrano 2.15
The setup did work with Rails 3.2 but once i started to migrate to rails 4.x i noticed an odd behaviour with the assets.
Inside the application the links to the asset files like application.js are not the ones inside the manifest.yml
If i do a deploy with asset cleanup, i will get many 404. The fingerprints used by the app and the manifest.yml differ.
If i do right away a second deploy without a cleanup, the assets will be served. They wont match with the manifest again, but the old ones are still in the folder. Killing unicorn and making a full restart did not change anything.
I have the same problem in the 4.0 and 4.1 branch of my app.
The assets are compiled on the server during deploy (capistrano scripts)
update:
I checked via console which fingerprints are generated and which files are present. Result: hashes are fine.
Problem is that the cleanup script deletes files older than a week or something and when doing a deploy (asset precompile) i get a wrong timestamp for some files.
If i do a precompile just now, some files have a datestamp 4th January and get deleted by the following cleanup script. Capistrano shows me some warning when writing that file but without any meaningfull information
I'm fairly new to the whole asset pipeline concept, I put the third party js file (as well as the css and image files) in the vendor/assets folder, allow me to give a quick example how I did it:
where the file is stored:
vendor/assets/javascript/plugins/isotope/isotope.pkgd.min.js
how it's referenced in the application.js:
//= require plugins/isotope/isotope.pkgd.min
and how it's referenced in the index.html.erb
<script src="/assets/plugins/isotope/isotope.pkgd.min.js"></script>
it works just fine locally, but after I deployed the app onto heroku, I got http://myapp.herokuapp.com/assets/plugins/isotope/isotope.pkgd.min.js not found error, without it, my app does not work...I kinda know part of the asset pipeline's job is to compress all the js files (the ones referenced in the application.js) into one single js file, does not mean I will need to refer isotope.pkgd.min.js differently in my html.erb?
There are several steps in debugging an asset pipeline problem. You didn't mention you'd done an asset pre-compilation before deploy. Heroku does not compile assets upon deploy automatically. Here's their text on the matter
If a public/assets/manifest.yml is detected in your app, Heroku will assume you are handling asset compilation yourself and will not attempt to compile your assets. Rails 4 uses a file called public/assets/manifest-.json instead. On both versions you can generate this file by running $ rake assets:precompile locally and checking the resultant files into Git.
https://devcenter.heroku.com/articles/rails-asset-pipeline
A simple test would be to precompile your assets (and git checkin, if you're using git push Heroku) before Heroku deploy and see if that doesn't resolve the issue.
On your dev platform:
bundle rake exec assets:precompile RAILS_ENV=production
So the question basically boild down to this:
How do you efficiently handle changing assets in a production rails environment without the need to restart the server?
The problem we're experiencing is, we have to restart the Thin server that runs the app in order for the updated javascript files to be served.
Some background:
Right now we're generating data from a couple of long running tasks into javascript files once an hour so we can use it in our Rails app.
To be clear, we update/overwrite existing files, not adding new ones.
After generation we run these commands in order to re-precompile all the assets.
bundle exec rake assets:precompile
bundle exec rake rails_group=assets assets:clean RAILS_ENV=production
Still after clearing the browser cache and reloading the page we're being served the old assets.
Have you guys made any similar experiences; what did you do to work around it?
PS. Happy holidays to you all!
So, what we ended up doing is basically letting rails also serve static assets by setting
config.serve_static_assets = true
in config/environments/production.rb
and just putting the frequently changing javascript data files into a directory structure under public/.
This works grate since it also separates assets and data into different locations.
According to the Rails guide:
6 How Caching Works Sprockets uses the default Rails cache store to
cache assets in development and production.
Rails is going to cache your assets unless you tell it to not cache them. The whole point of the asset pipeline is to serve assets as quickly as possible by encouraging browsers and servers and the rails servers themselves to cache the assets.
If your use case involves redoing assets very often, maybe the asset pipeline isn't for you.