Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial release 0.0.1

  • Loading branch information...
commit 643938e5bc9d1e1065b1506b62afb03e1722d82b 1 parent 9e80c51
@niftylettuce authored
Showing with 278 additions and 0 deletions.
  1. +102 −0 .gitignore
  2. +76 −0 Readme.md
  3. +66 −0 index.js
  4. +34 −0 package.json
View
102 .gitignore
@@ -0,0 +1,102 @@
+
+# ------------------------------------------------------------------------------
+# Common Node files and folders
+# https://github.com/github/gitignore/blob/master/Node.gitignore
+# ------------------------------------------------------------------------------
+
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
+
+
+# ------------------------------------------------------------------------------
+# JSHint
+# ------------------------------------------------------------------------------
+
+jshint
+
+# ------------------------------------------------------------------------------
+# Nodemon
+# ------------------------------------------------------------------------------
+
+.monitor
+
+
+# ------------------------------------------------------------------------------
+# Numerous always-ignore extensions
+# ------------------------------------------------------------------------------
+
+*.diff
+*.err
+*.orig
+*.rej
+*.swo
+*.swp
+*.vi
+*~
+*.sass-cache
+
+
+# ------------------------------------------------------------------------------
+# Files from other repository control systems
+# ------------------------------------------------------------------------------
+
+.hg
+.svn
+.CVS
+
+
+# ------------------------------------------------------------------------------
+# Your ideas
+# ------------------------------------------------------------------------------
+
+.idea
+
+
+# ------------------------------------------------------------------------------
+# OS X and other IDE's folder attributes
+# ------------------------------------------------------------------------------
+
+.DS_Store
+Thumbs.db
+.cache
+.project
+.settings
+.tmproj
+*.esproj
+nbproject
+*.sublime-project
+*.sublime-workspace
+
+
+# ------------------------------------------------------------------------------
+# Dreamweaver added files
+# ------------------------------------------------------------------------------
+
+_notes
+dwsync.xml
+
+
+# ------------------------------------------------------------------------------
+# Komodo
+# ------------------------------------------------------------------------------
+
+*.komodoproject
+.komodotools
+
+
+# ------------------------------------------------------------------------------
+# Add your custom excludes below
+# ------------------------------------------------------------------------------
View
76 Readme.md
@@ -0,0 +1,76 @@
+
+# Express Cachebuster
+
+`express-cachebuster` provides cross-browser version-control/cache-busting as a dynamic helper to your views.
+
+ app.dynamicHelpers({ cacheBuster: require('express-cachebuster') });
+
+This was made as an integrated feature of [Expressling][1].
+[1]: http://expressling.com
+
+# Quick install
+
+ $ npm install express-cachebuster
+
+# Usage
+
+In your views simply include the dynamic helper and pass your asset's path.
+
+For more options see <a href="#goodies">Goodies</a>.
+
+**Note:** `$` = variable which is automagically returned by `express-cachebuster`.
+
+## EJS
+
+ <%- cacheBuster('/js/mylibs/script.js') %>
+
+Outputs HTML as shown below for development environment:
+
+ <script type='text/javascript' src='/js/mylibs/script.js?v=$currentTimestamp'></script>
+
+...and for production environment:
+
+ <script type='text/javascript' src='/js/mylibs/script.js?v=$lastModifiedTimestamp'></script>
+
+## Jade
+
+ != cacheBuster('/stylesheets/style.css')
+
+Outputs HTML as shown below for development environment:
+
+ <link rel='stylesheet' href='/stylesheets/style.css?v=$currentTimestamp'>
+
+...and for production environment:
+
+ <link rel='stylesheet' href='/stylesheets/style.css?v=$lastModifiedTimestamp'>
+
+# <a href="#goodies" name="goodies">Goodies</a>
+
+### You can pass an array of assets to the middleware:
+
+**EJS:** `<%- cacheBuster(['/stylesheets/style.css', '/stylesheets/ie.css']) %>`
+
+**Jade:** `!= cacheBuster(['/stylesheets/style.css', '/stylesheets/ie.css'])`
+
+Outputs HTML as shown below for development environment:
+
+ <link rel='stylesheet' href='/public/css/style.css?v=$currentTimestamp'>
+ <link rel='stylesheet' href='/public/css/ie.css?v=$currentTimestamp'>
+
+.. and for production environment:
+
+ <link rel='stylesheet' href='/public/css/style.css?v=$lastModifiedTimestamp'>
+ <link rel='stylesheet' href='/public/css/ie.css?v=$lastModifiedTimestamp'>
+
+
+### You can override the default "public" folder:
+
+**EJS:** `<%- cacheBuster('/stylesheets/style.css', 'www') %>`
+
+**Jade:** `!= cacheBuster('/stylesheets/style.css', 'www')`
+
+... which looks inside `/home/username/project/www` for `/stylesheets/style.css`.
+
+# License
+
+MIT Licensed
View
66 index.js
@@ -0,0 +1,66 @@
+
+// Express Cachebuster
+// Copyright (c) 2011 Nick Baugh (niftylettuce@gmail.com)
+// MIT Licensed
+
+// # Express Cachebuster
+
+var fs = require('fs')
+ , path = require('path')
+ , mime = require('mime');
+
+function getFile(asset, publicDir) {
+ if(typeof publicDir === 'undefined' || typeof publicDir !== 'string') {
+ publicDir = 'public';
+ }
+ var file = path.join(__dirname, publicDir, asset);
+ return [fs.statSync(file, function(err, stats) {
+ if(err || !stats.isFile()) {
+ console.log('\n express-cachebuster asset not found (is path correct?)');
+ process.exit(0);
+ }
+ }), file, asset];
+}
+
+function renderTag(stats) {
+ var v = 1
+ , t = mime.lookup(stats[1]);
+ if(app.settings.env === 'development') {
+ v = new Date().getTime();
+ } else {
+ v = new Date().parse(stats[0].mtime);
+ }
+ switch(t) {
+ case 'text/javascript':
+ return '<script type="'+t+'" src="'+stats[2]+'?v='+v+'"></script>';
+ case 'text/css':
+ return '<link rel="stylesheet" href="'+stats[2]+'?v='+v+'">';
+ default:
+ console.log('\n express-cachebuster cannot render ' + t);
+ process.exit(0);
+ }
+}
+
+module.exports = function(req, res) {
+ return function(asset, publicDir) {
+ var buf = []
+ , stats;
+ if(asset instanceof Array && asset.length > 0) {
+ for(var i=0; i<asset.length; i++) {
+ if(typeof asset[i] === 'string') {
+ stats = getFile(asset, publicDir);
+ buf.push(renderTag(stats));
+ } else {
+ console.log('\n express-cachebuster asset undefined in array');
+ }
+ }
+ } else if (asset && typeof asset === 'string') {
+ stats = getFile(asset, publicDir);
+ buf.push(renderTag(stats));
+ } else {
+ console.log('\n express-cachebuster asset is undefined');
+ process.exit(0);
+ }
+ return buf.join('\n');
+ };
+};
View
34 package.json
@@ -0,0 +1,34 @@
+{
+ "name": "express-cachebuster"
+ , "description": "Express asset rendering with version-control/cache-busting."
+ , "version": "0.0.1"
+ , "author": "Nick Baugh <niftylettuce@gmail.com>"
+ , "keywords": [
+ "cache"
+ , "cache-buster"
+ , "cachebusting"
+ , "cache-busting"
+ , "version"
+ , "version-control"
+ , "version control"
+ , "cross-browser"
+ , "cross browser"
+ , "jade"
+ , "ejs"
+ , "expressling"
+ , "express"
+ , "html5"
+ , "boilerplate"
+ ]
+ , "devDependencies": {
+ "express": "*"
+ , "ejs": "*"
+ , "jade": "*"
+ }
+ , "main": "index"
+ , "engines": { "node":">= 0.5.0 < 0.7.0" }
+ , "repository": {
+ "type": "git"
+ , "url": "git://github.com/niftylettuce/express-cachebuster.git"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.