Skip to content

Commit

Permalink
Add backend view cache-control
Browse files Browse the repository at this point in the history
  • Loading branch information
mario4tier committed Sep 3, 2024
1 parent 9a32c9e commit 66d899c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 7 additions & 1 deletion backend/cache/dist/controllers/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = `
<!DOCTYPE html>
Expand All @@ -111,6 +115,7 @@ export const getView = async (req, res) => {
</html>
`;
// Send the HTML response
res.set(headers);
res.send(html);
return;
}
Expand Down Expand Up @@ -234,6 +239,7 @@ export const getView = async (req, res) => {
</html>
`;
// Send the HTML response
res.set(headers);
res.send(html);
}
catch (error) {
Expand Down
9 changes: 8 additions & 1 deletion backend/cache/src/controllers/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = `
<!DOCTYPE html>
Expand All @@ -118,6 +123,7 @@ export const getView = async (req: Request, res: Response) => {
</html>
`;
// Send the HTML response
res.set(headers);
res.send(html);
return;
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 66d899c

Please sign in to comment.