Skip to content

🐞 fix (server): support for awaiting next.js 15 params #1805

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

Merged
merged 2 commits into from
Oct 24, 2024
Merged
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
14 changes: 11 additions & 3 deletions packages/server/src/next/app-route-handler.ts
Original file line number Diff line number Diff line change
@@ -6,11 +6,12 @@ import { AppRouteRequestHandlerOptions } from '.';
import { RPCApiHandler } from '../api';
import { loadAssets } from '../shared';

type Context = { params: { path: string[] } };
type Context = { params: Promise<{ path: string[] }> | { path: string[] } };

/**
* Creates a Next.js 13 "app dir" API route request handler which encapsulates Prisma CRUD operations.
*
* @remarks Since Next.js 15, `context.params` is asynchronous and must be awaited.
* @param options Options for initialization
* @returns An API route request handler
*/
@@ -27,18 +28,25 @@ export default function factory(
return NextResponse.json({ message: 'unable to get prisma from request context' }, { status: 500 });
}

let params: Context['params'];
const url = new URL(req.url);
const query = Object.fromEntries(url.searchParams);

if (!context.params.path) {
try {
params = await context.params;
} catch {
return NextResponse.json({ message: 'Failed to resolve request parameters' }, { status: 500 });
}

if (!params.path) {
return NextResponse.json(
{ message: 'missing path parameter' },
{
status: 400,
}
);
}
const path = context.params.path.join('/');
const path = params.path.join('/');

let requestBody: unknown;
if (req.body) {