Skip to content

Commit

Permalink
cache static files and reduce requests for now.js, also fixes bug on …
Browse files Browse the repository at this point in the history
…safari
  • Loading branch information
ericz committed Mar 15, 2011
1 parent fed4550 commit 332f405
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/helloworld.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>nowjs test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="http://localhost:8080/nowjs/now.js"></script>
<script src="/nowjs/now.js"></script>

<script>
$(document).ready(function(){
Expand Down
3 changes: 0 additions & 3 deletions lib/now.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,3 @@ nowCore.constructRemoteFunction = function(client, functionName){
}
return remoteFn;
}



73 changes: 42 additions & 31 deletions lib/nowServerLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var disconnectedFuncs = [];
var everyone = {nowScope: {}};

var fileCache = {};
var nowFileCache;

everyone.connected = function(func) {
// Instead of using events, we'll just add it to an array of functions that needs to be called
Expand Down Expand Up @@ -72,41 +73,51 @@ exports.initialize = function(server){
}

function serveFile(filename, request, response){
fs.readFile(filename, function(err, data){
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}

var host = request.headers.host.split(":");
var hostServer = host[0]
var hostPort = '80';
if(host.length > 1){
hostPort = host[1];
}

var parsedFile = fileCache[filename];

if(parsedFile == undefined){
parsedFile = data.toString().replace(/\*\*SERVER\*\*/g, hostServer);
parsedFile = parsedFile.replace(/\*\*PORT\*\*/g, hostPort);
fs.readFile(__dirname + "/nowUtil.js", function(err, data){
parsedFile += data.toString();

if(filename in fileCache) {
response.writeHead(200);
response.write(fileCache[filename]);
response.end();
} else {
if(filename.indexOf("/now.js") !== -1) {
if(nowFileCache == undefined) {
fs.readFile(filename, function(err, data){

var host = request.headers.host.split(":");
var hostServer = host[0]
var hostPort = '80';
if(host.length > 1){
hostPort = host[1];
}

var text = data.toString();
text = text.replace(/\*\*SERVER\*\*/g, hostServer);
text = text.replace(/\*\*PORT\*\*/g, hostPort);

fs.readFile(__dirname + "/nowUtil.js", function(err, data){
var textUtil = data.toString();
nowFileCache = {nowUtil: textUtil, now: text};
response.writeHead(200);
response.write(nowFileCache['nowUtil']);
response.write(nowFileCache['now']);
response.end();
});
});
} else {
response.writeHead(200);
response.write(parsedFile);
response.write(nowFileCache['nowUtil']);
response.write(nowFileCache['now']);
response.end();

fileCache[filename] = parsedFile;
});
}
} else {
response.writeHead(200);
response.write(parsedFile);
response.end();
fs.readFile(filename, function(err, data){
var text = data.toString();
response.writeHead(200);
response.write(text);
response.end();
fileCache[filename] = text;
});
}
});
}
}


Expand Down

0 comments on commit 332f405

Please sign in to comment.