@@ -10,6 +10,7 @@ import type { MiddlewareManifest } from "types/next-types";
1010import type {
1111 InternalEvent ,
1212 InternalResult ,
13+ RoutingResult ,
1314 StreamCreator ,
1415} from "types/open-next.js" ;
1516
@@ -403,10 +404,11 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
403404 * @__PURE__
404405 */
405406export function createServerResponse (
406- internalEvent : InternalEvent ,
407+ routingResult : RoutingResult ,
407408 headers : Record < string , string | string [ ] | undefined > ,
408409 responseStream ?: StreamCreator ,
409410) {
411+ const internalEvent = routingResult . internalEvent ;
410412 return new OpenNextNodeResponse (
411413 ( _headers ) => {
412414 fixCacheHeaderForHtmlPages ( internalEvent , _headers ) ;
@@ -420,8 +422,30 @@ export function createServerResponse(
420422 internalEvent . rawPath ,
421423 _headers ,
422424 ) ;
425+ await invalidateCDNOnRequest ( routingResult , _headers ) ;
423426 } ,
424427 responseStream ,
425428 headers ,
426429 ) ;
427430}
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