Fix caching for all of the requests. The free wifi I get at Bacchus from Angela's Bridal isn't great.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3981 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-09-14 23:18:55 +00:00
parent f983c3d987
commit 7077e69bf7

View File

@ -415,9 +415,24 @@ async function staticFileHandler(request, response, blobId, uri) {
if (uri === kStaticFiles[i].uri) {
var path = kStaticFiles[i].path || uri.substring(1);
var type = kStaticFiles[i].type || guessType(path);
var data = await File.readFile("core/" + path);
response.writeHead(200, Object.assign({"Content-Type": type, "Content-Length": data.byteLength}, kStaticFiles[i].headers || {}));
let stat = await File.stat('core/' + path);
let id = `${stat.mtime}_${stat.size}`;
if (request.headers['if-none-match'] === '"' + id + '"') {
response.writeHead(304, {});
response.end();
} else {
var data = await File.readFile('core/' + path);
response.writeHead(200, Object.assign(
{
'Content-Type': type,
'Content-Length': data.byteLength,
'etag': '"' + id + '"',
},
kStaticFiles[i].headers || {}));
response.end(data);
}
return;
}
}
@ -443,9 +458,21 @@ async function staticDirectoryHandler(request, response, directory, uri) {
}
try {
let stat = await File.stat(directory + filename);
let id = `${stat.mtime}_${stat.size}`;
if (request.headers['if-none-match'] === '"' + id + '"') {
response.writeHead(304, {});
response.end();
} else {
var data = await File.readFile(directory + filename);
response.writeHead(200, {"Content-Type": k_mime_types[filename.split('.').pop()] || 'text/plain', "Content-Length": data.byteLength});
response.writeHead(200, {
'Content-Type': k_mime_types[filename.split('.').pop()] || 'text/plain',
'Content-Length': data.byteLength,
'etag': '"' + id + '"',
});
response.end(data);
}
} catch {
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": "File not found".length});
response.end("File not found");
@ -513,16 +540,26 @@ function guessType(path) {
}
async function blobHandler(request, response, blobId, uri) {
var found = false;
if (!found) {
for (var i in kStaticFiles) {
if (uri === kStaticFiles[i].uri && kStaticFiles[i].path) {
found = true;
var data = await File.readFile("core/" + kStaticFiles[i].path);
response.writeHead(200, {"Content-Type": kStaticFiles[i].type, "Content-Length": data.byteLength});
let stat = await File.stat('core/' + kStaticFiles[i].path);
let id = `${stat.mtime}_${stat.size}`;
if (request.headers['if-none-match'] === '"' + id + '"') {
response.writeHead(304, {});
response.end();
} else {
var data = await File.readFile('core/' + kStaticFiles[i].path);
response.writeHead(200, Object.assign(
{
'Content-Type': kStaticFiles[i].type,
'Content-Length': data.byteLength,
'etag': '"' + id + '"',
},
kStaticFiles[i].headers || {}));
response.end(data);
break;
}
return;
}
}
@ -532,14 +569,13 @@ async function blobHandler(request, response, blobId, uri) {
return;
}
if (!found) {
var process;
if (uri == "/view") {
var data;
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
var id = await new Database(match[1]).get('path:' + match[2]);
if (id) {
if (request.headers['if-none-match'] && request.headers['if-none-match'] == '"' + id + '"') {
if (request.headers['if-none-match'] === '"' + id + '"') {
response.writeHead(304, {});
response.end();
} else {
@ -551,11 +587,21 @@ async function blobHandler(request, response, blobId, uri) {
sendData(response, data, undefined, {etag: '"' + id + '"'});
}
} else {
sendData(response, data);
if (request.headers['if-none-match'] === '"' + blobId + '"') {
response.writeHead(304, {});
response.end();
} else {
sendData(response, data, undefined, {etag: '"' + blobId + '"'});
}
}
} else {
if (request.headers['if-none-match'] === '"' + blobId + '"') {
response.writeHead(304, {});
response.end();
} else {
data = await getBlobOrContent(blobId);
sendData(response, data);
sendData(response, data, undefined, {etag: '"' + blobId + '"'});
}
}
} else if (uri == "/save") {
let newBlobId = await ssb.blobStore(request.body);
@ -661,7 +707,6 @@ async function blobHandler(request, response, blobId, uri) {
}
}
}
}
ssb.addEventListener('broadcasts', function() {
broadcastEvent('onBroadcastsChanged', []);