I am designing a single page app with a normal html layout like so:
<html>
<head>
<title>...</title>
<link rel="stylesheet" media="all" type="text/css" href="css/main.css">
...more meta tags
</head>
<body>
<!--body generated with templates-->
<script src="js/main.js"></script>
</body>
</html>
On the node.js server:
route.get(req, function(url, type, hdr) {
if (type === 'error') {
//response 404
}else if (type === 'page') {
console.log(url);
fileSys.stat(url, function(err, stat) {
if (err) {
res.statusCode = 404;
res.setHeader("Content-Type","text/plain");
res.write("404 Not Found");
res.end();
}else {
var file,
headers = {},
modif = req.headers['if-modified-since'];
if (modif && new Date(modif).getTime() === stat.mtime.getTime()) { //STATUS 304
headers = {
'Last-Modified': stat.mtime.toUTCString(),
'Date': new Date().toUTCString()
};
res.writeHead(304, headers);
res.end();
}else { //STATUS 200
if (path.extname(url) === '.jpg' || path.extname(url) === '.png')
file = fileSys.readFileSync(url);
else {
if (fileSys.statSync(url).isDirectory())
url += '/index.html';
file = fileSys.readFileSync(url, 'binary');
}
headers = {
'Last-Modified': stat.mtime.toUTCString(),
'Content-Length': stat.size,
'Cache-Control': 'public, max-age=31536000',
'ETag': url+'_'+stat.mtime.getTime(),
'Date': new Date().toUTCString()
};
var contentType = contentExtens[path.extname(url)];
if (contentType)
headers['Content-Type'] = contentType;
res.writeHeader(200, headers);
res.end(file);
}
}
});
}
});
I'm handling the routing without Express and the problem I'm running into is after clearing my Chrome cache, I refresh my index page and server side I'm logging all of the requests which logs the index page, css, js, favicon and all of the images. But when I reload the index page another time, the only request which comes in is for the index page without its resources. And in the browser console everything is 304 and loaded from cache. Is this normal behavior? If I make some changes to the images or any resource files, I can't compare the mtime since those requests are not being sent.
Also I noticed on Safari all of the resources ARE being sent even after setting the cache, last modified and etag headers. Am I not understanding how this should work, is this a browser thing or amd I doing something wrong?
via denikov
No comments:
Post a Comment