How does Rails asset pipeline do preprocessing? - ruby-on-rails

In this blog, it says that assets pipeline will compile, compress, and preprocess your assets from your app/assets and then placed the out into public/assets. I don't understand the below where it says you can set up a Nginx or Apache server to preprocess your assets for you after the it already has been outputed to the public/assets. Is it saying that you can choose to have additional servers such as Apache/Nginx to do the preprocessing for your while the Rails server does the compiling/compressing?
bundle exec rake assets:precompile
This will create (by default) an assets directory in your public/
folder. It will then add all the compressed and compiled files into
that directory, in the appropriate formats and with the new digested
versions. You can then set up Nginx or Apache to server those files
directly so that Rails doesn’t have to deliver them (and run the
on-the-fly preprocessing, etc.) itself.

I don't understand the below where it says you can set up a Nginx or Apache server to preprocess your assets for you…
It doesn't say that. It does say the following:
You can then set up Nginx or Apache to server [sic] those files…
There's a difference between preprocessing and serving files. What this documentation is suggesting is that if you're using a server like nginx, you can configure it to handle requests to assets that exist in your public/assets folder. This alleviates your Rails app from handling those requests.
Pre-processing is still handled by Rails in advance of nginx coming into the picture.

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

heroku precompile assets is it necessary

I've started learning rails and I've already built two apps, one simple blog app and one store app. Now I ran into a term precompile assets when uploading to heroku, can someone explain it to me is that necessary when deploying an app to production, because i've uploaded my store app to heroku without any problems?
Assets is your css + JS. Precompile assets mean that they get joined into single .css and another single .js. file (to load it in one HTTP request). And special mechanism of minifying get applied to both these files (to make them smaller). Rails by default is setup in a way, that it uses average files in dev and compiled files in prod. You can easily change this in configs, but you shouldn't do this unless you really know what you do.
If you want you can compile this files locally running rake assets:precompile and then put it into git. I think that you can disable/enable precompile during heroku deploy in heroku config. But, in general, I would stick with the very defaults.
More info on asset pipeline: http://guides.rubyonrails.org/asset_pipeline.html
Rails has an assets pipeline which consists of Sprockets and the assets helpers.
The assets pipeline will concat and minify your CSS and javascript and takes care of setting the correct paths to images and other assets. This is known as compiling the assets.
In development this is done on the fly for each request which lets you immediately see changes.
In production this would be far to slow so instead the assets should be compiled once at deploy time. Heroku does this automatically for you in a post-commit hook.
Pre-compiling is when you run rake assets:precompile locally and then upload or push the result to a server. This is done if you are deploying to a server without the support for the assets pipeline. For example if the production server does not have a javascript runtime which is required to run uglifier.
It adds tons of noise to the git change history and manually doing anything is a common source of user error. So pretty much it sucks and you only do it if you have to.

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.

How to deploy Rails 4 with Capistrano 2 and precompile assets locally

Recently I upgraded an application from Rails 3 to Rails 4. In the deploy scripts I precompile the assets locally and then rsync them up to the server(s). In Rails 4 the asset pipeline now produces manifest- < random > .json instead of a manifest.yml. Since the manifest files are named differently, this adds multiple manifest.json files to the shared assets directory. The application then picks up the wrong manifest file, and serves old assets.
I have read about various issues related to this in some github pull request threads:
https://github.com/capistrano/capistrano/pull/412
https://github.com/capistrano/capistrano/issues/210
https://github.com/capistrano/capistrano/pull/281
My options seem to be:
Don't share the asset directory.
This would break old clients asking for old resources.
Switch to compiling assets on the servers.
This would add complexity to the server.
Move the manifest file outside of the shared asset directory.
I have since learned that this option was removed in Rails 4.
Are there other solutions to this problem?
I found the best answer after looking at the standard capistrano rails asset precompile task. I added a command to the local precompile task that moves the old asset manifest to the current release as asset_manifest.json. This leaves only one manifest when the new one is uploaded.
run "mv -- #{shared_manifest_path.shellescape} #{current_path.to_s.shellescape}/assets_manifest#{File.extname(shared_manifest_path)}".compact
Moving the manifest-.json to the current_dir as asset_manifest.json allows capistrano to restore the correct manifest file on rollback.

Rails Asset Pipeline Path Issue

I have a Rails app running on a shared web host under a folder in my root direction called 'mintrus-ror/'. There is a symbolic link 'public_html/' that points to 'mintrus-ror/public/'. My Rails app loads in the browser but the stylesheets don't load. Looking at the rendered source I noticed the assets path it is using is '/mintrus-ror/assets/application.css'. I am trying to figure out how to change it so it does not include the 'mintrus-ror/' directory in the assets path. Any ideas?
You can try playing with the config.assets.manifest configuration option in config/environments/production.rb. There are other variables that influence this, one being the web server configuration. I've got no recent (less than six years ago) experience with shared hosts, but I've read that on some systems, you can edit the .htaccess file.
I also presume that you're running in production mode and have previously compiled the assets with rake assets:precompile during your deployment step. Capistrano does this automatically when the asset pipeline integration is turned on.
The Rails Guide on the Asset Pipeline may be useful.
Your best bet may be to host on Heroku. It's free for small sites and a lot less hassle.

Resources