Rails can't find asset after page reload - ruby-on-rails

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.

Related

Loading webpacker assets in production

I'm trying to deploy to production (on a local machine) a Rails 5.2 app which uses webpacker for assets managemnet (I have totally replaced the assets pipeline).
Everything seems ok: as part of my deployment process I run the webpacker:compile task and both JS and CSS are compiled in the public/packs folder.
However, the assets aren't loaded from the app even if they are correctly linked.
Am I missing anything here?
I have tried to load via browser other files in the /public folder (i.e. robots.txt) but they are not availble neither. I get "The page you were looking for doesn't exist." error message.
In production by default rails expects to be behind a reverse proxy server like nginx that will serve all static files from public more efficiently.
Also for low loads the built-in file server can be enabled as a quick-fix, in production.rb:
config.public_file_server.enabled = true

Rails not precompiling images in the app/assets/images folder?

I have some images (svg) in my app/assets/images folder. According to the Rails Guides, all the files in the assets folder should be automatically precompiled.
However, when I reference the the image using image_tag('filename'), it shows me an Sprockets::Rails::Helper::AssetNotPrecompiled error
Asset was not declared to be precompiled in production.
It tells me to declare the file to be precompiled manually, but why should that be necessary? On top of that, why does it concern itself with the production environment when I am doing everything in development?
If you added the image after you've started the server in development, restart the server. Sprockets will then precompile that image and the error will go away.
I'm pretty sure Rails doesn't support .svg yet, hence why it would ignore it.
You'll need to include the file extensions in your config/application.rb file:
#config/application.rb
config.assets.precompile += %w(.svg)
In regards the application concerning itself with the production environment, you have to remember that the precompilation process is meant for production:
The first feature of the pipeline is to concatenate assets, which can reduce the number of requests that a browser makes to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application.
Concantenating assets essentially means to compile your asset files into a single file, which is typically then minified.
--
Although this can be done in real-time, it's mostly the realm of static assets (which have to be precompiled). This means that if you run the rake asstes:precompile task, it will work on the development environment, unless you call RAILS_ENV=production rake assets:precompile (which sets it to the production environment for that request.
why does it concern itself with the production environment when I am doing everything in development
The application is going to run in production, not development.
Ultimately, everything you do in development should make it easier / better to work in production. In the sense of your assets, it means that you can use many of the quirks of Rails' asset pipeline, from sprockets to preprocessors such as SASS & Coffeescript
It's probably because you didn't specify the complete image name. I ran into this problem after updating the gem too. Before I just used image_tag 'some-image', but it seems that you now have to specify what type of image/extension you want to use.
Try this: image_tag 'some-image.svg'. It worked for me.
Cheers.

Pushing RoR app to Heroku, files in /public are missing

Let me start by saying I am using Ruby 2.0.0 and Rails 4.1.1
I've worked my way through the Treehouse basic RoR course; ending in a very basic version of Twitter. I have the application running just fine on my local install, but when I pushed it to Heroku it seems to be missing the files in the /public directory; namely the /assets css and javascript.
I've precompiled my assets as instructed, and verified that they area indeed showing up on my GitHub remote that is using the same branch. I was told that Heroku will not compile your assets for you.
All my routes and HTML is displaying just fine, but I cannot pull any of the files that live in the /public directory (404.html, 500.html, etc)
It feels to me like it is a permissions issue or something with the /public directory, but I haven't found a way to actually browse what files are on my Heroku instance. I've tried re-pushing several times while making small changes, and the css/js never seems to appear.
In case that you have already set:
config.serve_static_assets = true
in your config/environments/production.rb
And still not working, you can actually see the logs from your heroku app using heroku logs or heroku logs -n NUMBER_OF_DESIRED_LINES_HERE in your terminal.

Updating assets without restarting rails server

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.

Rails 3.1 & Sprockets & compiled JS files

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.

Resources