Skip to content
migrating-async-request-apis logo

MIGRATION: Async Request APIs

migrating-async-request-apis

Teach async request APIs in Next.js 16 - params, searchParams, cookies(), headers(), draftMode() are now async. Use when migrating from Next.js 15, fixing type errors, or working with request data.

SKILL.md

Full skill instructions

MIGRATION: Async Request APIs

Purpose

Teach the breaking changes in Next.js 16 where request APIs are now async. This affects params, searchParams, cookies(), headers(), and draftMode() - all now return Promises and require await.

When to Use

  • Migrating from Next.js 15 to 16
  • Fixing TypeScript errors about Promise types
  • Working with route parameters, search params, or request headers
  • Encountering "object is possibly undefined" errors
  • Updating Server Components or API routes

Breaking Changes Overview

APIs Now Async

  1. Route Parameters (params)

    • Pages: params prop is now a Promise
    • Layouts: params prop is now a Promise
    • Route Handlers: params argument is now a Promise
  2. Search Parameters (searchParams)

    • Page searchParams prop is now a Promise
  3. Request APIs

    • cookies() returns Promise
    • headers() returns Promise
    • draftMode() returns Promise

Type Changes

import { cookies, headers, draftMode } from 'next/​headers';

type CookiesReturn = Promise<ReadonlyRequestCookies>;
type HeadersReturn = Promise<ReadonlyHeaders>;
type DraftModeReturn = Promise<{ isEnabled: boolean }>;

Migration Patterns

Pattern 1: Page Route Params

Before (Next.js 15):

export default function Page({ params }: { params: { id: string } }) {
  return <div>User ID: {params.id}</​div>;
}

export async function generateMetadata({ params }: { params: { id: string } }) {
  return { title: `User ${params.id}` };
}

After (Next.js 16):

export default async function Page({
  params
}: {
  params: Promise<{ id: string }>
}) {
  const { id } = await params;
  return <div>User ID: {id}</​div>;
}

export async function generateMetadata({
  params
}: {
  params: Promise<{ id: string }>
}) {
  const { id } = await params;
  return { title: `User ${id}` };
}

Pattern 2: Search Parameters

Before (Next.js 15):

export default function SearchPage({
  searchParams
}: {
  searchParams: { q?: string; page?: string }
}) {
  const query = searchParams.q || '';
  const page = Number(searchParams.page) || 1;

  return <SearchResults query={query} page={page} />;
}

After (Next.js 16):

export default async function SearchPage({
  searchParams
}: {
  searchParams: Promise<{ q?: string; page?: string }>
}) {
  const params = await searchParams;
  const query = params.q || '';
  const page = Number(params.page) || 1;

  return <SearchResults query={query} page={page} />;
}

Pattern 3: Cookies API

Before (Next.js 15):

import { cookies } from 'next/​headers';

export default function Component() {
  const cookieStore = cookies();
  const token = cookieStore.get('token');

  return <div>Token: {token?.value}</​div>;
}

After (Next.js 16):

import { cookies } from 'next/​headers';

export default async function Component() {
  const cookieStore = await cookies();
  const token = cookieStore.get('token');

  return <div>Token: {token?.value}</​div>;
}

Pattern 4: Headers API

Before (Next.js 15):

import { headers } from 'next/​headers';

export default function Component() {
  const headersList = headers();
  const userAgent = headersList.get('user-agent');

  return <div>User Agent: {userAgent}</​div>;
}

After (Next.js 16):

import { headers } from 'next/​headers';

export default async function Component() {
  const headersList = await headers();
  const userAgent = headersList.get('user-agent');

  return <div>User Agent: {userAgent}</​div>;
}

Pattern 5: Draft Mode

Before (Next.js 15):

import { draftMode } from 'next/​headers';

export default function Component() {
  const { isEnabled } = draftMode();

  return <div>Draft mode: {isEnabled ? 'on' : 'off'}</​div>;
}

After (Next.js 16):

import { draftMode } from 'next/​headers';

export default async function Component() {
  const { isEnabled } = await draftMode();

  return <div>Draft mode: {isEnabled ? 'on' : 'off'}</​div>;
}

Pattern 6: Route Handlers

Before (Next.js 15):

export async function GET(
  request: Request,
  { params }: { params: { id: string } }
) {
  const headersList = headers();
  const auth = headersList.get('authorization');

  return Response.json({ id: params.id, auth });
}

After (Next.js 16):

export async function GET(
  request: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  const [{ id }, headersList] = await Promise.all([
    params,
    headers()
  ]);
  const auth = headersList.get('authorization');

  return Response.json({ id, auth });
}

Pattern 7: Layouts with Params

Before (Next.js 15):

export default function Layout({
  children,
  params
}: {
  children: React.ReactNode;
  params: { locale: string };
}) {
  return (
    <html lang={params.locale}>
      <body>{children}</​body>
    </​html>
  );
}

After (Next.js 16):

