Skip to content

Commit 853ac55

Browse files
Address PR feedback: map+flat chain, remove dedupe, resourceTypes, drop hlxSidekickProjects
1 parent 0650cc6 commit 853ac55

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

src/extension/auth.js

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,46 @@ export function getHostDomain(host) {
4242
* @returns {Object[]} Rules to add
4343
*/
4444
export function getCacheControlRules(projectConfigs) {
45-
const seen = new Set();
4645
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-
},
46+
projectConfigs
47+
.map((p) => [
48+
p?.host,
49+
p?.previewHost,
50+
p?.liveHost,
51+
p?.reviewHost,
52+
].filter(Boolean).map(getHostDomain))
53+
.flat()
54+
.filter(Boolean)
55+
.forEach((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: [
72+
'main_frame',
73+
'sub_frame',
74+
'script',
75+
'stylesheet',
76+
'image',
77+
'xmlhttprequest',
78+
'media',
79+
'font',
80+
'other',
81+
],
82+
},
83+
});
7384
});
74-
});
7585
return rules;
7686
}
7787

@@ -91,8 +101,7 @@ export async function configureAuthAndCorsHeaders() {
91101
const allRules = [];
92102

93103
// 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') || [];
104+
const syncHandles = await getConfig('sync', 'projects') || [];
96105
const syncConfigs = (await Promise.all(
97106
syncHandles.map((handle) => getConfig('sync', handle)),
98107
)).filter(Boolean);

test/auth.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ describe('Test auth', () => {
8989
expect(filters).to.include('^https://preview\\.example\\.com/.*');
9090
expect(filters).to.include('^https://live\\.example\\.com/.*');
9191
});
92-
it('deduplicates same host across configs', () => {
92+
it('adds one rule per host occurrence (no deduplication)', () => {
9393
const configs = [
9494
{ host: 'same.com', previewHost: 'same.com' },
9595
{ liveHost: 'same.com' },
9696
];
9797
const rules = getCacheControlRules(configs);
98-
expect(rules).to.have.lengthOf(1);
99-
expect(rules[0].condition.regexFilter).to.equal('^https://same\\.com/.*');
98+
expect(rules).to.have.lengthOf(3);
99+
expect(rules.every((r) => r.condition.regexFilter === '^https://same\\.com/.*')).to.be.true;
100100
});
101101
it('extracts host from full URL in config', () => {
102102
const configs = [{ host: 'https://url-host.com/path' }];

0 commit comments

Comments
 (0)