Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero

Serving Static Assets from an external server or CDN

Posted on July 12, 2021February 27, 2023 by Rails Hero

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,

  1. i created a new bucket on digitalocean spaces,
  2. then i uploaded all of my assets to that bucket,
  3. and then i enabled CDN and static site hosting for that bucket.
  4. 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

  1. setup a cloud server in digitalocean, vultr, AWS, or anywhere else with OS of your choice: ubuntu, centOS, etc
  2. Install and run any HTTP Web server such as Apache, NGINX, etc
  3. 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/)
  4. 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

3 thoughts on “Serving Static Assets from an external server or CDN”

  1. Pingback: Adding custom error pages in your rails app - Railshero
  2. Pingback: Adding custom error pages to our rails app - Railshero
  3. Pingback: Deploying rails apps on heroku fails? here's what you can do to fix - Railshero

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tags

active record active storage assets assign to many associations attachment attachments authentication authorization avatar bootstrap cdn config database deploy deployments devise DRY environment variables file uploads Gemfile gems has_many helpers heroku index indexing initializer javascript pagination parameters postgres production public routes.rb ruby gem ruby gems search sendgrid server smtp stylesheets variants views voting

Recent Posts

  • Understanding the DRY Principle in Rails
  • Building Multi-tenant Applications with Rails
  • Rails Basics: Templating Engines
  • Deploying With Capistrano
  • Automated Testing in Rails

Archives

  • July 2023
  • June 2023
  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Active Record
  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • Rails Basics
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • Testing
  • User Interface
  • Video
  • Views & Templating
  • Voting
  • Web Security
©2025 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT