Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: next.js rewrites were not respected for rest api #10759

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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