@@ -25,10 +25,8 @@ type BodyEncoding = "manual" | "automatic";
2525// Before serving a 404, we check the cache to see if we've served this asset recently
2626// and if so, serve it from the cache instead of responding with a 404.
2727// This gives a bit of a grace period between deployments for any clients browsing the old deployment.
28- export const ASSET_PRESERVATION_CACHE_V1 = "assetPreservationCache" ;
29- // V2 stores the content hash instead of the asset.
30- // TODO: Remove V1 once we've fully migrated to V2
31- export const ASSET_PRESERVATION_CACHE_V2 = "assetPreservationCacheV2" ;
28+ // Only the content hash is actually stored in the body.
29+ export const ASSET_PRESERVATION_CACHE = "assetPreservationCacheV2" ;
3230const CACHE_CONTROL_PRESERVATION = "public, s-maxage=604800" ; // 1 week
3331
3432/** The preservation cache should be periodically
@@ -576,14 +574,14 @@ export async function generateHandler<
576574 waitUntil (
577575 ( async ( ) => {
578576 try {
579- const assetPreservationCacheV2 = await caches . open (
580- ASSET_PRESERVATION_CACHE_V2
577+ const assetPreservationCache = await caches . open (
578+ ASSET_PRESERVATION_CACHE
581579 ) ;
582580
583581 // Check if the asset has changed since last written to cache
584582 // or if the cached entry is getting too old and should have
585583 // it's expiration reset.
586- const match = await assetPreservationCacheV2 . match ( request ) ;
584+ const match = await assetPreservationCache . match ( request ) ;
587585 if (
588586 ! match ||
589587 assetKey !== ( await match . text ( ) ) ||
@@ -599,7 +597,7 @@ export async function generateHandler<
599597 ) ;
600598 preservedResponse . headers . set ( "x-robots-tag" , "noindex" ) ;
601599
602- await assetPreservationCacheV2 . put (
600+ await assetPreservationCache . put (
603601 request . url ,
604602 preservedResponse
605603 ) ;
@@ -637,30 +635,13 @@ export async function generateHandler<
637635 async function notFound ( ) : Promise < Response > {
638636 if ( caches ) {
639637 try {
640- const assetPreservationCacheV2 = await caches . open (
641- ASSET_PRESERVATION_CACHE_V2
638+ const assetPreservationCache = await caches . open (
639+ ASSET_PRESERVATION_CACHE
642640 ) ;
643- let preservedResponse = await assetPreservationCacheV2 . match (
641+ const preservedResponse = await assetPreservationCache . match (
644642 request . url
645643 ) ;
646644
647- // Continue serving from V1 preservation cache for some time to
648- // prevent 404s during the migration to V2
649- const cutoffDate = new Date ( "2024-05-17" ) ;
650- if ( ! preservedResponse && Date . now ( ) < cutoffDate . getTime ( ) ) {
651- const assetPreservationCacheV1 = await caches . open (
652- ASSET_PRESERVATION_CACHE_V1
653- ) ;
654- preservedResponse = await assetPreservationCacheV1 . match ( request . url ) ;
655- if ( preservedResponse ) {
656- // V1 cache contains full response bodies so we return it directly
657- if ( setMetrics ) {
658- setMetrics ( { preservationCacheResult : "checked-hit" } ) ;
659- }
660- return preservedResponse ;
661- }
662- }
663-
664645 // V2 cache only contains the asset key, rather than the asset body:
665646 if ( preservedResponse ) {
666647 if ( setMetrics ) {
@@ -674,6 +655,7 @@ export async function generateHandler<
674655 }
675656 if ( assetKey ) {
676657 const asset = await fetchAsset ( assetKey ) ;
658+
677659 if ( asset ) {
678660 // We know the asset hasn't changed, so use the cached headers.
679661 return new Response ( asset . body , preservedResponse ) ;
0 commit comments