Skip to content

Commit 0be8461

Browse files
committed
review fix
1 parent 1b01210 commit 0be8461

File tree

5 files changed

+64
-65
lines changed

5 files changed

+64
-65
lines changed

.changeset/spotty-chairs-pretend.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
---
44

55
Add additional metadata to RoutingResult
6+
7+
For some future features [#658](https://github.com/opennextjs/opennextjs-aws/issues/658) (and bug fix [#677](https://github.com/opennextjs/opennextjs-aws/issues/677)) we need to add some additional metadata to the RoutingResult.
8+
This PR adds 2 new fields to the RoutingResult: `initialPath` and `resolvedRoutes`

packages/open-next/src/adapters/config/util.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,17 @@ export function loadAppPathsManifest(nextDir: string) {
8989
return JSON.parse(appPathsManifestJson) as Record<string, string>;
9090
}
9191

92-
export function loadAppPathRoutesManifest(nextDir: string) {
92+
export function loadAppPathRoutesManifest(
93+
nextDir: string,
94+
): Record<string, string> {
9395
const appPathRoutesManifestPath = path.join(
9496
nextDir,
9597
"app-path-routes-manifest.json",
9698
);
97-
const appPathRoutesManifestJson = fs.existsSync(appPathRoutesManifestPath)
98-
? fs.readFileSync(appPathRoutesManifestPath, "utf-8")
99-
: "{}";
100-
return JSON.parse(appPathRoutesManifestJson) as Record<string, string>;
99+
if (fs.existsSync(appPathRoutesManifestPath)) {
100+
return JSON.parse(fs.readFileSync(appPathRoutesManifestPath, "utf-8"));
101+
}
102+
return {};
101103
}
102104

103105
export function loadAppPathsManifestKeys(nextDir: string) {

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

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,53 @@ import type { RouteDefinition } from "types/next-types";
33
import type { RouteType } from "types/open-next";
44

55
// Add the locale prefix to the regex so we correctly match the rawPath
6-
const optionalLocalePrefixRegex = RoutesManifest.locales.length
7-
? `^/(?:${RoutesManifest.locales.map((locale) => `${locale}/?`).join("|")})?`
8-
: "^/";
6+
const optionalLocalePrefixRegex = `^/(?:${RoutesManifest.locales.map((locale) => `${locale}/?`).join("|")})?`;
97

108
// Add the basepath prefix to the regex so we correctly match the rawPath
119
const optionalBasepathPrefixRegex = RoutesManifest.basePath
1210
? `^${RoutesManifest.basePath}/?`
1311
: "^/";
1412

1513
// Add the basePath prefix to the api routes
16-
export const apiPrefix = RoutesManifest.basePath
17-
? `${RoutesManifest.basePath}/api`
18-
: "/api";
14+
export const apiPrefix = `${RoutesManifest.basePath ?? ""}/api`;
15+
16+
const optionalPrefix = optionalLocalePrefixRegex.replace(
17+
"^/",
18+
optionalBasepathPrefixRegex,
19+
);
1920

2021
function routeMatcher(routeDefinitions: RouteDefinition[]) {
2122
const regexp = routeDefinitions.map((route) => ({
2223
page: route.page,
23-
regexp: new RegExp(
24-
route.regex
25-
.replace("^/", optionalLocalePrefixRegex)
26-
.replace("^/", optionalBasepathPrefixRegex),
27-
),
24+
regexp: new RegExp(route.regex.replace("^/", optionalPrefix)),
2825
}));
2926

27+
const appPathsSet = new Set();
28+
const routePathsSet = new Set();
3029
// We need to use AppPathRoutesManifest here
31-
const appPathsSet = new Set(
32-
Object.entries(AppPathRoutesManifest)
33-
.filter(([key, _]) => key.endsWith("page"))
34-
.map(([_, value]) => value),
35-
);
36-
const routePathsSet = new Set(
37-
Object.entries(AppPathRoutesManifest)
38-
.filter(([key, _]) => key.endsWith("route"))
39-
.map(([_, value]) => value),
40-
);
30+
for (const [k, v] of Object.entries(AppPathRoutesManifest)) {
31+
if (k.endsWith("page")) {
32+
appPathsSet.add(v);
33+
} else if (k.endsWith("route")) {
34+
routePathsSet.add(v);
35+
}
36+
}
37+
4138
return function matchRoute(path: string) {
4239
const foundRoutes = regexp.filter((route) => route.regexp.test(path));
4340

44-
if (foundRoutes.length > 0) {
45-
return foundRoutes.map((foundRoute) => {
46-
const routeType: RouteType | undefined = appPathsSet.has(
47-
foundRoute.page,
48-
)
49-
? "app"
50-
: routePathsSet.has(foundRoute.page)
51-
? "route"
52-
: "page";
53-
return {
54-
route: foundRoute.page,
55-
type: routeType,
56-
};
57-
});
58-
}
59-
return false;
41+
return foundRoutes.map((foundRoute) => {
42+
let routeType: RouteType = "page";
43+
if (appPathsSet.has(foundRoute.page)) {
44+
routeType = "app";
45+
} else if (routePathsSet.has(foundRoute.page)) {
46+
routeType = "route";
47+
}
48+
return {
49+
route: foundRoute.page,
50+
type: routeType,
51+
};
52+
});
6053
};
6154
}
6255

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ export default async function routingHandler(
115115
isExternalRewrite = beforeRewrites.isExternalRewrite;
116116
}
117117
const foundStaticRoute = staticRouteMatcher(internalEvent.rawPath);
118-
const isStaticRoute = !isExternalRewrite && Boolean(foundStaticRoute);
118+
const isStaticRoute = !isExternalRewrite && foundStaticRoute.length > 0;
119119

120-
if (!isStaticRoute && !isExternalRewrite) {
120+
if (!(isStaticRoute || isExternalRewrite)) {
121121
// Second rewrite to be applied
122122
const afterRewrites = handleRewrites(
123123
internalEvent,
@@ -135,7 +135,7 @@ export default async function routingHandler(
135135
internalEvent = fallbackEvent;
136136

137137
const foundDynamicRoute = dynamicRouteMatcher(internalEvent.rawPath);
138-
const isDynamicRoute = !isExternalRewrite && Boolean(foundDynamicRoute);
138+
const isDynamicRoute = !isExternalRewrite && foundDynamicRoute.length > 0;
139139

140140
if (!(isDynamicRoute || isStaticRoute || isExternalRewrite)) {
141141
// Fallback rewrite to be applied
@@ -167,8 +167,8 @@ export default async function routingHandler(
167167
isApiRoute ||
168168
isNextImageRoute ||
169169
// We need to check again once all rewrites have been applied
170-
staticRouteMatcher(internalEvent.rawPath) ||
171-
dynamicRouteMatcher(internalEvent.rawPath)
170+
staticRouteMatcher(internalEvent.rawPath).length > 0 ||
171+
dynamicRouteMatcher(internalEvent.rawPath).length > 0
172172
)
173173
) {
174174
internalEvent = {
@@ -209,11 +209,11 @@ export default async function routingHandler(
209209
});
210210

211211
const resolvedRoutes: ResolvedRoute[] = [
212-
...(Array.isArray(foundStaticRoute) ? foundStaticRoute : []),
213-
...(Array.isArray(foundDynamicRoute) ? foundDynamicRoute : []),
212+
...foundStaticRoute,
213+
...foundDynamicRoute,
214214
];
215215

216-
console.log("resolvedRoutes", resolvedRoutes);
216+
debug("resolvedRoutes", resolvedRoutes);
217217

218218
return {
219219
internalEvent,

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ describe("routeMatcher", () => {
7575

7676
describe("staticRouteMatcher", () => {
7777
it("should match static app route", () => {
78-
const route = staticRouteMatcher("/app");
79-
expect(route).toEqual([
78+
const routes = staticRouteMatcher("/app");
79+
expect(routes).toEqual([
8080
{
8181
route: "/app",
8282
type: "app",
@@ -85,8 +85,8 @@ describe("routeMatcher", () => {
8585
});
8686

8787
it("should match static api route", () => {
88-
const route = staticRouteMatcher("/api/app");
89-
expect(route).toEqual([
88+
const routes = staticRouteMatcher("/api/app");
89+
expect(routes).toEqual([
9090
{
9191
route: "/api/app",
9292
type: "route",
@@ -95,25 +95,25 @@ describe("routeMatcher", () => {
9595
});
9696

9797
it("should not match app dynamic route", () => {
98-
const route = staticRouteMatcher("/catchAll/slug");
99-
expect(route).toEqual(false);
98+
const routes = staticRouteMatcher("/catchAll/slug");
99+
expect(routes).toEqual([]);
100100
});
101101

102102
it("should not match page dynamic route", () => {
103-
const route = staticRouteMatcher("/page/catchAll/slug");
104-
expect(route).toEqual(false);
103+
const routes = staticRouteMatcher("/page/catchAll/slug");
104+
expect(routes).toEqual([]);
105105
});
106106

107107
it("should not match random route", () => {
108-
const route = staticRouteMatcher("/random");
109-
expect(route).toEqual(false);
108+
const routes = staticRouteMatcher("/random");
109+
expect(routes).toEqual([]);
110110
});
111111
});
112112

113113
describe("dynamicRouteMatcher", () => {
114114
it("should match dynamic app page", () => {
115-
const route = dynamicRouteMatcher("/catchAll/slug/b");
116-
expect(route).toEqual([
115+
const routes = dynamicRouteMatcher("/catchAll/slug/b");
116+
expect(routes).toEqual([
117117
{
118118
route: "/catchAll/[...slug]",
119119
type: "app",
@@ -122,8 +122,8 @@ describe("routeMatcher", () => {
122122
});
123123

124124
it("should match dynamic page router page", () => {
125-
const route = dynamicRouteMatcher("/page/catchAll/slug/b");
126-
expect(route).toEqual([
125+
const routes = dynamicRouteMatcher("/page/catchAll/slug/b");
126+
expect(routes).toEqual([
127127
{
128128
route: "/page/catchAll/[...slug]",
129129
type: "page",
@@ -134,13 +134,14 @@ describe("routeMatcher", () => {
134134
it("should match both the static and dynamic page", () => {
135135
const pathToMatch = "/page/catchAll/static";
136136
const dynamicRoutes = dynamicRouteMatcher(pathToMatch);
137-
const staticRoutes = staticRouteMatcher(pathToMatch);
138137
expect(dynamicRoutes).toEqual([
139138
{
140139
route: "/page/catchAll/[...slug]",
141140
type: "page",
142141
},
143142
]);
143+
144+
const staticRoutes = staticRouteMatcher(pathToMatch);
144145
expect(staticRoutes).toEqual([
145146
{
146147
route: "/page/catchAll/static",

0 commit comments

Comments
 (0)