I launched a little thing late Saturday night. I was working on a quickie Action for TableKit, which I still haven’t gotten around to adding to ActionsForge and documenting and all, and I wanted to link to all of the assets rather than rolling up a bundle Action. I looked on Google’s CDN, and even CDNJS (Cloudflare’s new all-access CDN) and didn’t see TableKit anywhere. I could add it there, but I wanted to see how hard it would be to make my own CDN. After all, I’ve been hosting all of the videos for FreewayCast on Amazon S3 for years, it’s rarely over $0.50 per month to do that, and with Amazon’s CloudFront service, you can expose S3 to the world through a huge network of edge cache servers without writing any code.
So here’s the steps involved:
- Create a bucket on S3. Mine is called ajax-cdn.s3.amazonaws.com.
- Upload some files to it. (I used Transmit for this.)
- Select the bucket, choose Get Info, and set it to be world-readable, and copy those permissions to all contained objects.
- Still in the Get Info inspector, check the “Distribute Using CloudFront” box. Copy the domain name of the server created by this step.
- At your favorite DNS provider, create a new CNAME record in your domain that maps your subdomain of choice to the CloudFront server.
- Log into the AWS (Amazon Web Services) portal, and move to the CloudFront page. Locate your server in the Distributions list, and open up the Edit screen. Add the custom subdomain you created in step 5 and save changes.
In about 15 minutes, you have a working CDN. Any files you add to the server will be available momentarily throughout the world through a network of servers that are geographically close to your users, no matter where they may live. And that makes the difference between having to roll up a bundle Action, with all of the resources your Action needs hidden inside it, and being able to do something simple and declarative like this instead:
function fwBeforeEndHead(){
addPrototype();
if(!fwPage.tablekit){
var head = fwDocument.fwTags.fwFind('head');
fwPage['tablekit'] = true;
var script = head.fwAdd('script', true);
script.src = fwQuote('http://cdn.freewaypro.com/tablekit/1.3/tablekit.js');
script.type = fwQuote('text/javascript');
script.charset = fwQuote('utf-8');
script.fwAddRawOpt('');
head.fwAddRawOpt('');
var link = head.fwAdd('link');
link.href = fwQuote('http://cdn.freewaypro.com/tablekit/1.3/css/style.css');
link.type = fwQuote('text/css');
link.rel = fwQuote('stylesheet')
link.charset = fwQuote('utf-8');
head.fwAddRawOpt('');
}
}
It’s better for you, and your users, and it costs next to nothing. I’m petitioning Softpress to offer this as a service to Action developers, and I plan to write a simple Rails front-end next year to allow an admin to upload files quickly and simply.
For the short term, if you want to try this out, send me your files and I’ll load them up for you. If the underlying storage changes hands from me to Softpress, there won’t be any disruption in the URL. We’ll have to collaborate on naming and versioning, though. At the moment, it’s organized like this:
/ajax-cdn/
/tablekit/
/1.3/
/css/
/images/
tablekit.js
The one thing that CDNs make very painful is deleting or changing something after you’ve added it to the system. That’s why the 1.3 is there. If I rev this script or the files, I will just make a new version folder like 1.3.1 or 1.4, and put the new files in there. Adding is cheaper than changing, at least in terms of effort.
I’d heard rumblings from various developer blogs that this was easy, but I never realized just how easy until I tried.
Happy new year to all, and keep writing Actions!
Walter
actionsdev mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options