esa-pcg-cli 0.1.17-beta.astro.8 → 0.1.17-beta.astro.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -8,6 +8,8 @@ const GLOBAL_CACHE_KEY = Symbol.for('pcg.astro.local.route-cache');
|
|
|
8
8
|
const GLOBAL_REFRESH_KEY = Symbol.for('pcg.astro.route-cache.refreshes');
|
|
9
9
|
const GLOBAL_EDGE_CONTEXT_KEY = Symbol.for('pcg.astro.edge-context.als');
|
|
10
10
|
const LOGICAL_TAG_HEADER = 'X-PCG-Astro-Logical-Tags';
|
|
11
|
+
const PRIVATE_CACHE_CONTROL =
|
|
12
|
+
'private, no-cache, no-store, max-age=0, must-revalidate';
|
|
11
13
|
const MAX_TAGS = 64;
|
|
12
14
|
const MAX_TAG_LENGTH = 128;
|
|
13
15
|
const ESA_CACHE_TAG_BATCH_SIZE = 30;
|
|
@@ -217,11 +219,50 @@ function cacheAge(envelope) {
|
|
|
217
219
|
return (Date.now() - envelope.storedAt) / 1000;
|
|
218
220
|
}
|
|
219
221
|
|
|
222
|
+
function setSharedCacheControl(response, maxAge, swr) {
|
|
223
|
+
// Match Next.js' ISR contract: only supply the shared-cache policy when the
|
|
224
|
+
// application did not set Cache-Control itself. In particular, never
|
|
225
|
+
// overwrite private/no-store directives with a public cache policy.
|
|
226
|
+
if (response.headers.has('cache-control')) return response;
|
|
227
|
+
const directives = [`s-maxage=${Math.max(0, maxAge)}`];
|
|
228
|
+
if (swr > 0) directives.push(`stale-while-revalidate=${swr}`);
|
|
229
|
+
response.headers.set('Cache-Control', directives.join(', '));
|
|
230
|
+
return response;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function setEnvelopeCacheControl(response, envelope) {
|
|
234
|
+
const age = Math.max(0, cacheAge(envelope));
|
|
235
|
+
const totalRemaining = Math.max(
|
|
236
|
+
0,
|
|
237
|
+
Math.ceil(envelope.maxAge + envelope.swr - age)
|
|
238
|
+
);
|
|
239
|
+
const freshRemaining = Math.max(0, Math.ceil(envelope.maxAge - age));
|
|
240
|
+
// A stale L2 response is exposed to L1 for at most one fresh second while
|
|
241
|
+
// the provider refreshes it in the background. Deduct that second from SWR
|
|
242
|
+
// so the combined L1 + L2 windows track the policy at whole-second precision.
|
|
243
|
+
const maxAge =
|
|
244
|
+
freshRemaining > 0 ? freshRemaining : Math.min(1, totalRemaining);
|
|
245
|
+
const swr = Math.max(0, totalRemaining - maxAge);
|
|
246
|
+
response.headers.set(
|
|
247
|
+
'CDN-Cache-Control',
|
|
248
|
+
`public, max-age=${maxAge}, stale-while-revalidate=${swr}`
|
|
249
|
+
);
|
|
250
|
+
if (response.headers.has('cache-control')) {
|
|
251
|
+
// An application policy wins, just as it does in Next.js. Propagate the
|
|
252
|
+
// L2 object's age so a downstream shared cache does not restart that
|
|
253
|
+
// policy's full lifetime on every L2 hit.
|
|
254
|
+
response.headers.set('Age', String(Math.floor(age)));
|
|
255
|
+
return response;
|
|
256
|
+
}
|
|
257
|
+
return setSharedCacheControl(response, maxAge, swr);
|
|
258
|
+
}
|
|
259
|
+
|
|
220
260
|
function responseFromEnvelope(envelope, state) {
|
|
221
261
|
const response = new Response(Buffer.from(envelope.body, 'base64'), {
|
|
222
262
|
status: envelope.status,
|
|
223
263
|
headers: envelope.headers,
|
|
224
264
|
});
|
|
265
|
+
setEnvelopeCacheControl(response, envelope);
|
|
225
266
|
response.headers.set('X-Astro-Cache', state);
|
|
226
267
|
return response;
|
|
227
268
|
}
|
|
@@ -238,15 +279,27 @@ function responseIsPrivate(response) {
|
|
|
238
279
|
);
|
|
239
280
|
}
|
|
240
281
|
|
|
241
|
-
function stripSharedCacheHeaders(response) {
|
|
282
|
+
function stripSharedCacheHeaders(response, forcePrivate = false) {
|
|
242
283
|
response.headers.delete(LOGICAL_TAG_HEADER);
|
|
243
284
|
response.headers.delete('Cache-Tag');
|
|
244
285
|
response.headers.delete('CDN-Cache-Control');
|
|
286
|
+
if (
|
|
287
|
+
forcePrivate &&
|
|
288
|
+
!/(?:^|,)\s*(?:private|no-store)(?:\s|,|$)/i.test(
|
|
289
|
+
response.headers.get('cache-control') || ''
|
|
290
|
+
)
|
|
291
|
+
) {
|
|
292
|
+
response.headers.set('Cache-Control', PRIVATE_CACHE_CONTROL);
|
|
293
|
+
}
|
|
245
294
|
return response;
|
|
246
295
|
}
|
|
247
296
|
|
|
248
|
-
async function bypassSharedCache(next) {
|
|
249
|
-
|
|
297
|
+
async function bypassSharedCache(next, forcePrivate = false) {
|
|
298
|
+
const response = await next();
|
|
299
|
+
return stripSharedCacheHeaders(
|
|
300
|
+
response,
|
|
301
|
+
forcePrivate || responseIsPrivate(response)
|
|
302
|
+
);
|
|
250
303
|
}
|
|
251
304
|
|
|
252
305
|
async function serializeResponse(
|
|
@@ -487,7 +540,7 @@ export default function pcgAstroCacheProvider(config = {}) {
|
|
|
487
540
|
return bypassSharedCache(next);
|
|
488
541
|
}
|
|
489
542
|
if (requestIsPrivate(context.request)) {
|
|
490
|
-
return bypassSharedCache(next);
|
|
543
|
+
return bypassSharedCache(next, true);
|
|
491
544
|
}
|
|
492
545
|
const key = primaryCacheKey(context.request);
|
|
493
546
|
const gateway = loadGatewayConfig();
|
|
@@ -506,9 +559,9 @@ export default function pcgAstroCacheProvider(config = {}) {
|
|
|
506
559
|
: await lastInvalidatedAt(gateway, cached.tags || []);
|
|
507
560
|
if (invalidatedAt <= cached.storedAt) {
|
|
508
561
|
const age = cacheAge(cached);
|
|
509
|
-
if (age
|
|
562
|
+
if (age < cached.maxAge)
|
|
510
563
|
return responseFromEnvelope(cached, 'HIT');
|
|
511
|
-
if (age
|
|
564
|
+
if (age < cached.maxAge + cached.swr) {
|
|
512
565
|
const refresh = runRefreshOnce(key, async () => {
|
|
513
566
|
let fresh;
|
|
514
567
|
try {
|
|
@@ -548,8 +601,9 @@ export default function pcgAstroCacheProvider(config = {}) {
|
|
|
548
601
|
const policy = parseCacheControl(
|
|
549
602
|
response.headers.get('CDN-Cache-Control')
|
|
550
603
|
);
|
|
551
|
-
|
|
552
|
-
|
|
604
|
+
const privateResponse = responseIsPrivate(response);
|
|
605
|
+
if (policy.maxAge <= 0 || privateResponse) {
|
|
606
|
+
return stripSharedCacheHeaders(response, privateResponse);
|
|
553
607
|
}
|
|
554
608
|
const tags = responseLogicalTags(response, context.request);
|
|
555
609
|
response.headers.delete(LOGICAL_TAG_HEADER);
|
|
@@ -561,17 +615,17 @@ export default function pcgAstroCacheProvider(config = {}) {
|
|
|
561
615
|
tags,
|
|
562
616
|
maxBytes
|
|
563
617
|
);
|
|
564
|
-
if (envelope)
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
}
|
|
618
|
+
if (!envelope) return stripSharedCacheHeaders(response);
|
|
619
|
+
try {
|
|
620
|
+
if (local) cacheMap().set(key, envelope);
|
|
621
|
+
else await writeRemote(gateway, key, envelope);
|
|
622
|
+
response.headers.set('X-Astro-Cache', 'MISS');
|
|
623
|
+
} catch (error) {
|
|
624
|
+
console.warn(
|
|
625
|
+
`[pcg astro cache] L2 write skipped: ${String(error)}`
|
|
626
|
+
);
|
|
574
627
|
}
|
|
628
|
+
setEnvelopeCacheControl(response, envelope);
|
|
575
629
|
return response;
|
|
576
630
|
}
|
|
577
631
|
);
|