From cd6bfd23eabbd2ae881d01ada997d5ff675a9e6f Mon Sep 17 00:00:00 2001 From: Thaddee Tyl Date: Sat, 4 Jan 2014 20:36:52 +0100 Subject: [PATCH] Cache images: max-age + last-modified. --- server.js | 9 +++++++++ web/index.html | 26 +++++++++++++------------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/server.js b/server.js index 4391d42..ad42928 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ var camp = require('camp').start({ port: process.env.PORT||+process.argv[2]||80 }); var badge = require('./badge.js'); +var serverStartTime = new Date((new Date()).toGMTString()); // Escapes `t` using the format specified in // @@ -23,6 +24,14 @@ camp.route(/^\/(([^-]|--)+)-(([^-]|--)+)-(([^-]|--)+).svg$/, var status = escapeFormat(match[3]); var color = escapeFormat(match[5]); ask.res.setHeader('Content-Type', 'image/svg+xml'); + var cacheDuration = (3600*24*1)|0; // 1 day. + ask.res.setHeader('Cache-Control', 'public, max-age=' + cacheDuration); + if (+(new Date(ask.req.headers['if-modified-since'])) >= +serverStartTime) { + ask.res.statusCode = 304; + ask.res.end(); // not modified. + return; + } + ask.res.setHeader('Last-Modified', serverStartTime.toGMTString()); try { var badgeData = {text: [subject, status]}; if (sixHex(color)) { diff --git a/web/index.html b/web/index.html index b9e8843..ba82b96 100644 --- a/web/index.html +++ b/web/index.html @@ -12,8 +12,8 @@ a.photo>img { padding: 2px; border: 1px solid grey; } ul { text-align: left; margin-left: 25%; } - - + +

Ever wanted to have your own GitHub badges?
@@ -24,7 +24,7 @@ Maybe you simply love SVG.
Maybe you fancy a format that uses less than half the bandwidth.

- +

As for me, I wanted a format that people can reuse and hack on.
@@ -32,7 +32,7 @@ I wanted a service for everyone.
I made the GitHub Badge Service.

- +

Here's how it works.
@@ -43,15 +43,15 @@ Use an underscore _ if you want a space. Or, you know, use a space.

Colors:

- - - - - - - - - + + + + + + + + +

Like This?