Precompile Rails 5 to Include all Assets
Asked Answered
R

3

9

I am trying to precompile all of my assets for production. When I run RAILS_ENV=production bundle exec rake assets:precompile not all of my assets are precompiling. I have tried to use the methods that others have suggested in other similar questions but they have not worked for me. For starters here is what my assets.rb looks like:

Rails.application.config.assets.version = '1.0'


Rails.application.config.assets.paths << Rails.root.join('node_modules')



Rails.application.config.assets.precompile += %w(creative/manifest.js creative/manifest.css images/* bootstrap/* fonts/* magnific-popup/* morphext/* owlcarousel2/* rs-plugin-5/* stylesheets/* javascripts/*)

As you can see where I am precompiling I have tried to add for example directory/* to include everything. Here are the files I am trying to include and everything in them (These are in the Assets directory):

bootstrap  
fonts   
javascripts     
morphext      
rs-plugin-5
config     
images  
magnific-popup  
owlcarousel2  
stylesheets

Here is what is precompiling in my public directory when I run the precompile take a look at the image under public and as you can see many of the directories are not in there I have even opened up each folder to see if they are in another folder and no nothing is there:

 public directory

The last part is when I run rails s -e production and I navigate to Localhost:3000 I keep getting a 500 error and nothing is showing. There are no errors in the console in chrome nor the terminal so I cannot see what my page would look like locally for production. Any help will be greatly appreciated.

Update

Here is my production.log it looks like it is not getting the necessary assets:

I, [2017-09-12T18:43:54.606853 #7393]  INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] Started GET "/" for 127.0.0.1 at 2017-09-12 18:43:54 -0700
I, [2017-09-12T18:43:54.625461 #7393]  INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] Processing by CreativesController#index as HTML
I, [2017-09-12T18:43:54.642161 #7393]  INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c]   Rendering creatives/index.html.erb within layouts/application
I, [2017-09-12T18:43:54.644418 #7393]  INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c]   Rendered creatives/index.html.erb within layouts/application (2.1ms)
I, [2017-09-12T18:43:54.644629 #7393]  INFO -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] Completed 500 Internal Server Error in 19ms
F, [2017-09-12T18:43:54.645433 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c]   
F, [2017-09-12T18:43:54.645478 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] ActionView::Template::Error (The asset "AdobeStock_95578405.jpeg" is not present in the asset pipeline.):
F, [2017-09-12T18:43:54.645761 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c]     17:                 <li data-transition="slidehorizontal" data-slotamount="default" data-masterspeed="default" data-title="Next Generation Care">
[b2f4fb53-c1ea-4d59-8045-44167c60416c]     18:                 
[b2f4fb53-c1ea-4d59-8045-44167c60416c]     19:                 <!-- main image -->
[b2f4fb53-c1ea-4d59-8045-44167c60416c]     20:                 <%= image_tag "AdobeStock_95578405.jpeg" ,alt: "slidebg1", :data => {bgposition: "center bottom",bgrepeat: "no-repeat", bgfit: "cover"} , :class => "rev-slidebg" %>
[b2f4fb53-c1ea-4d59-8045-44167c60416c]     21: 
[b2f4fb53-c1ea-4d59-8045-44167c60416c]     22:                 <!-- Transparent Background -->
[b2f4fb53-c1ea-4d59-8045-44167c60416c]     23:                 <div class="tp-caption dark-translucent-bg"
F, [2017-09-12T18:43:54.645799 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c]   
F, [2017-09-12T18:43:54.645828 #7393] FATAL -- : [b2f4fb53-c1ea-4d59-8045-44167c60416c] app/views/creatives/index.html.erb:20:in `_app_views_creatives_index_html_erb___200307797594027740_70304554688000'
I, [2017-09-12T18:43:54.734601 #7393]  INFO -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61] Started GET "/favicon.ico" for 127.0.0.1 at 2017-09-12 18:43:54 -0700
F, [2017-09-12T18:43:54.735010 #7393] FATAL -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61]   
F, [2017-09-12T18:43:54.735048 #7393] FATAL -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61] ActionController::RoutingError (No route matches [GET] "/favicon.ico"):
F, [2017-09-12T18:43:54.735071 #7393] FATAL -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61]   
F, [2017-09-12T18:43:54.735100 #7393] FATAL -- : [8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/debug_exceptions.rb:63:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] railties (5.1.3) lib/rails/rack/logger.rb:36:in `call_app'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] railties (5.1.3) lib/rails/rack/logger.rb:24:in `block in call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] activesupport (5.1.3) lib/active_support/tagged_logging.rb:26:in `tagged'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `tagged'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] railties (5.1.3) lib/rails/rack/logger.rb:24:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/request_id.rb:25:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] rack (2.0.3) lib/rack/method_override.rb:22:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] rack (2.0.3) lib/rack/runtime.rb:22:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] activesupport (5.1.3) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] railties (5.1.3) lib/rails/engine.rb:522:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/configuration.rb:225:in `call'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/server.rb:605:in `handle_request'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/server.rb:437:in `process_client'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/server.rb:301:in `block in run'
[8f353cbb-4884-4c2c-896d-ba60b58caa61] puma (3.10.0) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
I, [2017-09-12T18:43:58.249911 #7393]  INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] Started GET "/" for 127.0.0.1 at 2017-09-12 18:43:58 -0700
I, [2017-09-12T18:43:58.251066 #7393]  INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] Processing by CreativesController#index as HTML
I, [2017-09-12T18:43:58.252212 #7393]  INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26]   Rendering creatives/index.html.erb within layouts/application
I, [2017-09-12T18:43:58.253079 #7393]  INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26]   Rendered creatives/index.html.erb within layouts/application (0.7ms)
I, [2017-09-12T18:43:58.253250 #7393]  INFO -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] Completed 500 Internal Server Error in 2ms
F, [2017-09-12T18:43:58.254061 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26]   
F, [2017-09-12T18:43:58.254123 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] ActionView::Template::Error (The asset "AdobeStock_95578405.jpeg" is not present in the asset pipeline.):
F, [2017-09-12T18:43:58.254377 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26]     17:                 <li data-transition="slidehorizontal" data-slotamount="default" data-masterspeed="default" data-title="Next Generation Care">
[2e6342ac-ec61-410c-98ba-ae205fb9cc26]     18:                 
[2e6342ac-ec61-410c-98ba-ae205fb9cc26]     19:                 <!-- main image -->
[2e6342ac-ec61-410c-98ba-ae205fb9cc26]     20:                 <%= image_tag "AdobeStock_95578405.jpeg" ,alt: "slidebg1", :data => {bgposition: "center bottom",bgrepeat: "no-repeat", bgfit: "cover"} , :class => "rev-slidebg" %>
[2e6342ac-ec61-410c-98ba-ae205fb9cc26]     21: 
[2e6342ac-ec61-410c-98ba-ae205fb9cc26]     22:                 <!-- Transparent Background -->
[2e6342ac-ec61-410c-98ba-ae205fb9cc26]     23:                 <div class="tp-caption dark-translucent-bg"
F, [2017-09-12T18:43:58.254415 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26]   
F, [2017-09-12T18:43:58.254442 #7393] FATAL -- : [2e6342ac-ec61-410c-98ba-ae205fb9cc26] app/views/creatives/index.html.erb:20:in `_app_views_creatives_index_html_erb___200307797594027740_70304554688000'

UPDATE 2

Here is the image of my asset folder structure:

Asset Path

I also noticed that there is another assets folder because it is precompiling the rs-plugin's structure and not the other folders see image:

rs-plugin

Here is my application.rb:

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Cnd
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1
    config.assets.paths << Rails.root.join("app", "assets", "fonts")
    config.assets.paths << Rails.root.join("app", "assets", "images")
    config.assets.paths << Rails.root.join("app", "assets", "bootstrap")
    config.assets.paths << Rails.root.join("app", "assets", "rs-plugin-5")
    config.assets.paths << Rails.root.join("app", "assets", "magnific-popup")
    config.assets.paths << Rails.root.join("app", "assets", "owlcarousel2")
    config.assets.paths << Rails.root.join("app", "assets", "morphext")

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
  end
end
Resendez answered 8/9, 2017 at 3:58 Comment(11)
your problem is only in production? did you run RAILS_ENV=production bundle exec rake assets:precompile? Also if the problem is from development, please include the error message you get there. Production will suppress some details. In some cases some errors with the Asset Pipeline may only show in production. I need to understand what is not working in the asset pipeline. No files are included in the asset pipeline or some specific files are not included?Dryad
Had to edit my question out that's the code I ran RAILS_ENV=PRODUCTION... and development side works fine my only issue is trying to get everything running right in production.Resendez
enable the error messages in production and the log so you can have a better understanding of the error. You did not post any error or you did not tell us which files are not getting included. for example a specific file is not getting precompiles. It is missing from the application.js or from the application.scss? But your app is not working, it has a 500 Internal Server Error. Probably means that you are trying to load files and rails does not find them. Usually this error comes when you need to include some assets in your assets.rb or when the asset is not found in that pathDryad
Another problem is that rails in development sometimes will find the file in the app/assets/ folder if it does not find it in the public/assets/ folder. so it falls back to the non fingerprinted asset. To test this you can disable this feature config.assets.unknown_asset_fallback = false from the guide chapter 3.2 of guides.rubyonrails.org/asset_pipeline.html#turning-digests-offDryad
How do I enable error messages in production?Resendez
read the guide i posted.. you ll find everything there and y need to know the asset pipelineDryad
You need to read what kind of error it is. Check out logs directory on productionClasp
Whomever downvoted my post it would be helpful if you can explain why so I can learn. I might not have everything you would want but the purpose of this post was to learn as this is my first time launching in production.Resendez
Based on your screenshot, you've got an assets directory inside your assets directory. That doesn't seem right, but I've no idea how that'd happen either. Is your public/assets directory in source control? Have you manually added files to that directory?Reunionist
Also I believe you changed the /assets path by doing config.assets.prefix = "/some_other_path" because you files are precompiled inside public/assets/assetsDryad
I didn't change the asset path it is just precompiling the file structure from my rs-plugin-5 see the last image I updated.Resendez
D
7
Rails.application.config.assets.precompile += %w(creative/manifest.js creative/manifest.css images/* bootstrap/* fonts/* magnific-popup/* morphext/* owlcarousel2/* rs-plugin-5/* stylesheets/* javascripts/*)

but as I see from your screenshot you have a folder named assets inside your public/assets directory.

Your fingerprinted-files precompiled files are inside public/assets/assets instead of public/assets

This means that if those files are being push to production (check that they are in your git repository, if not do a git add), you are not going to be able to see them at the path localhost:3000/assets/<nameofyourfile> because your directory structure is changed, maybe you could display them at localhost:3000/assets/assets/<nameofyourfile>, but If I was you I would keep the normal directory structure.

Precompiling your assets means creating a fingerprinted version of it

By saying I want to precompile all my assets you are saying that you want all the files added or edited in your asset pipeline to generate a precompiled fingerprinted version to be used in production.

How does precompilation works? You can read chapter 4.1 of the Asset Pipeline Documentation, basically sprockets uses a file called .sprockets-manifest-fingerprint.json to check which is the latest version of the fingerprinted files to use in production. This is why sometimes deleting the public/assets folder solves problems, because you delete all the old files including the sprockets-manifest.json which may not be updated with the latest fingerprint.

Also development will fallback to the files in your app/assets folder if it does not find them in your public/assets folder, that is why you are getting the error only in production. You can read hear how to disable this behaviours

chapter 3.1 and 3.2

So do those steps, because errors are usually easier to solve in development, as you do not have to push a git to see the result. Sometimes files are included in .gitignore or you need to manually add that file with git add app/assets/<nameofthefolder>/<nameofthefile>

Also we did see your public folder in the screenshot, but we did not see you app/assets folder structure and any of your sprockets settings, so maybe it is not about adding a bounty to the question to get a solution, but just giving us the information necessary to find the solution.

Right now this is a generic opinion based question

Also I believe you changed the /assets path by doing config.assets.prefix = "/some_other_path"

Dryad answered 19/9, 2017 at 7:55 Comment(9)
it says assets/assets in the public directory because the only assets being compiled are the rs-plugin-5 see the last image I posted. I am not sure why it is only compiling that one and not the other plugin folders.Resendez
@JermaineSubia the one you are using is not really the best practice. you should require the js files in your manifest.js and the css files in your manifest.scss. The reason for this is that the order the files get loaded my cause errors in your Js and css.Dryad
I was doing that initially but what I found was that the plugins had other requirements like images or other files that without them when rendered locally would not function properly until I put the plugins in my app.Resendez
In order to make that work I had to make some changes to my application.rb file which I added to my question.Resendez
ok. You should have 3/4 folders. javascript folder which will all be required in your manifest.js or you can have more then one manifest.js, stylesheets that will be required in your manifest.scss then you can have a folder for images and fonts, I suggest to just require each one of those files explicitly in your assets.rbDryad
@JermaineSubia [b2f4fb53-c1ea-4d59-8045-44167c60416c] 20: <%= image_tag "AdobeStock_95578405.jpeg" ,alt: "slidebg1", :data => {bgposition: "center bottom",bgrepeat: "no-repeat", bgfit: "cover"} , :class => "rev-slidebg" %> what about this. it should be <%= image_tag 'AdobeStock.jpeg' %>Dryad
@JermaineSubia do not include the fingerprint of the asset. The file sprockets.json will find out which is the fingerprint to be used. Include sprockets.json from public/ in this postDryad
Let us continue this discussion in chat.Dryad
@JermaineSubia do you want to check this issue? #46396264 maybe it works giving the subfolder like in that issueDryad
K
6

First make sure of this if you want your rails application to serve.

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

Regarding assets compilation be aware that you may need to add into your application.css or .js if you are talking about css or js respectively. Check this.

For example (like in the link above)

app/assets/javascripts/home.js
lib/assets/javascripts/moovinator.js
vendor/assets/javascripts/slider.js
vendor/assets/somepackage/phonebox.js

would be referenced in a manifest like this:

//= require home
//= require moovinator
//= require slider
//= require phonebox

Assets inside subdirectories can also be accessed.

app/assets/javascripts/sub/something.js

is referenced as:

//= require sub/something

A few things I noticed:

  1. You have an assets folder within another assets folder. That doesn't seem right at all.
  2. Can you show me your asset directory where you have that AdobeStock_95578405.jpegimage?
  3. Add Rails.application.config.assets.precompile in your config/initializers/assets.rb file.
  4. Oh btw, make sure you restart your server every time you make changes while testing this. I already spent too many hours just because I completely forgot this simple thing.
Keynesianism answered 15/9, 2017 at 10:39 Comment(4)
Just added an image of my file structureResendez
What I am finding is that it is only precompiling the images and fonts and only the rs-plugin. See the images I just uploadedResendez
Are you adding the others into your manifest? are you requiring the entire tree? Also, I think that if you add assets into your "assets" folder, you don't need to include them again in your application.rb file. You really just need to require them on your manifestKeynesianism
Btw, third party libs should be insider vendor folder. Only specific app assets must be on the assets pipeline folder.Keynesianism
S
4

This could be wrong, but it looks like you left out config from your list of precompiled directories. Try updating your list to:

Rails.application.config.assets.precompile += %w(creative/manifest.js creative/manifest.css images/* bootstrap/* fonts/* magnific-popup/* morphext/* owlcarousel2/* rs-plugin-5/* stylesheets/* javascripts/* config/*)
Sheepdip answered 13/9, 2017 at 20:27 Comment(1)
What I am finding is that it is only precompiling the images and fonts and only the rs-plugin. See the images I just uploaded.Resendez

© 2022 - 2024 — McMap. All rights reserved.