Skip to content

Commit fcc2851

Browse files
committed
update to use initialPath when needed
1 parent 55123b9 commit fcc2851

File tree

6 files changed

+89
-44
lines changed

6 files changed

+89
-44
lines changed

packages/open-next/src/adapters/cache.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,15 @@ export default class Cache {
447447
if (uniquePaths.length > 0) {
448448
await globalThis.cdnInvalidationHandler.invalidatePaths(
449449
uniquePaths.map((path) => ({
450-
path,
451-
// Here we can be sure that the path is from app router
452-
isAppRouter: true,
450+
initialPath: path,
451+
rawPath: path,
452+
resolvedRoutes: [
453+
{
454+
route: path,
455+
// TODO: ideally here we should check if it's an app router page or route
456+
type: "app",
457+
},
458+
],
453459
})),
454460
);
455461
}

packages/open-next/src/core/requestHandler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,14 @@ export async function openNextHandler(
118118
// response is used only in the streaming case
119119
if (responseStreaming) {
120120
const response = createServerResponse(
121-
internalEvent,
121+
{
122+
internalEvent,
123+
isExternalRewrite: false,
124+
isISR: false,
125+
resolvedRoutes: [],
126+
origin: false,
127+
initialPath: internalEvent.rawPath,
128+
},
122129
headers,
123130
responseStreaming,
124131
);
@@ -162,7 +169,7 @@ export async function openNextHandler(
162169

163170
const req = new IncomingMessage(reqProps);
164171
const res = createServerResponse(
165-
preprocessedEvent,
172+
routingResult,
166173
overwrittenResponseHeaders,
167174
responseStreaming,
168175
);

packages/open-next/src/core/routing/util.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { OpenNextNodeResponse } from "http/openNextResponse.js";
88
import { parseHeaders } from "http/util.js";
99
import type { MiddlewareManifest } from "types/next-types";
1010
import type {
11-
InternalEvent,
1211
InternalResult,
12+
RoutingResult,
1313
StreamCreator,
1414
} from "types/open-next.js";
1515

@@ -403,10 +403,11 @@ export function fixISRHeaders(headers: OutgoingHttpHeaders) {
403403
* @__PURE__
404404
*/
405405
export function createServerResponse(
406-
internalEvent: InternalEvent,
406+
routingResult: RoutingResult,
407407
headers: Record<string, string | string[] | undefined>,
408408
responseStream?: StreamCreator,
409409
) {
410+
const internalEvent = routingResult.internalEvent;
410411
return new OpenNextNodeResponse(
411412
(_headers) => {
412413
fixCacheHeaderForHtmlPages(internalEvent, _headers);
@@ -420,34 +421,29 @@ export function createServerResponse(
420421
internalEvent.rawPath,
421422
_headers,
422423
);
423-
await invalidateCDNOnRequest({
424-
rawPath: internalEvent.rawPath,
425-
isIsrRevalidation: internalEvent.headers["x-isr"] === "1",
426-
headers: _headers,
427-
});
424+
await invalidateCDNOnRequest(routingResult, _headers);
428425
},
429426
responseStream,
430427
headers,
431428
);
432429
}
433430

434431
// This function is used only for `res.revalidate()`
435-
export async function invalidateCDNOnRequest(params: {
436-
//TODO: use the initialPath instead of rawPath, a rewrite could have happened and would make cdn invalidation fail
437-
rawPath: string;
438-
isIsrRevalidation?: boolean;
439-
headers: OutgoingHttpHeaders;
440-
}) {
441-
const { rawPath, isIsrRevalidation, headers } = params;
432+
export async function invalidateCDNOnRequest(
433+
params: RoutingResult,
434+
headers: OutgoingHttpHeaders,
435+
) {
436+
const { internalEvent, initialPath, resolvedRoutes } = params;
437+
const isIsrRevalidation = internalEvent.headers["x-isr"] === "1";
442438
if (
443439
!isIsrRevalidation &&
444440
headers[CommonHeaders.NEXT_CACHE] === "REVALIDATED"
445441
) {
446442
await globalThis.cdnInvalidationHandler.invalidatePaths([
447443
{
448-
path: rawPath,
449-
//TODO: Here we assume that the path is for page router, this might not be the case
450-
isAppRouter: false,
444+
initialPath,
445+
rawPath: internalEvent.rawPath,
446+
resolvedRoutes,
451447
},
452448
]);
453449
}

packages/open-next/src/overrides/cdnInvalidation/cloudfront.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ const cloudfront = new CloudFrontClient({});
88
export default {
99
name: "cloudfront",
1010
invalidatePaths: async (paths) => {
11-
//TODO: test the constructed paths
12-
const constructedPaths = paths.flatMap(({ path, isAppRouter }) =>
13-
isAppRouter
14-
? [`${path}`, `${path}?_rsc=*`]
15-
: [
16-
`${path}`,
17-
`/_next/data/${process.env.NEXT_BUILD_ID}${path === "/" ? "/index" : path}.json*`,
18-
],
11+
const constructedPaths = paths.flatMap(
12+
({ initialPath, resolvedRoutes }) => {
13+
const isAppRouter = resolvedRoutes.some(
14+
(route) => route.type === "app",
15+
);
16+
// revalidateTag doesn't have any leading slash, remove it just to be sure
17+
const path = initialPath.replace(/^\//, "");
18+
return isAppRouter
19+
? [`/${path}`, `/${path}?_rsc=*`]
20+
: [
21+
`/${path}`,
22+
`/_next/data/${process.env.NEXT_BUILD_ID}${path === "/" ? "/index" : `/${path}`}.json*`,
23+
];
24+
},
1925
);
2026
await cloudfront.send(
2127
new CreateInvalidationCommand({

packages/open-next/src/types/overrides.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
InternalEvent,
99
InternalResult,
1010
Origin,
11+
ResolvedRoute,
1112
StreamCreator,
1213
} from "./open-next";
1314

@@ -141,8 +142,9 @@ export type ProxyExternalRequest = BaseOverride & {
141142
};
142143

143144
type CDNPath = {
144-
path: string;
145-
isAppRouter: boolean;
145+
initialPath: string;
146+
rawPath: string;
147+
resolvedRoutes: ResolvedRoute[];
146148
};
147149

148150
export type CDNInvalidationHandler = BaseOverride & {

packages/tests-unit/tests/core/routing/util.test.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,14 @@ describe("invalidateCDNOnRequest", () => {
747747
const headers: Record<string, string> = {
748748
"x-nextjs-cache": "HIT",
749749
};
750-
await invalidateCDNOnRequest({
751-
rawPath: "/path",
750+
await invalidateCDNOnRequest(
751+
{
752+
internalEvent: {
753+
headers: {},
754+
},
755+
},
752756
headers,
753-
});
757+
);
754758

755759
expect(
756760
globalThis.cdnInvalidationHandler.invalidatePaths,
@@ -761,11 +765,17 @@ describe("invalidateCDNOnRequest", () => {
761765
const headers: Record<string, string> = {
762766
"x-nextjs-cache": "REVALIDATED",
763767
};
764-
await invalidateCDNOnRequest({
765-
rawPath: "/path",
768+
await invalidateCDNOnRequest(
769+
{
770+
internalEvent: {
771+
rawPath: "/path",
772+
headers: {
773+
"x-isr": "1",
774+
},
775+
},
776+
},
766777
headers,
767-
isIsrRevalidation: true,
768-
});
778+
);
769779

770780
expect(
771781
globalThis.cdnInvalidationHandler.invalidatePaths,
@@ -776,17 +786,35 @@ describe("invalidateCDNOnRequest", () => {
776786
const headers: Record<string, string> = {
777787
"x-nextjs-cache": "REVALIDATED",
778788
};
779-
await invalidateCDNOnRequest({
780-
rawPath: "/path",
789+
await invalidateCDNOnRequest(
790+
{
791+
initialPath: "/path",
792+
internalEvent: {
793+
rawPath: "/path",
794+
headers: {},
795+
},
796+
resolvedRoutes: [
797+
{
798+
type: "app",
799+
route: "/path",
800+
},
801+
],
802+
},
781803
headers,
782-
});
804+
);
783805

784806
expect(
785807
globalThis.cdnInvalidationHandler.invalidatePaths,
786808
).toHaveBeenCalledWith([
787809
{
788-
path: "/path",
789-
isAppRouter: false,
810+
initialPath: "/path",
811+
rawPath: "/path",
812+
resolvedRoutes: [
813+
{
814+
type: "app",
815+
route: "/path",
816+
},
817+
],
790818
},
791819
]);
792820
});

0 commit comments

Comments
 (0)