@@ -18,10 +18,63 @@ import { ADMIN_ORIGIN } from './utils/admin.js';
1818
1919const { 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+
2124function 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 ) {
0 commit comments