Skip to content

Commit eff1cd2

Browse files
committed
review
1 parent cde5938 commit eff1cd2

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed
Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from "node:path";
22

3-
import type { RouteDefinition } from "types/next-types";
43
import { debug } from "../logger";
54
import {
65
loadAppPathRoutesManifest,
@@ -12,7 +11,6 @@ import {
1211
loadFunctionsConfigManifest,
1312
loadHtmlPages,
1413
loadMiddlewareManifest,
15-
loadPagesManifest,
1614
loadPrerenderManifest,
1715
loadRoutesManifest,
1816
} from "./util.js";
@@ -41,38 +39,3 @@ export const AppPathRoutesManifest =
4139

4240
export const FunctionsConfigManifest =
4341
/* @__PURE__ */ loadFunctionsConfigManifest(NEXT_DIR);
44-
45-
/**
46-
* Returns static API routes for both app and pages router cause Next will filter them out in staticRoutes in `routes-manifest.json`.
47-
* We also need to filter out page files that are under `app/api/*` as those would not be present in the routes manifest either.
48-
* This line from Next.js skips it:
49-
* https://github.com/vercel/next.js/blob/ded56f952154a40dcfe53bdb38c73174e9eca9e5/packages/next/src/build/index.ts#L1299
50-
*
51-
* Without it handleFallbackFalse will 404 on static API routes if there is a catch-all route on root level.
52-
*/
53-
export function getStaticAPIRoutes(): RouteDefinition[] {
54-
const createRouteDefinition = (route: string) => ({
55-
page: route,
56-
regex: `^${route}(?:/)?$`,
57-
});
58-
const dynamicRoutePages = new Set(
59-
RoutesManifest.routes.dynamic.map(({ page }) => page),
60-
);
61-
const PagesManifest = loadPagesManifest(NEXT_DIR);
62-
const pagesStaticAPIRoutes = Object.keys(PagesManifest)
63-
.filter(
64-
(route) => route.startsWith("/api/") && !dynamicRoutePages.has(route),
65-
)
66-
.map(createRouteDefinition);
67-
68-
// We filter out both static API and page routes from the app paths manifest
69-
const appPathsStaticAPIRoutes = Object.values(AppPathRoutesManifest)
70-
.filter(
71-
(route) =>
72-
route.startsWith("/api/") ||
73-
(route === "/api" && !dynamicRoutePages.has(route)),
74-
)
75-
.map(createRouteDefinition);
76-
77-
return [...pagesStaticAPIRoutes, ...appPathsStaticAPIRoutes];
78-
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,8 @@ export function handleFallbackFalse(
437437
? rawPath
438438
: `/${NextConfig.i18n?.defaultLocale}${rawPath}`;
439439
// We need to remove the trailing slash if it exists
440-
// Not if localizedPath is "/" tho, because that would not make it find `isPregenerated` below since it would be try to match an empty string.
441440
if (
441+
// Not if localizedPath is "/" tho, because that would not make it find `isPregenerated` below since it would be try to match an empty string.
442442
localizedPath !== "/" &&
443443
NextConfig.trailingSlash &&
444444
localizedPath.endsWith("/")

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import {
2-
AppPathRoutesManifest,
3-
RoutesManifest,
4-
getStaticAPIRoutes,
5-
} from "config/index";
1+
import { AppPathRoutesManifest, NEXT_DIR, RoutesManifest } from "config/index";
2+
import { loadPagesManifest } from "config/util";
63
import type { RouteDefinition } from "types/next-types";
74
import type { ResolvedRoute, RouteType } from "types/open-next";
85

@@ -62,3 +59,38 @@ export const staticRouteMatcher = routeMatcher([
6259
...getStaticAPIRoutes(),
6360
]);
6461
export const dynamicRouteMatcher = routeMatcher(RoutesManifest.routes.dynamic);
62+
63+
/**
64+
* Returns static API routes for both app and pages router cause Next will filter them out in staticRoutes in `routes-manifest.json`.
65+
* We also need to filter out page files that are under `app/api/*` as those would not be present in the routes manifest either.
66+
* This line from Next.js skips it:
67+
* https://github.com/vercel/next.js/blob/ded56f952154a40dcfe53bdb38c73174e9eca9e5/packages/next/src/build/index.ts#L1299
68+
*
69+
* Without it handleFallbackFalse will 404 on static API routes if there is a catch-all route on root level.
70+
*/
71+
export function getStaticAPIRoutes(): RouteDefinition[] {
72+
const createRouteDefinition = (route: string) => ({
73+
page: route,
74+
regex: `^${route}(?:/)?$`,
75+
});
76+
const dynamicRoutePages = new Set(
77+
RoutesManifest.routes.dynamic.map(({ page }) => page),
78+
);
79+
const PagesManifest = loadPagesManifest(NEXT_DIR);
80+
const pagesStaticAPIRoutes = Object.keys(PagesManifest)
81+
.filter(
82+
(route) => route.startsWith("/api/") && !dynamicRoutePages.has(route),
83+
)
84+
.map(createRouteDefinition);
85+
86+
// We filter out both static API and page routes from the app paths manifest
87+
const appPathsStaticAPIRoutes = Object.values(AppPathRoutesManifest)
88+
.filter(
89+
(route) =>
90+
route.startsWith("/api/") ||
91+
(route === "/api" && !dynamicRoutePages.has(route)),
92+
)
93+
.map(createRouteDefinition);
94+
95+
return [...pagesStaticAPIRoutes, ...appPathsStaticAPIRoutes];
96+
}

0 commit comments

Comments
 (0)