2024-12-02 Web Development

Params in Next.js: From Version 14 to 15

By O. Wolfson

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:


Next.js 14: Retrieving params in a Server Component

In 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;
  • Key Points in Next.js 14:
    1. params is synchronous: It's passed directly as an object, and there is no need to await anything.
    2. No Promises involved: This makes the implementation simpler and easier to reason about, but it may not optimize certain server-rendering workflows.

Next.js 15: Retrieving params as a Promise

In 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;
  • Key Points in Next.js 15:
    1. params is asynchronous: It is passed as a promise, requiring await to access its values.
    2. Optimized server rendering: This pattern enables better integration with streaming, parallel data fetching, and other performance optimizations introduced in Next.js 15.

Comparison of Key Differences

FeatureNext.js 14Next.js 15
params HandlingSynchronous, passed as an objectAsynchronous, passed as a promise
Data Fetching PatternSimple and synchronousRequires await to resolve the promise
OptimizationLess optimization for server renderingEnhanced performance for streaming and parallel fetching
Ease of UseEasier for simpler casesSlightly more complex but scalable

Why the Change?

  • The move to asynchronous 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.