From 137e84dfb671c1240a9d2d60ff3c9f22ef86b86c Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Sun, 15 May 2022 15:31:33 +0900 Subject: [PATCH] Fix caching policy Before rendering it, the Mango reader should check the E-Tag of page or it renders wrong image when an image file is moved/removed/reordered --- src/routes/api.cr | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/routes/api.cr b/src/routes/api.cr index 03d6e53..840ce92 100644 --- a/src/routes/api.cr +++ b/src/routes/api.cr @@ -142,8 +142,13 @@ struct APIRouter env.response.status_code = 304 "" else + if entry.is_a? DirectoryEntry + cache_control = "no-cache, max-age=86400" + else + cache_control = "public, max-age=86400" + end env.response.headers["ETag"] = e_tag - env.response.headers["Cache-Control"] = "public, max-age=86400" + env.response.headers["Cache-Control"] = cache_control send_img env, img end rescue e @@ -1138,15 +1143,24 @@ struct APIRouter entry = title.get_entry eid raise "Entry ID `#{eid}` of `#{title.title}` not found" if entry.nil? - file_hash = Digest::SHA1.hexdigest (entry.path + entry.mtime.to_s) + if entry.is_a? DirectoryEntry + file_hash = Digest::SHA1.hexdigest (entry.path + entry.mtime.to_s + entry.size) + else + file_hash = Digest::SHA1.hexdigest (entry.path + entry.mtime.to_s) + end e_tag = "W/#{file_hash}" if e_tag == prev_e_tag env.response.status_code = 304 send_text env, "" else sizes = entry.page_dimensions + if entry.is_a? DirectoryEntry + cache_control = "no-cache, max-age=86400" + else + cache_control = "public, max-age=86400" + end env.response.headers["ETag"] = e_tag - env.response.headers["Cache-Control"] = "public, max-age=86400" + env.response.headers["Cache-Control"] = cache_control send_json env, { "success" => true, "dimensions" => sizes,