Skip to content
This repository has been archived by the owner on Sep 10, 2020. It is now read-only.

Commit

Permalink
server push header added for http/2.0
Browse files Browse the repository at this point in the history
Though we are using http/1.1 in express/nodejs, we are using CDN that
supports http/2.0, this header will be sent to CDN server.

This commit can not really benifit from that feature since nodejs
doesn't support multiple headers with the same header name yet, see:

 - nodejs/node#3591
 - expressjs/express#2845
  • Loading branch information
PeterDaveHello committed May 18, 2016
1 parent 33a6468 commit 4a7df01
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion webServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,44 @@ function start() {
res.setHeader("Expires", new Date(Date.now() + 60 * 60 * hours * 1000).toUTCString());
}

var serverPush = function(res, uri) {
var temp = uri.split('.'), ext = temp[temp.length-1], as = -1;
switch (ext) {
case 'js':
as='script';
break;
case 'css':
as='style';
break;
case 'png' :
case 'jpg' :
case 'jpeg':
case 'gif' :
case 'ico' :
as='image';
break;
case 'xml' :
as='';
break;
}
delete temp;
if (as != -1) {
res.setHeader("Link", "<" + uri + ">; rel=preload; as=" + as ");
}
delete temp;
delete ext;
delete as;
}

function pushAssets(res) {
serverPush(res, '/css/theme.css');
serverPush(res, '/css/main.css');
serverPush(res, '/js/main.js');
}

app.get('/', function(req, res) {
pushAssets(res);
serverPush(res, '/img/algolia64x20.png');
setCache(res, 2);
res.send(generatePage({
page: {
Expand Down Expand Up @@ -379,7 +415,6 @@ function start() {
app.get('/gitstats', function(req, res) { return res.redirect(301, '/gitstats/cdnjs'); });
app.get('/git_stats', function(req, res) { return res.redirect(301, '/git_stats/cdnjs'); });
app.get('/about', function(req, res) {

setCache(res, 72);
res.send(generatePage({
page: {
Expand Down

0 comments on commit 4a7df01

Please sign in to comment.