Skip to content

Commit c0ae994

Browse files
authored
fix: next.js rewrites were not respected for rest api (#10759)
Fixes #10655
1 parent 5689c65 commit c0ae994

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

packages/next/src/routes/rest/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ let initedOGEndpoint = false
66

77
const handlerBuilder =
88
(config: Promise<SanitizedConfig> | SanitizedConfig) =>
9-
async (request: Request): Promise<Response> => {
9+
async (
10+
request: Request,
11+
args: {
12+
params: Promise<{ slug: string[] }>
13+
},
14+
): Promise<Response> => {
1015
const awaitedConfig = await config
1116

1217
// Add this endpoint only when using Next.js, still can be overriden.
@@ -25,9 +30,11 @@ const handlerBuilder =
2530

2631
initedOGEndpoint = true
2732

33+
const awaitedParams = await args.params
34+
2835
const response = await handleEndpoints({
29-
basePath: process.env.NEXT_BASE_PATH,
3036
config,
37+
path: `${awaitedConfig.routes.api}/${awaitedParams.slug.join('/')}`,
3138
request,
3239
})
3340

packages/payload/src/utilities/handleEndpoints.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import { headersWithCors } from './headersWithCors.js'
1111
import { mergeHeaders } from './mergeHeaders.js'
1212
import { routeError } from './routeError.js'
1313

14-
const notFoundResponse = (req: PayloadRequest) => {
14+
const notFoundResponse = (req: PayloadRequest, pathname?: string) => {
1515
return Response.json(
1616
{
17-
message: `Route not found "${new URL(req.url).pathname}"`,
17+
message: `Route not found "${pathname ?? new URL(req.url).pathname}"`,
1818
},
1919
{
2020
headers: headersWithCors({
@@ -61,10 +61,13 @@ const notFoundResponse = (req: PayloadRequest) => {
6161
export const handleEndpoints = async ({
6262
basePath = '',
6363
config: incomingConfig,
64+
path,
6465
request,
6566
}: {
6667
basePath?: string
6768
config: Promise<SanitizedConfig> | SanitizedConfig
69+
/** Override path from the request */
70+
path?: string
6871
request: Request
6972
}): Promise<Response> => {
7073
let handler: PayloadHandler
@@ -85,6 +88,7 @@ export const handleEndpoints = async ({
8588
const response = await handleEndpoints({
8689
basePath,
8790
config: incomingConfig,
91+
path,
8892
request: new Request(url, {
8993
cache: request.cache,
9094
credentials: request.credentials,
@@ -116,10 +120,10 @@ export const handleEndpoints = async ({
116120
const { payload } = req
117121
const { config } = payload
118122

119-
const pathname = `${basePath}${new URL(req.url).pathname}`
123+
const pathname = `${basePath}${path ?? new URL(req.url).pathname}`
120124

121125
if (!pathname.startsWith(config.routes.api)) {
122-
return notFoundResponse(req)
126+
return notFoundResponse(req, pathname)
123127
}
124128

125129
// /api/posts/route -> /posts/route
@@ -213,7 +217,7 @@ export const handleEndpoints = async ({
213217
}
214218

215219
if (!handler) {
216-
return notFoundResponse(req)
220+
return notFoundResponse(req, pathname)
217221
}
218222

219223
const response = await handler(req)

0 commit comments

Comments
 (0)