CloudFront CORS with Custom Origin

Modern browsers respect Cross Origin Resource Sharing which means they won’t load web fonts (.woff, .ttf, etc.) from a CloudFront distribution.

If you are using a custom origin (AKA your website) for CloudFront you need to do the following:

1. Send the header Access-Control-Allow-Origin: * when serving up web fonts on your server.

Example for Apache (in .htaccess):

 # Set CORS headers so CloudFront will forward them
 <FilesMatch ".(eot|ttf|otf|woff|svg)">
 Header set Access-Control-Allow-Origin "*"
 </FilesMatch>

2. Edit your CloudFront distribution Behavior to Forward Headers and add Origin to the whitelist. I know this doesn’t make a ton of sense but that’s how AWS set it up (source).

 

 

3. Invalidate your cache from the AWS console or just wait until CloudFront refreshes its cache.

Use DateTime instead of Date for ActiveRecord ranges

I was working with a query that selected all “Orders” created this month, it looked like this:

 @today = Time.zone.now.to_date
 @start_date = @today.beginning_of_month
 @end_date = @today.end_of_month
 @orders = Order.where(created_at: @start_date..@end_date)

and the resulting SQL query looked like this:

 SELECT `orders`.* FROM `orders` WHERE (`orders`.`created_at` BETWEEN '2014-08-01' AND '2014-08-31')

…but since Rails automatically converts Date objects from localtime to UTC the dates in that query should have been offset by my timezone (in this case PDT -7 hours).

The fix was simple, use DateTime objects instead of Date objects and Rails automatically calculated the offset based on my Timezone (config.time_zone):

@today = Time.zone.now.to_datetime
 # ...

and the fixed query:

 SELECT `orders`.* FROM `orders` WHERE (`orders`.`created_at` BETWEEN '2014-08-01 07:00:00' AND '2014-09-01 06:59:59')