export default async function Layout({
  children,
  params
}: {
  children: React.ReactNode;
  params: Promise<{ locale: string }>;
}) {
  const { locale } = await params;

  return (
    <html lang={locale}>
      <body>{children}</​body>
    </​html>
  );
}

Common Migration Errors

Error 1: Missing await

const { id } = params;

Fix:

const { id } = await params;

Error 2: Non-async function

export default function Page({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
}

Fix:

export default async function Page({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
}

Error 3: Wrong type annotation

export default async function Page({ params }: { params: { id: string } }) {
  const { id } = await params;
}

Fix:

export default async function Page({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
}

Error 4: Accessing properties directly

const cookieStore = await cookies();
const allCookies = cookieStore.getAll();

Fix (Same, but ensure await on cookies()):

const cookieStore = await cookies();
const allCookies = cookieStore.getAll();

Performance Optimization

Use Promise.all for Multiple Async Calls

Inefficient (Sequential):

const { id } = await params;
const search = await searchParams;
const cookieStore = await cookies();
const headersList = await headers();

Optimized (Parallel):

const [{ id }, search, cookieStore, headersList] = await Promise.all([
  params,
  searchParams,
  cookies(),
  headers()
]);

When to Use Sequential vs Parallel

Sequential (dependencies exist):

const { id } = await params;
const data = await fetchData(id);

Parallel (no dependencies):

const [{ id }, cookieStore] = await Promise.all([
  params,
  cookies()
]);

Type Safety

Define Reusable Types

type PageParams<T = {}> = Promise<T>;
type SearchParams = Promise<{ [key: string]: string | string[] | undefined }>;

type PageProps<T = {}> = {
  params: PageParams<T>;
  searchParams: SearchParams;
};

export default async function Page({ params, searchParams }: PageProps<{ id: string }>) {
  const { id } = await params;
  const search = await searchParams;

  return <div>{id}</​div>;
}

Type Guards with Promises

For advanced Promise type handling and type guards, see @typescript/​TYPES-type-guards skill.

function isValidId(id: string): id is string {
  return /^[a-z0-9]+$/​i.test(id);
}

export default async function Page({
  params
}: {
  params: Promise<{ id: string }>
}) {
  const { id } = await params;

  if (!isValidId(id)) {
    return <div>Invalid ID</​div>;
  }

  return <div>Valid ID: {id}</​div>;
}

Special Cases

Multi-Segment Routes

export default async function Page({
  params
}: {
  params: Promise<{ category: string; product: string }>
}) {
  const { category, product } = await params;

  return (
    <div>
      <h1>Category: {category}</​h1>
      <h2>Product: {product}</​h2>
    </​div>
  );
}

Catch-All Routes

export default async function Page({
  params
}: {
  params: Promise<{ slug: string[] }>
}) {
  const { slug } = await params;
  const path = slug.join('/');

  return <div>Path: {path}</​div>;
}

Optional Catch-All Routes

export default async function Page({
  params
}: {
  params: Promise<{ slug?: string[] }>
}) {
  const { slug } = await params;

  if (!slug) {
    return <div>Home Page</​div>;
  }

  return <div>Path: {slug.join('/')}</​div>;
}

References

For detailed migration examples, edge cases, and troubleshooting, see:

Migration Checklist

When migrating to async request APIs:

  1. Update all params props to Promise<T> type
  2. Update all searchParams props to Promise<T> type
  3. Add await to all cookies() calls
  4. Add await to all headers() calls
  5. Add await to all draftMode() calls
  6. Convert components to async where needed
  7. Update type annotations
  8. Use Promise.all for multiple async calls
  9. Test all dynamic routes
  10. Test all API routes
  11. Verify TypeScript compilation
  12. Run production build to catch errors

Success Criteria

  • No TypeScript errors related to Promise types
  • All dynamic routes work correctly
  • All API routes handle params properly
  • Cookies, headers, and draft mode work as expected
  • No runtime errors about "object is possibly undefined"
  • Build completes successfully

More Other skills

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View

Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.

179.7K 100.1K
View

Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with colors/fonts that you can apply to any artifact that has been creating, or can generate a new theme on-the-fly.

179.7K 89.5K
View

A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill whenever asked to write some sort of internal communications (status reports, leadership updates, 3P updates, company newsletters, FAQs, incident reports, project updates, etc.).

179.7K 76.1K
View

Fundamental design principles for writing maintainable, testable Python code.

40.2K 21.9K
View

Modern responsive layouts using container queries, fluid typography, CSS Grid, and mobile-first strategies.

40.2K 20K
View

Generate Playwright tests from scenarios using interactive browser exploration and validation.

39.7K 18.2K
View

Modern React state management with Redux Toolkit, Zustand, Jotai, and React Query for every state category.

40.2K 14.7K
View

GDPR-compliant data handling with consent management, data subject rights, and privacy controls.

40.2K 14.3K
View

Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.

40.2K 13.5K
View

Detailed multi-file refactor planning with safety checks and rollback guidance.

39.7K 13K
View