0

It's my first deploying a real application to production, and we are having some problems with speed on the website. The main problem right now is the time it takes to load the index page, so I know it's really a thing about queries or anything like that.

We are using Heroku + Puma + CloudFront as a CDN to server our static assets. Thing is, I'm not really that sure this should be happening:

Dev Tools

As you can see, application.js and application.css is being loaded twice, one from cloudfront and one from /assets. Should this be happening? This is only an example but is happening too with some images and .svgs that I have, they are 'being loaded' twice.

Important to notice that the call for including both js and css is only being made once in the head tag

Thanks

Cesar Palafox
  • 16
  • 1
  • 2

1 Answers1

0

The assets are being loaded once, but via two HTTP calls, the first being a redirect to the second. You can see that the first application.js results in a redirect (see below the URL in the third column) to the second application.js, and the same goes for application.css

To avoid the redirect, you need to configure Rails to load assets from a different domain. To do so, set config.action_controller.asset_host in the relevant environment config file, like so:

config.action_controller.asset_host = "http://XXXXXXX.cloudfront.net"
eirikir
  • 3,802
  • 3
  • 21
  • 39
  • Thanks for the answer! I do have the asset_host set on my production.rb, could there be any other reason? http://take.ms/goAI6 – Cesar Palafox Jul 24 '15 at 17:31
  • Are you sure RAILS_ENV is production for your Heroku app? Try `Rails.env` in the console. If it is production, can you paste the rest of your production.rb config? and the code you use to include the assets? – eirikir Jul 24 '15 at 17:33
  • @eirkir It is production, the console says so and the SSL config and the domain and s3 bucket is in place. This is the code for production.rb http://take.ms/Ey9VJ and http://take.ms/BvncN thanks – Cesar Palafox Jul 24 '15 at 18:08
  • 1
    Why do you have `config.assets.compile = true`? See this [excellent SO post](http://stackoverflow.com/questions/8821864/config-assets-compile-true-in-rails-production-why-not) to understand the consequences. Also, separately, your assets are huge (and that's not a compliment). You may want to take a hard look at the JS you are including to see if you can cut it down a bit. – steve klein Jul 24 '15 at 19:12
  • @CesarPalafox I misread the output above: it looks like Cloudfront is redirecting to your Rails server (not vice-versa). How are your precompiled assets supposed to reach the CDN? Are you using asset_sync or another gem? – eirikir Jul 24 '15 at 19:22
  • @steveklein awesome read, didn't know about that, thought that since I already had the precompiled assets it wouldn't matter. Removing that right now. Yeah, the file is big :/, we are using some libraries and gems that rely on using some JS I'm at loss figuring out how to trim it even more. Only thought was to leave basic JS functionality for the static pages and through all the app stuff together for the user pages. – Cesar Palafox Jul 24 '15 at 21:15
  • @eirikir I precompile all the assets on my local and deploy them with the rest of the app. The CDN is configured to take look for the asset in the server if it doesn't have it, no gems to upload the assets to S3 or anything like that – Cesar Palafox Jul 24 '15 at 21:15
  • Are you sure the assets are being uploaded to the CDN? If you're using S3, do the have a permission for Everyone to open? – eirikir Jul 24 '15 at 21:47