Permalink
Browse files

Initial implementation of cachify_js and cachify_css.

In production url is passthrough, in dev mode individual tags
are output for ease of debugging.

Added underscore as a dependency.
  • Loading branch information...
1 parent 6d448e3 commit fefa3d3ff1f9d143d27505609485e622650cb908 @ozten ozten committed Feb 9, 2012
Showing with 56 additions and 9 deletions.
  1. +11 −6 README.md
  2. +1 −1 index.js
  3. +43 −2 lib/connect-cachify.js
  4. +1 −0 package.json
View
@@ -1,8 +1,10 @@
# Cachify #
-The ``connect-cachify`` makes is super easy to have fantastic frontend caching behavior.
+``connect-cachify`` makes having proper browser cache and HTTP caching behavior for assets easier.
It is a set of middlware, helpers, etc for Node.js express framework.
+This does not provide in-memory caching, middleware caching, or many other types of caching. Cashify is focused on reducing the number of HTTP requests to your web nodes.
+
## Installation ##
npm install connect-cachify
@@ -52,19 +54,19 @@ Note: You **must** put ``cachify.setup`` before static or other connect middlewa
...
<head>
<title>Dashboard: Hamsters of North America</title>
- <%- csslinks('dashboard.min.css') %>
+ <%- cachify_css('dashboard.min.css') %>
</head>
<body>
...
- <%- jslinks('/js/main.min.js') %>
+ <%- cachify_js('/js/main.min.js') %>
</body>
...
-In production mode, a call to jslinks will produce a single script tag like
+In production mode, a call to ``cachify_js`` will produce a single script tag like
<script src="/js/fa6d51a13a245a90aeb48eeca0e52396/main.min.js"></script>
-When production was set to false, jslinks will produce:
+When production was set to false, ``cachify_js`` will produce:
<script src="/js/lib/jquery.js"></script>
<script src="/js/magick.js"></script>
@@ -73,10 +75,13 @@ When production was set to false, jslinks will produce:
## Options ##
The following are optional config for ``cachify.setup``
-* url_prefix - A url prefix to append to links generated in ``jslinks`` and ``csslinks``. **Example:** "http://cdn.example.com/media/"
+* url_prefix - A url prefix to append to links generated in ``cachify_js`` and ``cachify_css``. **Example:** "http://cdn.example.com/media/"
Using ``url_prefix``, one could support dark launches, since all assets are revisioned with hashes and the CDN network would service your request.
+## Wordpress Cachify ##
+Does this all sound like gobbledeegook? Maybe your looking for [Wordpress cachify plugin](http://wordpress.org/extend/plugins/cachify/) instead of ``connect-cachify``.
+
## Questions ##
* Should we always put hash in urls, even in development mode? Maybe that is a config flag, so we can do that while writing cachify?
View
@@ -1 +1 @@
-module.exports = require('lib/connect-cachify');
+module.exports = require('./lib/connect-cachify');
View
@@ -1,3 +1,44 @@
-exports.setup = function () {
+var format = require('util').format,
+ und = require('underscore');
-};
+var opts;
+
+exports.setup = function (options) {
+ opts = options;
+ if (options.production === undefined) {
+ opts['production'] = true;
+ } else {
+ opts['production'] = options['production'];
+ }
+
+ console.log(opts);
+ if (! options.js) options.js = {};
+ if (! options.css) options.css = {};
+
+ return function (req, resp, next) {
+ resp.local('cachify_js', cachify_js);
+ resp.local('cachify_css', cachify_css);
+ next();
+ };
+};
+
+var prod_or_dev_tags = function (filename, family, link_fmt) {
+ if (opts['production'] !== true &&
+ opts[family] &&
+ opts[family][filename]) {
+ return und.map(opts[family][filename], function (f) {
+ return format(link_fmt, f);
+ }).join('\n');
+ }
+ return format(link_fmt, filename);
+};
+
+exports.cachify_js = cachify_js = function (filename) {
+ return prod_or_dev_tags(filename, 'js',
+ '<script src="%s"></script>');
+};
+
+exports.cachify_css = cachify_css = function (filename) {
+ return prod_or_dev_tags(filename, 'css',
+ '<link href="%s" rel="stylesheet" type="text/css">');
+};
View
@@ -13,6 +13,7 @@
"node": ">=0.4.7"
},
"dependencies": {
+ "underscore": ">=1.3.1"
},
"devDependencies": {
}

0 comments on commit fefa3d3

Please sign in to comment.