December 2, 2024
O. Wolfson
Hi. With each release, Next.js introduces new patterns to enhance developer experience and application performance. In Next.js 15, the way dynamic route parameters (params) are handled in server components has undergone a significant change. Previously, in Next.js 14, params were passed synchronously as an object, making them straightforward to use. However, Next.js 15 brings a promise-based approach to optimize server-side rendering, streaming, and parallel data fetching. We'll dive into the differences between the two versions, explore why this change was made.
In Next.js 14, the way you retrieve params
in a server component is different from the pattern used in Next.js 15. Here's a comparison:
params
in a Server ComponentIn Next.js 14, params
is typically provided to the page component directly as a property by the framework. You don't need to handle it as a promise.
tsx// Next.js 14 Server Component
import React from "react";
type Props = {
params: { slug: string };
};
function Page({ params }: Props) {
const { slug } = params;
return <div>{slug}</div>;
}
export default Page;
params
is synchronous: It's passed directly as an object, and there is no need to await
anything.params
as a PromiseIn Next.js 15, params
is provided as a promise to enhance data-fetching performance, particularly for server-side rendering scenarios.
tsx// Next.js 15 Server Component
import React from "react";
type Props = {
params: Promise<{ slug: string }>;
};
async function Page({ params }: Props) {
const { slug } = await params;
return <div>{slug}</div>;
}
export default Page;
params
is asynchronous: It is passed as a promise, requiring await
to access its values.Feature | Next.js 14 | Next.js 15 |
---|---|---|
params Handling | Synchronous, passed as an object | Asynchronous, passed as a promise |
Data Fetching Pattern | Simple and synchronous | Requires await to resolve the promise |
Optimization | Less optimization for server rendering | Enhanced performance for streaming and parallel fetching |
params
in Next.js 15 aligns with the framework's emphasis on performance optimizations, especially when handling streaming SSR and server actions. It helps to reduce bottlenecks by allowing parameters to resolve concurrently with other server tasks.If you're migrating from Next.js 14 to 15, you'll need to update components to handle params
as promises in server components.
Ease of Use |
Easier for simpler cases |
Slightly more complex but scalable |