Skip to content

Commit 5f50df1

Browse files
Address PR feedback: use flatMap, dedupe hosts with Set, replaceAll for escaping
1 parent 853ac55 commit 5f50df1

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

src/extension/auth.js

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,45 +43,47 @@ export function getHostDomain(host) {
4343
*/
4444
export function getCacheControlRules(projectConfigs) {
4545
const rules = [];
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-
});
46+
const hosts = [
47+
...new Set(
48+
projectConfigs
49+
.flatMap((p) => [
50+
p?.host,
51+
p?.previewHost,
52+
p?.liveHost,
53+
p?.reviewHost,
54+
].filter(Boolean).map(getHostDomain)),
55+
),
56+
].filter(Boolean);
57+
hosts.forEach((domain) => {
58+
const escaped = domain.replaceAll(/\./g, '\\.');
59+
rules.push({
60+
id: getRandomId(),
61+
priority: 1,
62+
action: {
63+
type: 'modifyHeaders',
64+
responseHeaders: [{
65+
header: 'Cache-Control',
66+
operation: 'set',
67+
value: `max-age=${CACHE_MAX_AGE_SECONDS}`,
68+
}],
69+
},
70+
condition: {
71+
regexFilter: `^https://${escaped}/.*`,
72+
requestMethods: ['get'],
73+
resourceTypes: [
74+
'main_frame',
75+
'sub_frame',
76+
'script',
77+
'stylesheet',
78+
'image',
79+
'xmlhttprequest',
80+
'media',
81+
'font',
82+
'other',
83+
],
84+
},
8485
});
86+
});
8587
return rules;
8688
}
8789

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('adds one rule per host occurrence (no deduplication)', () => {
92+
it('deduplicates same host across configs', () => {
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(3);
99-
expect(rules.every((r) => r.condition.regexFilter === '^https://same\\.com/.*')).to.be.true;
98+
expect(rules).to.have.lengthOf(1);
99+
expect(rules[0].condition.regexFilter).to.equal('^https://same\\.com/.*');
100100
});
101101
it('extracts host from full URL in config', () => {
102102
const configs = [{ host: 'https://url-host.com/path' }];

0 commit comments

Comments
 (0)