From 66d899cbc88bfc0bd63f9946e4ff73c4b946ddae Mon Sep 17 00:00:00 2001 From: mario4tier Date: Tue, 3 Sep 2024 19:56:27 -0400 Subject: [PATCH] Add backend view cache-control --- backend/cache/dist/controllers/view.js | 8 +++++++- backend/cache/src/controllers/view.ts | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/backend/cache/dist/controllers/view.js b/backend/cache/dist/controllers/view.js index e5f7d30..5cc1a19 100644 --- a/backend/cache/dist/controllers/view.js +++ b/backend/cache/dist/controllers/view.js @@ -12,7 +12,7 @@ import { getIdPrefixes } from '../common/strings.js'; // Encoded size (including metadata): 62.0 MiB // Sui object ID: 0x0ebad3b13ee9bc64f8d6370e71d3408a43febaa017a309d2367117afe144ae8c // Cache-Control value initialized once -//const viewCacheControl = process.env.VIEW_CACHE_CONTROL || 'public, max-age=10'; +const viewCacheControl = process.env.VIEW_CACHE_CONTROL || 'public, max-age=10'; export const getView = async (req, res) => { const { id = '' } = req.params; // Request validation @@ -97,6 +97,10 @@ export const getView = async (req, res) => { // Read the metrics file. Can fail but won't throw an error. const metrics = await loadMetricsFile(metricsPath, { verbose: false }); const isValidMetrics = isValidMetricsType(metrics, { verbose: false }); + const headers = { + 'Cache-Control': viewCacheControl, + 'Content-Type': 'text/html', + }; if (!isValidMetrics) { const html = ` @@ -111,6 +115,7 @@ export const getView = async (req, res) => { `; // Send the HTML response + res.set(headers); res.send(html); return; } @@ -234,6 +239,7 @@ export const getView = async (req, res) => { `; // Send the HTML response + res.set(headers); res.send(html); } catch (error) { diff --git a/backend/cache/src/controllers/view.ts b/backend/cache/src/controllers/view.ts index d0545fc..bd56f93 100644 --- a/backend/cache/src/controllers/view.ts +++ b/backend/cache/src/controllers/view.ts @@ -16,7 +16,7 @@ import { getIdPrefixes } from '@/common/strings'; // Sui object ID: 0x0ebad3b13ee9bc64f8d6370e71d3408a43febaa017a309d2367117afe144ae8c // Cache-Control value initialized once -//const viewCacheControl = process.env.VIEW_CACHE_CONTROL || 'public, max-age=10'; +const viewCacheControl = process.env.VIEW_CACHE_CONTROL || 'public, max-age=10'; export const getView = async (req: Request, res: Response) => { const { id = '' } = req.params; @@ -104,6 +104,11 @@ export const getView = async (req: Request, res: Response) => { const metrics = await loadMetricsFile(metricsPath, { verbose: false }); const isValidMetrics = isValidMetricsType(metrics, { verbose: false }); + const headers = { + 'Cache-Control': viewCacheControl, + 'Content-Type': 'text/html', + }; + if (!isValidMetrics) { const html = ` @@ -118,6 +123,7 @@ export const getView = async (req: Request, res: Response) => { `; // Send the HTML response + res.set(headers); res.send(html); return; } @@ -247,6 +253,7 @@ export const getView = async (req: Request, res: Response) => { `; // Send the HTML response + res.set(headers); res.send(html); } catch (error) { console.error('Error reading metrics:', error);