Post navigation

Integrating SpreeCommerce Store with AWS CloudFront CDN

When your website receives thousands of visitors every day from around the world, we need to ensure that the experience does not deteriorate based on the geographical location of the user. But the reality is bitter and geographical location does plays a role in website’s responsiveness.

Why does the geography comes in play when we are showing same content to anyone accessing the site. It’s due to the latency at the network level, also known as “Fibre Optic Latency”. Same data needs to travel a large distance for one user as the server location is fixed.

We will explore how can we use Content Delivery Network to provide a virtual feeling of server closeness for every user irrespective of different geographical location.

Content Delivery Network allows you to serve the cached content from the edge locations close to user which are also known as Point of Presence, PoP. It reduces the load times, hence, adds to better user experience which reflects in revenues too :)

We have set the AWS CloudFront distribution to use our Amazon S3 bucket as Origin to retrieve assets if its not already cached with Cloudfront.

SpreeCommerce Customization

We should explore where all our assets are being managed and plan to have them on AWS S3 when AWS Cloudfront needs them.

We will list out some of the assets and tools which can be easily found in almost all SpreeCommerce stores and should be configured to use with CDN.

Spree Store Assets like stylesheets, javascripts, images and other.

Spree::Image which is used to store product and variant images

Spree::Taxon which has icon image

There could be Spree extensions which introduces their Paperclip models or editors which are used to manage assets like CKEditor

Basically, we need to ZERO down on the possibilities of asset references across our Spree Store & extensions and make sure that they are present on AWS S3 after deploy or as they are created from our Spree Store.

Don’t be disheartened, it’s not that tedious as it looks.

You can start with finding references of has_attached_file and start overriding url option as ":s3_alias_url"

We can use the decorator pattern to override the url option for code we don’t manage directly and change the code directly where we own it

# app/models/spree/image_decorator.rb
# Code we don't own and part of Spree::Core
Spree::Image.class_eval do
attachment_definitions[:attachment][:url] = ':s3_alias_url'
end