Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export fingerprint #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Last-Modified, ETag, and Expires).

## API

static-asset exposes a function `req.assetFingerprint`, which allows one to generate
static-asset exposes a function `req.assetFingerprint` ( by default, though it can be exported under a different name, in case you want to use the middleware for more than one directory ), which allows one to generate
and register URL fingerprints for static assets.

Once a URL fingerprint is *registered* with static-asset, any HTTP request for that
Expand Down Expand Up @@ -193,6 +193,25 @@ This will render to something like this:
Notice that static-asset added a URL fingerprint (the UNIX timestamp
1318365481) to the filename.

Alternatively, if you want to use the middleware for more than one folder, you can do so like this:

```javascript
var express = require('express');
var app = express();
var staticAsset = require('static-asset');
app.use(staticAsset(__dirname + "/public/") );
app.use(staticAsset(__dirname + "/other-public/", null, "secondaryAssetFingerprint") );
app.use(express.static(__dirname + "/public/") );
//... application code follows (routes, etc.)
```
And then, in your jade view:

```jade
script(type="text/javascript", src=assetFingerprint("/client.js") )
script(type="text/javascript", src=secondaryAssetFingerprint("/client2.js") )
```


## More Advanced Usage

You can override the "cache strategy" with your own implementation that might
Expand Down
19 changes: 11 additions & 8 deletions lib/static-asset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var path = require("path"),
fs = require("fs");
module.exports = staticAsset;
function staticAsset(rootPath, strategy) {
function staticAsset(rootPath, strategy, assetFingerprintName) {
/* Labels are named resources that have been manually assigned a fingerprint by
calling `req.assetFingerprint(label, fingerprint, cacheInfo)`

Expand All @@ -28,6 +28,9 @@ function staticAsset(rootPath, strategy) {
*/
var fingerprints = {};

if(!assetFingerprintName)
assetFingerprintName = "assetFingerprint";

if(!strategy)
strategy = staticAsset.strategies["default"];

Expand Down Expand Up @@ -56,20 +59,20 @@ function staticAsset(rootPath, strategy) {
return (res.sendStatus || res.send).call(res, 304);
}
//If req.assetFingerprint is already there, do nothing.
if(!req.assetFingerprint)
if(!req.hasOwnProperty(assetFingerprintName))
{
//Create req.assetFingerprint
req.assetFingerprint = assetFingerprint;
req.assetFingerprint = this[assetFingerprintName];
//Expose req.assetFingerprint in Express helper
res.locals.assetFingerprint = function() {
return assetFingerprint.apply(req, arguments);
};
res.locals[assetFingerprintName] = function() {
return this[assetFingerprintName].apply(req, arguments);
}.bind(this);
}
//Run next middleware
next();
};
//req.assetFingerprint function
function assetFingerprint(label, fingerprint, cacheInfo) {
this[assetFingerprintName] = function(label, fingerprint, cacheInfo) {
if(arguments.length > 1)
{
//Add a label
Expand Down Expand Up @@ -118,7 +121,7 @@ function staticAsset(rootPath, strategy) {
}

//Export assetFingerprint on middleware function and return middleware
middleware.assetFingerprint = assetFingerprint;
middleware[assetFingerprintName] = this[assetFingerprintName];
return middleware;
};

Expand Down