The Asset Pipeline
The rails asset pipeline is the default way to serve assets to your rails apps, but it’s not the best and not the only way. By default rails will use the asset pipeline to serve static assets, its a quick and easy way to serve static assets, but it degrades the performance of your rails apps, because it is slow and even when deploying to heroku.
I had to face many issues with the default way. There are a lot of ways to serve static assets, but i wanted it to be quick and easy to do, so this is my way of serving static assets: keep in mind that this is a really unusual way of adding static assets, it’s time consuming(as i have to re-upload the assets every time i make changes to it) and also not ‘DRY’, but it works, and its blazing fast, so what more can i expect ?
Serving Assets With CDN (Digitalocean Spaces)
i have often faced problems with the asset pipeline after deploying my rails apps to heroku, even after precompiling my assets locally and deploying it, some of the assets just doesn’t load the way i wanted it to.
So i came up with a easy and simple solution to fix this issue,
- i created a new bucket on digitalocean spaces,
- then i uploaded all of my assets to that bucket,
- and then i enabled CDN and static site hosting for that bucket.
- after that i just added my assets, such as images and stylesheets the regular old way
Serving with HTTP Web Server
Similarly if i wanted to host in an external server, i would setup a HTTP web server on the cloud or somewhere else and then i would upload all of my assets in that server, and add the source of my assets to my rails apps. Any kind of web server will work, Apache, NGINX, etc. and also there’s not need of installing any other runtimes such as PHP or even Ruby, just plain old web server that can serve files over HTTP because its just static assets.
Easiest serve assets with a http web server
- setup a cloud server in digitalocean, vultr, AWS, or anywhere else with OS of your choice: ubuntu, centOS, etc
- Install and run any HTTP Web server such as Apache, NGINX, etc
- Upload your assets, from app/assets folder to that cloud server using FTP, SFTP, SCP or any other way. (copy it to the root http directory eg; var/www/html/)
- Now just test the web server and add links to your assets
Although this can be also done, but i recommend using a CDN or simply using a regular old AWS S3 Bucket as it’s much better that way
How i added my asset sources
i added my asset sources the old fashioned way and also in future i might move the assets to a different server or a different bucket so i did the following so that i can change it quickly.
in my application controller, i added a few global variables like this
#File: app\controller\application_controller.rb
class ApplicationController < ActionController::Base
#bucket endpoint example: 'https://mybucket.sgp1.digitaloceanspaces.com'
$myasset_link = "https://linktomybucket.bucket.com"
#for images directory
$myimages_link = $myasset_link+"/images/"
#for stylesheet directory
$mystyles_link = $myasset_link+"/stylesheets/"
end
$myasset_link is the URL of my bucket on digitalocean, if you are using a HTTP server to serve assets this will be the domain/hostname of the server or even a direct IP address to the server will work.
I added separate directories for my images and stylesheets so i added two more global variables separately for that.
now whenever i want to add a new stylesheet i can do this in my views, actually i had to do it for every stylesheet
#File: app\views\layouts\application.html.erb
<link href="<%= $mystyles_link %>bootstrap.css" rel="stylesheet" />
<link href="<%= $mystyles_link %>style.css" rel="stylesheet" />
and for images i can do this:
<img alt="image" class="rounded-circle" src="<%= $myimages_link %>profile_small.jpg">
There are also other ways that you can use to serve assets to your rails apps, this is not the most efficient way, but it gets the job done, quickly and easily. let me know if you have used other ways to serve assets