@@ -10,6 +10,7 @@ import type { MiddlewareManifest } from "types/next-types";
10
10
import type {
11
11
InternalEvent ,
12
12
InternalResult ,
13
+ RoutingResult ,
13
14
StreamCreator ,
14
15
} from "types/open-next.js" ;
15
16
@@ -403,10 +404,11 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
403
404
* @__PURE__
404
405
*/
405
406
export function createServerResponse (
406
- internalEvent : InternalEvent ,
407
+ routingResult : RoutingResult ,
407
408
headers : Record < string , string | string [ ] | undefined > ,
408
409
responseStream ?: StreamCreator ,
409
410
) {
411
+ const internalEvent = routingResult . internalEvent ;
410
412
return new OpenNextNodeResponse (
411
413
( _headers ) => {
412
414
fixCacheHeaderForHtmlPages ( internalEvent , _headers ) ;
@@ -420,8 +422,30 @@ export function createServerResponse(
420
422
internalEvent . rawPath ,
421
423
_headers ,
422
424
) ;
425
+ await invalidateCDNOnRequest ( routingResult , _headers ) ;
423
426
} ,
424
427
responseStream ,
425
428
headers ,
426
429
) ;
427
430
}
431
+
432
+ // This function is used only for `res.revalidate()`
433
+ export async function invalidateCDNOnRequest (
434
+ params : RoutingResult ,
435
+ headers : OutgoingHttpHeaders ,
436
+ ) {
437
+ const { internalEvent, initialPath, resolvedRoutes } = params ;
438
+ const isIsrRevalidation = internalEvent . headers [ "x-isr" ] === "1" ;
439
+ if (
440
+ ! isIsrRevalidation &&
441
+ headers [ CommonHeaders . NEXT_CACHE ] === "REVALIDATED"
442
+ ) {
443
+ await globalThis . cdnInvalidationHandler . invalidatePaths ( [
444
+ {
445
+ initialPath,
446
+ rawPath : internalEvent . rawPath ,
447
+ resolvedRoutes,
448
+ } ,
449
+ ] ) ;
450
+ }
451
+ }
0 commit comments