Skip to content

Commit

Permalink
fix: next.js rewrites were not respected for rest api (#10759)
Browse files Browse the repository at this point in the history
Fixes #10655
  • Loading branch information
r1tsuu authored Jan 23, 2025
1 parent 5689c65 commit c0ae994
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 9 additions & 2 deletions packages/next/src/routes/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ let initedOGEndpoint = false

const handlerBuilder =
(config: Promise<SanitizedConfig> | SanitizedConfig) =>
async (request: Request): Promise<Response> => {
async (
request: Request,
args: {
params: Promise<{ slug: string[] }>
},
): Promise<Response> => {
const awaitedConfig = await config

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

initedOGEndpoint = true

const awaitedParams = await args.params

const response = await handleEndpoints({
basePath: process.env.NEXT_BASE_PATH,
config,
path: `${awaitedConfig.routes.api}/${awaitedParams.slug.join('/')}`,
request,
})

Expand Down
14 changes: 9 additions & 5 deletions packages/payload/src/utilities/handleEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { headersWithCors } from './headersWithCors.js'
import { mergeHeaders } from './mergeHeaders.js'
import { routeError } from './routeError.js'

const notFoundResponse = (req: PayloadRequest) => {
const notFoundResponse = (req: PayloadRequest, pathname?: string) => {
return Response.json(
{
message: `Route not found "${new URL(req.url).pathname}"`,
message: `Route not found "${pathname ?? new URL(req.url).pathname}"`,
},
{
headers: headersWithCors({
Expand Down Expand Up @@ -61,10 +61,13 @@ const notFoundResponse = (req: PayloadRequest) => {
export const handleEndpoints = async ({
basePath = '',
config: incomingConfig,
path,
request,
}: {
basePath?: string
config: Promise<SanitizedConfig> | SanitizedConfig
/** Override path from the request */
path?: string
request: Request
}): Promise<Response> => {
let handler: PayloadHandler
Expand All @@ -85,6 +88,7 @@ export const handleEndpoints = async ({
const response = await handleEndpoints({
basePath,
config: incomingConfig,
path,
request: new Request(url, {
cache: request.cache,
credentials: request.credentials,
Expand Down Expand Up @@ -116,10 +120,10 @@ export const handleEndpoints = async ({
const { payload } = req
const { config } = payload

const pathname = `${basePath}${new URL(req.url).pathname}`
const pathname = `${basePath}${path ?? new URL(req.url).pathname}`

if (!pathname.startsWith(config.routes.api)) {
return notFoundResponse(req)
return notFoundResponse(req, pathname)
}

// /api/posts/route -> /posts/route
Expand Down Expand Up @@ -213,7 +217,7 @@ export const handleEndpoints = async ({
}

if (!handler) {
return notFoundResponse(req)
return notFoundResponse(req, pathname)
}

const response = await handler(req)
Expand Down

0 comments on commit c0ae994

Please # to comment.