Skip to content

Commit 7f14dfd

Browse files
Address PR feedback: host+liveHost only, must-revalidate, resourceTypes, @ts-ignore replaceAll
Made-with: Cursor
1 parent 2b9bbf5 commit 7f14dfd

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

src/extension/auth.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { ADMIN_ORIGIN, ADMIN_ORIGIN_NEW } from './utils/admin.js';
1919
const { host: adminHost } = new URL(ADMIN_ORIGIN);
2020
const { host: newAdminHost } = new URL(ADMIN_ORIGIN_NEW);
2121

22-
/** Cache-Control max-age in seconds for added project hosts (HTML/JSON). 60 = 1 minute. */
22+
/** Cache-Control max-age in seconds for added project hosts (HTML, JSON and code): 1 minute. */
2323
export const CACHE_MAX_AGE_SECONDS = 60;
2424

2525
function getRandomId() {
@@ -39,21 +39,20 @@ export function getHostDomain(host) {
3939
/**
4040
* Builds declarativeNetRequest rules that set Cache-Control on responses
4141
* for added project hosts.
42-
* @param {Object[]} projectConfigs Configs with host, previewHost, liveHost, reviewHost
42+
* @param {Object[]} projectConfigs Configs with host, liveHost
4343
* @returns {Object[]} Rules to add
4444
*/
4545
export function getCacheControlRules(projectConfigs) {
4646
const rules = [];
4747
projectConfigs
4848
.flatMap((p) => [
4949
p?.host,
50-
p?.previewHost,
5150
p?.liveHost,
52-
p?.reviewHost,
5351
].filter(Boolean).map(getHostDomain))
5452
.filter(Boolean)
5553
.filter((domain, i, self) => self.indexOf(domain) === i)
5654
.forEach((domain) => {
55+
// @ts-ignore
5756
const escaped = domain.replaceAll(/\./g, '\\.');
5857
rules.push({
5958
id: getRandomId(),
@@ -63,7 +62,7 @@ export function getCacheControlRules(projectConfigs) {
6362
responseHeaders: [{
6463
header: 'Cache-Control',
6564
operation: 'set',
66-
value: `max-age=${CACHE_MAX_AGE_SECONDS}`,
65+
value: `max-age=${CACHE_MAX_AGE_SECONDS}, must-revalidate`,
6766
}],
6867
},
6968
condition: {
@@ -74,11 +73,8 @@ export function getCacheControlRules(projectConfigs) {
7473
'sub_frame',
7574
'script',
7675
'stylesheet',
77-
'image',
7876
'xmlhttprequest',
79-
'media',
8077
'font',
81-
'other',
8278
],
8379
},
8480
});

test/auth.test.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,20 @@ describe('Test auth', () => {
7777
describe('getCacheControlRules', () => {
7878
it('returns one rule per unique host', () => {
7979
const configs = [
80-
{ host: 'prod.example.com', previewHost: 'preview.example.com' },
80+
{ host: 'prod.example.com' },
8181
{ liveHost: 'live.example.com' },
8282
];
8383
const rules = getCacheControlRules(configs);
84-
expect(rules).to.have.lengthOf(3);
84+
expect(rules).to.have.lengthOf(2);
8585
expect(rules.every((r) => r.action?.responseHeaders?.[0]?.header === 'Cache-Control')).to.be.true;
86-
expect(rules.every((r) => r.action.responseHeaders[0].value === `max-age=${CACHE_MAX_AGE_SECONDS}`)).to.be.true;
86+
expect(rules.every((r) => r.action.responseHeaders[0].value === `max-age=${CACHE_MAX_AGE_SECONDS}, must-revalidate`)).to.be.true;
8787
const filters = rules.map((r) => r.condition.regexFilter);
8888
expect(filters).to.include('^https://prod\\.example\\.com/.*');
89-
expect(filters).to.include('^https://preview\\.example\\.com/.*');
9089
expect(filters).to.include('^https://live\\.example\\.com/.*');
9190
});
9291
it('deduplicates same host across configs', () => {
9392
const configs = [
94-
{ host: 'same.com', previewHost: 'same.com' },
95-
{ liveHost: 'same.com' },
93+
{ host: 'same.com', liveHost: 'same.com' },
9694
];
9795
const rules = getCacheControlRules(configs);
9896
expect(rules).to.have.lengthOf(1);
@@ -106,7 +104,7 @@ describe('Test auth', () => {
106104
});
107105
it('returns empty array for empty or no valid hosts', () => {
108106
expect(getCacheControlRules([])).to.deep.equal([]);
109-
expect(getCacheControlRules([{ host: '' }, { previewHost: null }])).to.deep.equal([]);
107+
expect(getCacheControlRules([{ host: '' }, { liveHost: null }])).to.deep.equal([]);
110108
});
111109
});
112110

0 commit comments

Comments
 (0)