Skip to content

Commit 1b91708

Browse files
authored
fix 404 when basePath is set (#485)
* fix 404 when basePath is set * Create lovely-penguins-taste.md
1 parent 8ddb621 commit 1b91708

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
fix 404 when basePath is set

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function loadRoutesManifest(nextDir: string) {
4646
};
4747

4848
return {
49+
basePath: routesManifest.basePath,
4950
rewrites: Array.isArray(routesManifest.rewrites)
5051
? { beforeFiles: [], afterFiles: routesManifest.rewrites, fallback: [] }
5152
: {

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,27 @@ const optionalLocalePrefixRegex = !!RoutesManifest.locales.length
2828
? `^/(?:${RoutesManifest.locales.map((locale) => `${locale}/?`).join("|")})?`
2929
: "^/";
3030

31+
// Add the basepath prefix to the regex so we correctly match the rawPath
32+
const optionalBasepathPrefixRegex = !!RoutesManifest.basePath
33+
? `^${RoutesManifest.basePath}/?`
34+
: "^/";
35+
3136
const staticRegexp = RoutesManifest.routes.static.map(
32-
(route) => new RegExp(route.regex.replace("^/", optionalLocalePrefixRegex)),
37+
(route) =>
38+
new RegExp(
39+
route.regex
40+
.replace("^/", optionalLocalePrefixRegex)
41+
.replace("^/", optionalBasepathPrefixRegex),
42+
),
3343
);
3444

3545
const dynamicRegexp = RoutesManifest.routes.dynamic.map(
36-
(route) => new RegExp(route.regex.replace("^/", optionalLocalePrefixRegex)),
46+
(route) =>
47+
new RegExp(
48+
route.regex
49+
.replace("^/", optionalLocalePrefixRegex)
50+
.replace("^/", optionalBasepathPrefixRegex),
51+
),
3752
);
3853

3954
export default async function routingHandler(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export interface RedirectDefinition extends RewriteDefinition {
103103
}
104104

105105
export interface RoutesManifest {
106+
basePath?: string;
106107
dynamicRoutes: RouteDefinition[];
107108
staticRoutes: RouteDefinition[];
108109
dataRoutes: DataRouteDefinition[];

0 commit comments

Comments
 (0)