Let caller of Site.serve() override caching setting for the request
authorNick Downing <nick@ndcode.org>
Mon, 10 Jan 2022 05:02:30 +0000 (16:02 +1100)
committerNick Downing <nick@ndcode.org>
Mon, 10 Jan 2022 05:02:30 +0000 (16:02 +1100)
Site.js

diff --git a/Site.js b/Site.js
index b47dbfc..052e46c 100644 (file)
--- a/Site.js
+++ b/Site.js
@@ -224,12 +224,12 @@ Site.prototype.kick = async function() {
   this.zip_cache.kick()
 }
 
-Site.prototype.serve_internal = function(response, status, mime_type, data) {
+Site.prototype.serve_internal = function(response, status, mime_type, caching, data) {
   response.statusCode = status
   // html files will be direct recipient of links/bookmarks so can't have
   // a long lifetime, other files like css or images are often large files
   // and won't change frequently (but we'll need cache busting eventually)
-  if (this.options.caching && mime_type !== this.options.mime_type['.html'])
+  if (caching && mime_type !== this.options.mime_type['.html'])
     response.setHeader('Cache-Control', 'max-age=3600')
   response.setHeader('Content-Type', mime_type)
   response.setHeader('Content-Length', data.length)
@@ -240,7 +240,7 @@ Site.prototype.serve = function(env, status, data, from) {
   console.log(
     `${env.parsed_url.host} serving ${env.parsed_url.pathname} size ${data.length} from ${from}`
   )
-  this.serve_internal(env.response, status, env.mime_type, data)
+  this.serve_internal(env.response, status, env.mime_type, env.caching, data)
 }
 
 Site.prototype.die = function(env, message) {
@@ -249,6 +249,7 @@ Site.prototype.die = function(env, message) {
     env.response,
     404,
     this.options.mime_types['.html'],
+    false,
     Buffer.from(
       `<html>
   <head>
@@ -276,6 +277,7 @@ Site.prototype.redirect = function(env, pathname, message) {
     env.response,
     301,
     this.options.mime_types['.html'],
+    false,
     Buffer.from(
       `<html>
   <head>
@@ -665,6 +667,7 @@ Site.prototype.serve_path = async function(env, pathname, components) {
 
 Site.prototype.respond = async function(env) {
   env.mime_type = 'application/octet-stream'
+  env.caching = this.options.caching
   let pathname = decodeURIComponent(env.parsed_url.pathname)
   let i = pathname.lastIndexOf('.')
   if (i !== -1) {