@@ -435,11 +435,27 @@ describe("asset-server handler", () => {
435
435
436
436
const findAssetEntryForPath = async ( path : string ) => {
437
437
if ( path === "/index.html" ) {
438
- return "index.html" ;
438
+ return "asset-key- index.html" ;
439
439
}
440
440
441
441
return null ;
442
442
} ;
443
+ const fetchAsset = ( ) =>
444
+ Promise . resolve (
445
+ Object . assign (
446
+ new Response ( `
447
+ <!DOCTYPE html>
448
+ <html>
449
+ <body>
450
+ <link rel="preload" as="image" href="/a.png" />
451
+ <link rel="preload" as="image" href="/b.png" />
452
+ <link rel="modulepreload" href="lib.js" />
453
+ <link rel="preconnect" href="cloudflare.com" />
454
+ </body>
455
+ </html>` ) ,
456
+ { contentType : "text/html" }
457
+ )
458
+ ) ;
443
459
444
460
// Create cache storage to reuse between requests
445
461
const { caches } = createCacheStorage ( ) ;
@@ -450,22 +466,7 @@ describe("asset-server handler", () => {
450
466
metadata,
451
467
findAssetEntryForPath,
452
468
caches,
453
- fetchAsset : ( ) =>
454
- Promise . resolve (
455
- Object . assign (
456
- new Response ( `
457
- <!DOCTYPE html>
458
- <html>
459
- <body>
460
- <link rel="preload" as="image" href="/a.png" />
461
- <link rel="preload" as="image" href="/b.png" />
462
- <link rel="modulepreload" href="lib.js" />
463
- <link rel="preconnect" href="cloudflare.com" />
464
- </body>
465
- </html>` ) ,
466
- { contentType : "text/html" }
467
- )
468
- ) ,
469
+ fetchAsset,
469
470
} ) ;
470
471
471
472
const { response, spies } = await getResponse ( ) ;
@@ -476,17 +477,20 @@ describe("asset-server handler", () => {
476
477
await Promise . all ( spies . waitUntil ) ;
477
478
478
479
const earlyHintsCache = await caches . open ( `eh:${ deploymentId } ` ) ;
479
- const earlyHintsRes = await earlyHintsCache . match ( "https://example.com/" ) ;
480
+ const earlyHintsRes = await earlyHintsCache . match (
481
+ "https://example.com/asset-key-index.html"
482
+ ) ;
480
483
481
484
if ( ! earlyHintsRes ) {
482
485
throw new Error (
483
- "Did not match early hints cache on https://example.com/"
486
+ "Did not match early hints cache on https://example.com/asset-key-index.html "
484
487
) ;
485
488
}
486
489
487
490
expect ( earlyHintsRes . headers . get ( "link" ) ) . toMatchInlineSnapshot (
488
491
`"</a.png>; rel="preload"; as=image, </b.png>; rel="preload"; as=image, <lib.js>; rel="modulepreload", <cloudflare.com>; rel="preconnect""`
489
492
) ;
493
+ expect ( response . headers . get ( "link" ) ) . toBeNull ( ) ;
490
494
491
495
// Do it again, but this time ensure that we didn't write to cache again
492
496
const { response : response2 , spies : spies2 } = await getResponse ( ) ;
@@ -497,17 +501,54 @@ describe("asset-server handler", () => {
497
501
498
502
await Promise . all ( spies2 . waitUntil ) ;
499
503
500
- const earlyHintsRes2 = await earlyHintsCache . match ( "https://example.com/" ) ;
504
+ const earlyHintsRes2 = await earlyHintsCache . match (
505
+ "https://example.com/asset-key-index.html"
506
+ ) ;
501
507
502
508
if ( ! earlyHintsRes2 ) {
503
509
throw new Error (
504
- "Did not match early hints cache on https://example.com/"
510
+ "Did not match early hints cache on https://example.com/asset-key-index.html "
505
511
) ;
506
512
}
507
513
508
514
expect ( earlyHintsRes2 . headers . get ( "link" ) ) . toMatchInlineSnapshot (
509
515
`"</a.png>; rel="preload"; as=image, </b.png>; rel="preload"; as=image, <lib.js>; rel="modulepreload", <cloudflare.com>; rel="preconnect""`
510
516
) ;
517
+ expect ( response2 . headers . get ( "link" ) ) . toMatchInlineSnapshot (
518
+ `"</a.png>; rel="preload"; as=image, </b.png>; rel="preload"; as=image, <lib.js>; rel="modulepreload", <cloudflare.com>; rel="preconnect""`
519
+ ) ;
520
+
521
+ // Now make sure that requests for other paths which resolve to the same asset share the EH cache result
522
+ const { response : response3 , spies : spies3 } = await getTestResponse ( {
523
+ request : new Request ( "https://example.com/foo" ) ,
524
+ metadata,
525
+ findAssetEntryForPath,
526
+ caches,
527
+ fetchAsset,
528
+ } ) ;
529
+
530
+ expect ( response3 . status ) . toBe ( 200 ) ;
531
+ // waitUntil should not be called at all (SPA)
532
+ expect ( spies3 . waitUntil . length ) . toBe ( 0 ) ;
533
+
534
+ await Promise . all ( spies3 . waitUntil ) ;
535
+
536
+ const earlyHintsRes3 = await earlyHintsCache . match (
537
+ "https://example.com/asset-key-index.html"
538
+ ) ;
539
+
540
+ if ( ! earlyHintsRes3 ) {
541
+ throw new Error (
542
+ "Did not match early hints cache on https://example.com/asset-key-index.html"
543
+ ) ;
544
+ }
545
+
546
+ expect ( earlyHintsRes3 . headers . get ( "link" ) ) . toMatchInlineSnapshot (
547
+ `"</a.png>; rel="preload"; as=image, </b.png>; rel="preload"; as=image, <lib.js>; rel="modulepreload", <cloudflare.com>; rel="preconnect""`
548
+ ) ;
549
+ expect ( response3 . headers . get ( "link" ) ) . toMatchInlineSnapshot (
550
+ `"</a.png>; rel="preload"; as=image, </b.png>; rel="preload"; as=image, <lib.js>; rel="modulepreload", <cloudflare.com>; rel="preconnect""`
551
+ ) ;
511
552
} ) ;
512
553
513
554
test ( "early hints should cache empty link headers" , async ( ) => {
@@ -516,7 +557,7 @@ describe("asset-server handler", () => {
516
557
517
558
const findAssetEntryForPath = async ( path : string ) => {
518
559
if ( path === "/index.html" ) {
519
- return "index.html" ;
560
+ return "asset-key- index.html" ;
520
561
}
521
562
522
563
return null ;
@@ -554,15 +595,18 @@ describe("asset-server handler", () => {
554
595
await Promise . all ( spies . waitUntil ) ;
555
596
556
597
const earlyHintsCache = await caches . open ( `eh:${ deploymentId } ` ) ;
557
- const earlyHintsRes = await earlyHintsCache . match ( "https://example.com/" ) ;
598
+ const earlyHintsRes = await earlyHintsCache . match (
599
+ "https://example.com/asset-key-index.html"
600
+ ) ;
558
601
559
602
if ( ! earlyHintsRes ) {
560
603
throw new Error (
561
- "Did not match early hints cache on https://example.com/"
604
+ "Did not match early hints cache on https://example.com/asset-key-index.html "
562
605
) ;
563
606
}
564
607
565
608
expect ( earlyHintsRes . headers . get ( "link" ) ) . toBeNull ( ) ;
609
+ expect ( response . headers . get ( "link" ) ) . toBeNull ( ) ;
566
610
567
611
// Do it again, but this time ensure that we didn't write to cache again
568
612
const { response : response2 , spies : spies2 } = await getResponse ( ) ;
@@ -573,15 +617,18 @@ describe("asset-server handler", () => {
573
617
574
618
await Promise . all ( spies2 . waitUntil ) ;
575
619
576
- const earlyHintsRes2 = await earlyHintsCache . match ( "https://example.com/" ) ;
620
+ const earlyHintsRes2 = await earlyHintsCache . match (
621
+ "https://example.com/asset-key-index.html"
622
+ ) ;
577
623
578
624
if ( ! earlyHintsRes2 ) {
579
625
throw new Error (
580
- "Did not match early hints cache on https://example.com/"
626
+ "Did not match early hints cache on https://example.com/asset-key-index.html "
581
627
) ;
582
628
}
583
629
584
630
expect ( earlyHintsRes2 . headers . get ( "link" ) ) . toBeNull ( ) ;
631
+ expect ( response2 . headers . get ( "link" ) ) . toBeNull ( ) ;
585
632
} ) ;
586
633
587
634
test . todo (
0 commit comments