diff --git a/.changeset/polite-news-happen.md b/.changeset/polite-news-happen.md new file mode 100644 index 000000000..e73025e12 --- /dev/null +++ b/.changeset/polite-news-happen.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/aws": patch +--- + +fix: fix basePath support for API routes diff --git a/packages/open-next/src/core/routingHandler.ts b/packages/open-next/src/core/routingHandler.ts index 54936d666..1c4e72638 100644 --- a/packages/open-next/src/core/routingHandler.ts +++ b/packages/open-next/src/core/routingHandler.ts @@ -34,6 +34,11 @@ const optionalBasepathPrefixRegex = !!RoutesManifest.basePath ? `^${RoutesManifest.basePath}/?` : "^/"; +// Add the basePath prefix to the api routes +const apiPrefix = !!RoutesManifest.basePath + ? `${RoutesManifest.basePath}/api` + : "/api"; + const staticRegexp = RoutesManifest.routes.static.map( (route) => new RegExp( @@ -145,8 +150,8 @@ export default async function routingHandler( // /api even if it's a page route doesn't get generated in the manifest // Ideally we would need to properly check api routes here const isApiRoute = - internalEvent.rawPath === "/api" || - internalEvent.rawPath.startsWith("/api/"); + internalEvent.rawPath === apiPrefix || + internalEvent.rawPath.startsWith(`${apiPrefix}/`); const isNextImageRoute = internalEvent.rawPath.startsWith("/_next/image");