Skip to content

Better 'Cache-Control' syntax #117

@pfumagalli

Description

@pfumagalli

Provide a way to support Cache-Control: no-cache or other custom caching informations. Here's a patch:

--- lib/node-static.js.orig 2014-03-04 14:13:43.000000000 +0900
+++ lib/node-static.js  2014-03-04 14:33:00.000000000 +0900
@@ -15,16 +15,26 @@

     this.root    = path.resolve(root || '.');
     this.options = options || {};
-    this.cache   = 3600;
+    this.cache   = 'max-age=3600';

     this.defaultHeaders  = {};
     this.options.headers = this.options.headers || {};

     if ('cache' in this.options) {
         if (typeof(this.options.cache) === 'number') {
+            if (this.options.cache > 0) {
+                this.cache = 'max-age=' + this.options.cache;
+            } else if (this.options.cache <= 0) {
+                this.cache = 'no-cache';
+            }
+        } else if (typeof(this.options.cache) === 'boolean') {
+            if (! this.options.cache) {
+                this.cache = 'no-cache';
+            }
+        } else if (this.options.cache.toLowerCase() == 'false') {
+            this.cache = 'no-cache';
+        } else if (this.options.cache.toLowerCase() != 'true') {
             this.cache = this.options.cache;
-        } else if (! this.options.cache) {
-            this.cache = false;
         }
     }

@@ -36,9 +46,7 @@

     this.defaultHeaders['server'] = this.serverInfo;

-    if (this.cache !== false) {
-        this.defaultHeaders['cache-control'] = 'max-age=' + this.cache;
-    }
+    this.defaultHeaders['Cache-Control'] = this.cache;

     for (var k in this.defaultHeaders) {
         this.options.headers[k] = this.options.headers[k] ||

Basically this is what it produces:

  • .../static --cache=no-cache produces Cache-Control: no-cache
  • .../static --cache produces Cache-Control: max-age=3600
  • .../static --cache=-1 produces Cache-Control: no-cache
  • .../static --cache=true produces Cache-Control: max-age=3600
  • .../static --cache=false produces Cache-Control: no-cache
  • .../static --cache=1234 produces Cache-Control: max-age=1234
  • .../static --cache=foo-and-bar produces Cache-Control: foo-and-bar

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions