Codemods
Codemods 是在你的代码库上以编程方式运行的转换。这允许大量更改以编程方式应用,而无需手动遍历每个文件。
Next.js 提供 Codemod 转换来帮助在 API 更新或弃用时升级你的 Next.js 代码库。
用法
在终端中,导航(cd
)到你的项目文件夹,然后运行:
npx @next/codemod <transform> <path>
用适当的值替换 <transform>
和 <path>
。
transform
- 转换的名称path
- 要转换的文件或目录--dry
进行试运行,不会编辑代码--print
打印更改的输出以供比较
Codemods
15.0
将 App Router 路由段配置 runtime
值从 experimental-edge
转换为 edge
app-dir-runtime-config-experimental-edge
注意:此 codemod 特定于 App Router。
npx @next/codemod@latest app-dir-runtime-config-experimental-edge .
此 codemod 将 路由段配置 runtime
值 experimental-edge
转换为 edge
。
例如:
export const runtime = 'experimental-edge'
转换为:
export const runtime = 'edge'
迁移到异步动态 API
选择动态渲染的 API 之前支持同步访问,现在是异步的。你可以在升级指南中了解更多关于此破坏性更改的信息。
next-async-request-api
npx @next/codemod@latest next-async-request-api .
此 codemod 将转换现在是异步的动态 API(来自 next/headers
的 cookies()
、headers()
和 draftMode()
),使其被正确等待或在适用时用 React.use()
包装。
当自动迁移不可能时,codemod 将添加类型转换(如果是 TypeScript 文件)或注释来通知用户需要手动审查和更新。
例如:
import { cookies, headers } from 'next/headers'
const token = cookies().get('token')
function useToken() {
const token = cookies().get('token')
return token
}
export default function Page() {
const name = cookies().get('name')
}
function getHeader() {
return headers().get('x-foo')
}
转换为:
import { use } from 'react'
import {
cookies,
headers,
type UnsafeUnwrappedCookies,
type UnsafeUnwrappedHeaders,
} from 'next/headers'
const token = (cookies() as unknown as UnsafeUnwrappedCookies).get('token')
function useToken() {
const token = use(cookies()).get('token')
return token
}
export default async function Page() {
const name = (await cookies()).get('name')
}
function getHeader() {
return (headers() as unknown as UnsafeUnwrappedHeaders).get('x-foo')
}
当我们检测到页面/路由条目(page.js
、layout.js
、route.js
或 default.js
)或 generateMetadata
/ generateViewport
API 中 params
或 searchParams
props 上的属性访问时,
它将尝试将调用点从同步函数转换为异步函数,并等待属性访问。如果无法使其异步(例如使用客户端组件),它将使用 React.use
来解包 Promise。
例如:
// page.tsx
export default function Page({
params,
searchParams,
}: {
params: { slug: string }
searchParams: { [key: string]: string | string[] | undefined }
}) {
const { value } = searchParams
if (value === 'foo') {
// ...
}
}
export function generateMetadata({ params }: { params: { slug: string } }) {
const { slug } = params
return {
title: `My Page - ${slug}`,
}
}
转换为:
// page.tsx
export default async function Page(props: {
params: Promise<{ slug: string }>
searchParams: Promise<{ [key: string]: string | string[] | undefined }>
}) {
const searchParams = await props.searchParams
const { value } = searchParams
if (value === 'foo') {
// ...
}
}
export async function generateMetadata(props: {
params: Promise<{ slug: string }>
}) {
const params = await props.params
const { slug } = params
return {
title: `My Page - ${slug}`,
}
}
需要了解: 当此 codemod 识别可能需要手动干预但无法确定确切修复的位置时,它会在代码中添加注释或类型转换来通知用户需要手动更新。这些注释以 @next/codemod 为前缀,类型转换以
UnsafeUnwrapped
为前缀。 你的构建将出错,直到这些注释被明确删除。了解更多。
用 @vercel/functions
替换 NextRequest
的 geo
和 ip
属性
next-request-geo-ip
npx @next/codemod@latest next-request-geo-ip .
此 codemod 安装 @vercel/functions
并将 NextRequest
的 geo
和 ip
属性转换为相应的 @vercel/functions
功能。
例如:
import type { NextRequest } from 'next/server'
export function GET(req: NextRequest) {
const { geo, ip } = req
}
转换为:
import type { NextRequest } from 'next/server'
import { geolocation, ipAddress } from '@vercel/functions'
export function GET(req: NextRequest) {
const geo = geolocation(req)
const ip = ipAddress(req)
}
14.0
迁移 ImageResponse
导入
next-og-import
npx @next/codemod@latest next-og-import .
此 codemod 将 动态 OG 图像生成 的导入从 next/server
转换为 next/og
。
例如:
import { ImageResponse } from 'next/server'
转换为:
import { ImageResponse } from 'next/og'