File tree 4 files changed +20
-5
lines changed
4 files changed +20
-5
lines changed Original file line number Diff line number Diff line change @@ -370,6 +370,9 @@ export function defineCachedEventHandler<
370
370
fetch : globalThis . $fetch ,
371
371
} ) ) as $Fetch < unknown , NitroFetchRequest > ;
372
372
event . context = incomingEvent . context ;
373
+ event . context . cache = {
374
+ options : _opts ,
375
+ } ;
373
376
const body = ( await handler ( event ) ) || _resSendBody ;
374
377
375
378
// Collect cachable headers
Original file line number Diff line number Diff line change 1
- import type { CaptureError , CapturedErrorContext } from "../runtime/types" ;
1
+ import type { CacheOptions , CaptureError , CapturedErrorContext } from "../runtime/types" ;
2
2
import type { NitroFetchRequest , $Fetch } from "./fetch" ;
3
3
4
4
export type H3EventFetch = (
@@ -25,6 +25,10 @@ declare module "h3" {
25
25
/** @experimental */
26
26
errors : { error ?: Error ; context : CapturedErrorContext } [ ] ;
27
27
} ;
28
+
29
+ cache : {
30
+ options : CacheOptions ;
31
+ } ;
28
32
}
29
33
}
30
34
Original file line number Diff line number Diff line change 1
- export default defineCachedEventHandler ( ( ) => {
2
- return Date . now ( ) ;
1
+ export default defineCachedEventHandler ( ( event ) => {
2
+ return {
3
+ timestamp : Date . now ( ) ,
4
+ eventContextCache : event . context . cache ,
5
+ } ;
3
6
} ) ;
Original file line number Diff line number Diff line change @@ -643,7 +643,11 @@ export function testNitro(
643
643
it . skipIf ( ctx . isIsolated ) (
644
644
"should setItem before returning response the first time" ,
645
645
async ( ) => {
646
- const { data : timestamp } = await callHandler ( { url : "/api/cached" } ) ;
646
+ const {
647
+ data : { timestamp, eventContextCache } ,
648
+ } = await callHandler ( { url : "/api/cached" } ) ;
649
+
650
+ expect ( eventContextCache ?. options . swr ) . toBe ( true ) ;
647
651
648
652
const calls = await Promise . all ( [
649
653
callHandler ( { url : "/api/cached" } ) ,
@@ -652,7 +656,8 @@ export function testNitro(
652
656
] ) ;
653
657
654
658
for ( const call of calls ) {
655
- expect ( call . data ) . toBe ( timestamp ) ;
659
+ expect ( call . data . timestamp ) . toBe ( timestamp ) ;
660
+ expect ( call . data . eventContextCache . options . swr ) . toBe ( true ) ;
656
661
}
657
662
}
658
663
) ;
You can’t perform that action at this time.
0 commit comments