Skip to content

Commit 528134e

Browse files
feat: suppress caching for added project hosts via Cache-Control override
- Add declarativeNetRequest rules that set response header Cache-Control: max-age=60 for all added project hosts (prod, preview, live, review) - Overrides server Cache-Control for HTML (main_frame, sub_frame) and JSON (xmlhttprequest) to 1 minute - Re-apply header config when project is added or updated
1 parent 277b5cd commit 528134e

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

src/extension/auth.js

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,63 @@ import { ADMIN_ORIGIN } from './utils/admin.js';
1818

1919
const { host: adminHost } = new URL(ADMIN_ORIGIN);
2020

21+
/** Cache-Control max-age in seconds for added project hosts (HTML/JSON). 60 = 1 minute. */
22+
export const CACHE_MAX_AGE_SECONDS = 60;
23+
2124
function getRandomId() {
2225
return Math.floor(Math.random() * 1000000);
2326
}
2427

28+
/**
29+
* Returns the hostname (domain) from a project host value (may be domain or full URL).
30+
* @param {string} host
31+
* @returns {string}
32+
*/
33+
function getHostDomain(host) {
34+
if (!host || typeof host !== 'string') return '';
35+
return host.startsWith('http') ? new URL(host).host : host;
36+
}
37+
38+
/**
39+
* Builds declarativeNetRequest rules that set Cache-Control on responses
40+
* for added project hosts.
41+
* @param {Object[]} projectConfigs Configs with host, previewHost, liveHost, reviewHost
42+
* @returns {Object[]} Rules to add
43+
*/
44+
function getCacheControlRules(projectConfigs) {
45+
const seen = new Set();
46+
const rules = [];
47+
const hosts = projectConfigs.flatMap((p) => [
48+
p?.host,
49+
p?.previewHost,
50+
p?.liveHost,
51+
p?.reviewHost,
52+
].filter(Boolean).map(getHostDomain)).filter(Boolean);
53+
hosts.forEach((domain) => {
54+
if (seen.has(domain)) return;
55+
seen.add(domain);
56+
const escaped = domain.replace(/\./g, '\\.');
57+
rules.push({
58+
id: getRandomId(),
59+
priority: 1,
60+
action: {
61+
type: 'modifyHeaders',
62+
responseHeaders: [{
63+
header: 'Cache-Control',
64+
operation: 'set',
65+
value: `max-age=${CACHE_MAX_AGE_SECONDS}`,
66+
}],
67+
},
68+
condition: {
69+
regexFilter: `^https://${escaped}/.*`,
70+
requestMethods: ['get'],
71+
resourceTypes: ['main_frame', 'sub_frame', 'xmlhttprequest', 'other'],
72+
},
73+
});
74+
});
75+
return rules;
76+
}
77+
2578
/**
2679
* Sets the x-auth-token header for all requests to the Admin API if project config
2780
* has an auth token. Also sets the Access-Control-Allow-Origin header for
@@ -35,6 +88,16 @@ export async function configureAuthAndCorsHeaders() {
3588
removeRuleIds: (await chrome.declarativeNetRequest.getSessionRules())
3689
.map((rule) => rule.id),
3790
});
91+
const allRules = [];
92+
93+
// Cache-Control rules for added project hosts (prod, preview, live, review) – override to 1 min
94+
const syncHandles = await getConfig('sync', 'projects')
95+
|| await getConfig('sync', 'hlxSidekickProjects') || [];
96+
const syncConfigs = (await Promise.all(
97+
syncHandles.map((handle) => getConfig('sync', handle)),
98+
)).filter(Boolean);
99+
allRules.push(...getCacheControlRules(syncConfigs));
100+
38101
// find projects with auth tokens and add rules for each
39102
const projects = await getConfig('session', 'projects') || [];
40103
const addRulesPromises = projects.map(async ({
@@ -130,10 +193,10 @@ export async function configureAuthAndCorsHeaders() {
130193
return rules;
131194
});
132195

133-
const addRules = (await Promise.all(addRulesPromises)).flat();
134-
if (addRules.length > 0) {
196+
allRules.push(...(await Promise.all(addRulesPromises)).flat());
197+
if (allRules.length > 0) {
135198
await chrome.declarativeNetRequest.updateSessionRules({
136-
addRules,
199+
addRules: allRules,
137200
});
138201
}
139202
} catch (e) {

src/extension/project.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from './config.js';
1919
import { urlCache } from './url-cache.js';
2020
import { callAdmin, createAdminUrl } from './utils/admin.js';
21-
import { setAuthToken } from './auth.js';
21+
import { configureAuthAndCorsHeaders, setAuthToken } from './auth.js';
2222

2323
export const DEV_URL = 'http://localhost:3000/';
2424

@@ -78,6 +78,7 @@ export async function updateProject(project) {
7878
projects.push(handle);
7979
await setConfig('sync', { projects });
8080
}
81+
await configureAuthAndCorsHeaders();
8182
log.info('updated project', project);
8283
return project;
8384
}

0 commit comments

Comments
 (0)