Hello!
Having explored the powerful features of Next.js 16 in the previous posts, it is time to put them into practice.
I will walk you through safely upgrading your project to Next.js 16, step by step.
This involves more than updating package versions: several
breaking changes
require code changes, so follow along carefully.
1. Use the Automated Upgrade Tool (Codemod) ๐
The Next.js team provides a powerful automation tool to help with upgrades.
I strongly recommend using it instead of updating versions manually.
Open a terminal in your project folder and run the following command.
Terminal npx @next/codemod@canary upgrade latest
This command performs the following tasks:
Updates the Next.js, React, and React DOM versions in package.json to the latest versions.
Updates the next.config.js configuration file, including removing unsupported options.
Automatically applies some API changes.
However, the command cannot fully understand and fix your business logic.
The following are
required changes
that developers need to handle themselves.
2. Required Changes: Adopt Asynchronous APIs ๐
The first and most common errors you will encounter concern synchronous API access.
As explained in part 2, runtime data access in Server Components must now be
asynchronous
.
Update params and searchParams ๐
You need to change the page component's props type and how you use it.
app/blog/[slug]/page.tsx // [๋ณ๊ฒฝ ์ ]
// export default function Page({ params }: { params: { slug: string } }) { ... }
// [๋ณ๊ฒฝ ํ]
// 1. Props ํ์
: params๋ฅผ Promise๋ก ๊ฐ์ธ์ค๋๋ค.
export default async function Page ({ params } : { params : Promise<{ slug : string }> }) {
// 2. ๋ฐ์ดํฐ ์ ๊ทผ: ๋ฐ๋์ await๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ์ถ์ถํด์ผ ํฉ๋๋ค.
// await ์์ด params.slug์ ์ ๊ทผํ๋ฉด ๋ฐํ์ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
const { slug } = await params;
return < div >Blog Post: { slug } </ div >;
}
Update cookies and headers ๐
The functions imported from next/headers also require await.
app/layout.tsx import { cookies } from 'next/headers' ;
export default async function RootLayout ({ children } : { children : React.ReactNode }) {
// [๋ณ๊ฒฝ] cookies() ํธ์ถ ์์ await๋ฅผ ๋ถ์ฌ์ผ ํฉ๋๋ค.
// ์ด์ ๋ฒ์ (v15)์์๋ ๊ฒฝ๊ณ ๋ง ๋ด์ง๋ง, v16์์๋ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
const cookieStore = await cookies ();
const theme = cookieStore. get ( 'theme' );
return (
< html lang = "en" className ={ theme?.value } >
< body > { children } </ body >
</ html >
);
}
3. Stricter next/image Security Settings ๐
The image optimization component's default settings have changed.
They are now a little stricter for security and performance.
Allow Query Strings for Local Images ๐
If you have been using query parameters such as
?v=1 in local image paths, you must now explicitly allow them in
next.config.js.
This prevents unrestricted image generation.
Allow Local IP Addresses on Private Networks ๐
If you need to load images from local IP addresses on a private network, rather than a development environment (localhost), enable the dangerouslyAllowLocalIP option.
next.config.js module . exports = {
images: {
// 1. ๋ก์ปฌ ์ด๋ฏธ์ง์ ์ฟผ๋ฆฌ ์คํธ๋ง ์ฌ์ฉ ์ ํจํด ๋ช
์ ํ์
localPatterns: [
{
pathname: '/assets/**' ,
search: '?v=1' ,
},
],
// 2. ์ฌ์ค๋ง ๋ฑ์์ ๋ก์ปฌ IP๋ก ์ด๋ฏธ์ง ๋ก๋ ์ ํ์ (๋ณด์ ์ฃผ์)
// ๊ธฐ๋ณธ๊ฐ์ false๋ก ๋ณ๊ฒฝ๋์์ต๋๋ค.
dangerouslyAllowLocalIP: true ,
},
};
4. Parallel Routes: default.js Is Required ๐
Take care if you use
Parallel Routes
, with the
@folder format.
Now, if a slot does not have a corresponding
default.js file,
the build itself fails
.
Previously, Next.js would handle this with a 404 or ignore it. Now developers must explicitly declare that there is nothing to display.
app/@analytics/default.tsx // ํด๋น ์ฌ๋กฏ์ ๋ณด์ฌ์ค ๊ธฐ๋ณธ ์ปจํ
์ธ ๊ฐ ์๋๋ผ๋ ํ์ผ์ ์์ฑํด์ผ ํฉ๋๋ค.
export default function Default () {
// ์๋ฌด๊ฒ๋ ๋ ๋๋งํ์ง ์์ผ๋ ค๋ฉด null์ ๋ฐํํฉ๋๋ค.
return null ;
}
5. Rename the Middleware File ๐
Rename the
middleware.ts (or .js) file in your project root to
proxy.ts
.
You must also rename its exported function from
middleware to
proxy.
Terminal # ํ์ผ๋ช
๋ณ๊ฒฝ
mv middleware.ts proxy.ts
proxy.ts import { NextResponse } from 'next/server' ;
import type { NextRequest } from 'next/server' ;
// [๋ณ๊ฒฝ] ํจ์ ์ด๋ฆ์ proxy๋ก ์์
export function proxy ( request : NextRequest ) {
return NextResponse. next ();
}
6. Remove Unsupported Features ๐
Finally, remove features that are no longer supported from your code.
Migrating to Next.js 16 means more than updating a version number.
It is a chance to improve your application's foundations and long-term maintainability through
asynchronous APIs
and
explicit caching
.
By carefully following this post and the reference links below, you should be able to complete your migration without major problems.
Thank you.