There are about 10 files in a directory called "assets" which are required by by various img tags in the main layout on the browser.
10 separate requests are sent to get the separate tiny image files. (eventually i will optimise this to just send one request to load a single image sheet consisting of all images in one)
But its sometimes unable to load all of them, so the browser shows the spinning/loading icon for ages till it timesout or it loads them but they are a wrong order/match (img tag will display a wrong image) or it just doesnt load the image, its a different behaviour everytime i refresh the page
//first request gzips and loads the main layout, subsequent requests loads images
require('fs').readFile('assets/'+params.file, function(err, data) {
if(err) throw err;
if(params.file == 'layout.html') {
require('zlib').gzip(data, function(err, buffer) {
if(err) throw err;
output(buffer, params);
});
} else output(data, params);
});
function output(data, params){
switch(params.file.substring(params.file.length-3)) {
case 'tml': params.headers = {'Content-Encoding': 'gzip'};break;
case 'ico': params.headers = {'Content-Type': 'image/x-icon'};break;
case 'ttf': params.headers = {'Content-Type': 'application/x-font-ttf' }; break;
case 'otf': params.headers = {'Content-Type':'font/opentype'};break;
case 'png': params.headers = {'Content-Type':'image/png'};break;
case 'gif': params.headers = {'Content-Type':'image/gif'};break;
case 'peg':
case 'jpg': params.headers = {'Content-Type':'image/jpeg'};break;
default: params.headers = {};
}
params.headers['Set-Cookie'] = '';
params.responseOutput(data, params);
}
this feels a little like those pesky "closure" problems, but there are no forloops here and each image request from the browser should not interfere with another (..and yet it is)
The fact that some requests sometimes serve the wrong file suggests that you do have a closure issue, most likely involving params
getting changed before either the readfile
or gzip
callbacks execute. What's its scope and how do you construct it (another possibility is that params
itself is properly scoped, but it contains references to something that isn't)?