migrate-to-vinext
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMigrate Next.js to vinext
将Next.js迁移至vinext
vinext reimplements the Next.js API surface on Vite. Existing , , and work as-is — migration is a package swap, config generation, and ESM conversion. No changes to application code required.
app/pages/next.config.jsvinext 在Vite之上重实现了Next.js的API接口。现有、目录及可直接使用——迁移仅需替换依赖包、生成配置文件并转换为ESM格式,无需修改应用代码。
app/pages/next.config.jsFIRST: Verify Next.js Project
第一步:验证Next.js项目
Confirm is in or in . If not found, STOP — this skill does not apply.
nextdependenciesdevDependenciespackage.jsonDetect the package manager from the lockfile:
| Lockfile | Manager | Install | Uninstall |
|---|---|---|---|
| pnpm | | |
| yarn | | |
| bun | | |
| npm | | |
Detect the router: if an directory exists at root or under , it's App Router. If only exists, it's Pages Router. Both can coexist.
app/src/pages/确认的或中包含。若未找到,请停止操作——本技能不适用当前项目。
package.jsondependenciesdevDependenciesnext通过锁文件检测包管理器:
| 锁文件 | 管理器 | 安装命令 | 卸载命令 |
|---|---|---|---|
| pnpm | | |
| yarn | | |
| bun | | |
| npm | | |
检测路由类型:若根目录或下存在目录,则为App Router;若仅存在目录,则为Pages Router。两种路由可共存。
src/app/pages/Quick Reference
快速参考
| Command | Purpose |
|---|---|
| Scan project for compatibility issues, produce scored report |
| Automated migration — installs deps, generates config, converts to ESM |
| Development server with HMR |
| Production build (multi-environment for App Router) |
| Local production server |
| Build and deploy to Cloudflare Workers |
| 命令 | 用途 |
|---|---|
| 扫描项目兼容性问题,生成带评分的报告 |
| 自动化迁移——安装依赖、生成配置、转换为ESM格式 |
| 带HMR的开发服务器 |
| 生产构建(App Router支持多环境) |
| 本地生产服务器 |
| 构建并部署至Cloudflare Workers |
Phase 1: Check Compatibility
阶段1:检查兼容性
Run (install vinext first if needed via ). Review the scored report. If critical incompatibilities exist, inform the user before proceeding.
vinext checknpx vinext checkSee references/compatibility.md for supported/unsupported features and ecosystem library status.
运行(若未安装vinext,可通过直接执行)。查看评分报告。若存在严重兼容性问题,请在继续操作前告知用户。
vinext checknpx vinext check查看references/compatibility.md了解支持/不支持的功能及生态库状态。
Phase 2: Automated Migration (Recommended)
阶段2:自动化迁移(推荐)
Run . This command:
vinext init- Runs for a compatibility report
vinext check - Installs as a devDependency (and
vitefor App Router)@vitejs/plugin-rsc - Adds to package.json
"type": "module" - Renames CJS config files (e.g., →
postcss.config.js) to avoid ESM conflicts.cjs - Adds and
dev:vinextscripts to package.jsonbuild:vinext - Generates a minimal
vite.config.ts
This is non-destructive — the existing Next.js setup continues to work alongside vinext. Use the script to test before fully switching over.
dev:vinextIf succeeds, skip to Phase 4 (Verify). If it fails or the user prefers manual control, continue to Phase 3.
vinext init运行。该命令会执行以下操作:
vinext init- 运行生成兼容性报告
vinext check - 安装作为devDependency(若为App Router,还会安装
vite)@vitejs/plugin-rsc - 向package.json中添加
"type": "module" - 重命名CJS配置文件(如→
postcss.config.js)以避免ESM冲突.cjs - 向package.json中添加和
dev:vinext脚本build:vinext - 生成极简版
vite.config.ts
此操作不会破坏原有配置——现有Next.js设置可与vinext并行使用。在完全切换前,可使用脚本进行测试。
dev:vinext若执行成功,直接跳至阶段4(验证)。若执行失败或用户偏好手动控制,请继续阶段3。
vinext initPhase 3: Manual Migration
阶段3:手动迁移
Use this as a fallback when doesn't work or the user wants full control.
vinext init当无法正常工作或用户希望完全掌控迁移过程时,可使用此方案作为备选。
vinext init3a. Replace packages
3a. 替换依赖包
bash
undefinedbash
undefinedExample with npm:
npm示例:
npm uninstall next
npm install vinext
npm install -D vite
npm uninstall next
npm install vinext
npm install -D vite
App Router only:
仅App Router需要:
npm install -D @vitejs/plugin-rsc
undefinednpm install -D @vitejs/plugin-rsc
undefined3b. Update scripts
3b. 更新脚本
Replace all commands in scripts:
nextpackage.json| Before | After | Notes |
|---|---|---|
| | Dev server with HMR |
| | Production build |
| | Local production server |
| | Delegates to eslint/oxlint |
Preserve flags: → .
next dev --port 3001vinext dev --port 3001替换脚本中所有命令:
package.jsonnext| 原命令 | 新命令 | 说明 |
|---|---|---|
| | 带HMR的开发服务器 |
| | 生产构建 |
| | 本地生产服务器 |
| | 委托给eslint/oxlint执行 |
保留参数: → 。
next dev --port 3001vinext dev --port 30013c. Convert to ESM
3c. 转换为ESM格式
Add to package.json. Rename any CJS config files:
"type": "module"- →
postcss.config.jspostcss.config.cjs - →
tailwind.config.jstailwind.config.cjs - Any other config that uses
.jsmodule.exports
向package.json中添加。重命名所有CJS配置文件:
"type": "module"- →
postcss.config.jspostcss.config.cjs - →
tailwind.config.jstailwind.config.cjs - 其他使用的
module.exports配置文件.js
3d. Generate vite.config.ts
3d. 生成vite.config.ts
See references/config-examples.md for config variants per router and deployment target.
If the project already has custom Vite config, prefer Vite 8-native keys when editing it: , , and . Older and settings still work for now but are migration targets.
oxcoptimizeDeps.rolldownOptionsbuild.rolldownOptionsesbuildbuild.rollupOptionsPages Router (minimal):
ts
import vinext from "vinext";
import { defineConfig } from "vite";
export default defineConfig({ plugins: [vinext()] });App Router (minimal):
ts
import vinext from "vinext";
import { defineConfig } from "vite";
export default defineConfig({ plugins: [vinext()] });vinext auto-registers for App Router when the option is not explicitly . No manual RSC plugin config needed for local development.
@vitejs/plugin-rscrscfalse查看references/config-examples.md获取不同路由及部署目标的配置示例。
若项目已有自定义Vite配置,编辑时优先使用Vite 8原生配置项:、和。旧版和设置目前仍可使用,但属于待迁移的配置项。
oxcoptimizeDeps.rolldownOptionsbuild.rolldownOptionsesbuildbuild.rollupOptionsPages Router(极简版):
ts
import vinext from "vinext";
import { defineConfig } from "vite";
export default defineConfig({ plugins: [vinext()] });App Router(极简版):
ts
import vinext from "vinext";
import { defineConfig } from "vite";
export default defineConfig({ plugins: [vinext()] });当选项未显式设置为时,vinext会自动为App Router注册。本地开发无需手动配置RSC插件。
rscfalse@vitejs/plugin-rscPhase 4: Deployment (Optional)
阶段4:部署(可选)
Option A: Cloudflare Workers (recommended for Cloudflare)
选项A:Cloudflare Workers(Cloudflare推荐方案)
If the user wants to deploy to Cloudflare Workers, use . It auto-generates , worker entry, and Vite config if missing, installs and , then builds and deploys.
vinext deploywrangler.jsonc@cloudflare/vite-pluginwranglerFor manual setup or custom worker entries, see references/config-examples.md.
若用户希望部署至Cloudflare Workers,使用。该命令会自动生成、Worker入口文件及缺失的Vite配置,安装和,然后执行构建与部署。
vinext deploywrangler.jsonc@cloudflare/vite-pluginwrangler如需手动设置或自定义Worker入口,请查看references/config-examples.md。
Cloudflare Bindings (D1, R2, KV, AI, etc.)
Cloudflare绑定(D1、R2、KV、AI等)
To access Cloudflare bindings (D1, R2, KV, AI, Queues, Durable Objects, etc.), use in any server component, route handler, or server action:
import { env } from "cloudflare:workers"tsx
import { env } from "cloudflare:workers";
export default async function Page() {
const result = await env.DB.prepare("SELECT * FROM posts").all();
return <div>{JSON.stringify(result)}</div>;
}This works because runs server environments in workerd, where is a native module. No custom worker entry, no , no special configuration needed. Just import and use.
@cloudflare/vite-plugincloudflare:workersgetPlatformProxy()Bindings must be defined in . For TypeScript types, run .
wrangler.jsoncwrangler typesIMPORTANT: Do not use , , or custom worker entries with to access bindings. These are older patterns. is the recommended approach and works out of the box with vinext.
getPlatformProxy()getRequestContext()fetch(request, env)cloudflare:workers要访问Cloudflare绑定(D1、R2、KV、AI、队列、Durable Objects等),可在任意服务器组件、路由处理器或服务器操作中使用:
import { env } from "cloudflare:workers"tsx
import { env } from "cloudflare:workers";
export default async function Page() {
const result = await env.DB.prepare("SELECT * FROM posts").all();
return <div>{JSON.stringify(result)}</div>;
}此方式可行的原因是在workerd中运行服务器环境,而是原生模块。无需自定义Worker入口、或特殊配置,直接导入即可使用。
@cloudflare/vite-plugincloudflare:workersgetPlatformProxy()绑定需在中定义。如需TypeScript类型,运行。
wrangler.jsoncwrangler types重要提示: 请勿使用、或自定义 Worker入口来访问绑定。这些属于旧模式。推荐使用方式,可与vinext开箱即用。
getPlatformProxy()getRequestContext()fetch(request, env)cloudflare:workersOption B: Other platforms (via Nitro)
选项B:其他平台(通过Nitro)
For deploying to Vercel, Netlify, AWS, Deno Deploy, or any other Nitro-supported platform, add the Nitro Vite plugin:
bash
npm install nitrots
// vite.config.ts
import { defineConfig } from "vite";
import vinext from "vinext";
import { nitro } from "nitro/vite";
export default defineConfig({
plugins: [vinext(), nitro()],
});Build and deploy:
bash
NITRO_PRESET=vercel npx vite build # Vercel
NITRO_PRESET=netlify npx vite build # Netlify
NITRO_PRESET=deno_deploy npx vite build # Deno Deploy
NITRO_PRESET=node npx vite build # Node.js serverNitro auto-detects the platform in most CI/CD environments, so the preset is often unnecessary.
Note: For Cloudflare Workers, Nitro works but the native integration ( / ) is recommended for the best developer experience with bindings, KV caching, and one-command deploys.
vinext deploy@cloudflare/vite-plugincloudflare:workers如需部署至Vercel、Netlify、AWS、Deno Deploy或其他Nitro支持的平台,添加Nitro Vite插件:
bash
npm install nitrots
// vite.config.ts
import { defineConfig } from "vite";
import vinext from "vinext";
import { nitro } from "nitro/vite";
export default defineConfig({
plugins: [vinext(), nitro()],
});构建并部署:
bash
NITRO_PRESET=vercel npx vite build # Vercel
NITRO_PRESET=netlify npx vite build # Netlify
NITRO_PRESET=deno_deploy npx vite build # Deno Deploy
NITRO_PRESET=node npx vite build # Node.js服务器在大多数CI/CD环境中,Nitro会自动检测平台,因此通常无需指定预设。
注意: 对于Cloudflare Workers,Nitro可正常工作,但原生集成( / )提供更好的开发者体验,支持绑定、KV缓存及一键部署。因此推荐使用原生设置而非Nitro。
vinext deploy@cloudflare/vite-plugincloudflare:workersPhase 5: Verify
阶段5:验证
- Run to start the development server
vinext dev - Confirm the server starts without errors
- Navigate key routes and check functionality
- Report the result to the user — if errors occur, share full output
See references/troubleshooting.md for common migration errors.
- 运行启动开发服务器
vinext dev - 确认服务器无错误启动
- 访问关键路由并检查功能
- 向用户反馈结果——若出现错误,分享完整输出信息
查看references/troubleshooting.md了解常见迁移错误。
Known Limitations
已知限制
| Feature | Status |
|---|---|
| Remote images via @unpic; no build-time optimization |
| CDN-loaded, not self-hosted |
| Domain-based i18n | Not supported; path-prefix i18n works |
| Not supported; use Vitest |
| Turbopack/webpack config | Ignored; use Vite plugins instead |
| Route segment configs ignored |
| PPR (Partial Prerendering) | Use |
| 功能 | 状态 |
|---|---|
| 支持通过@unpic处理远程图片;不支持构建时优化 |
| 采用CDN加载,不支持自托管 |
| 基于域名的国际化(i18n) | 不支持;路径前缀式国际化可正常使用 |
| 不支持;请使用Vitest |
| Turbopack/webpack 配置 | 会被忽略;请使用Vite插件替代 |
| 路由段配置会被忽略 |
| PPR(部分预渲染) | 请使用 |
Anti-patterns
反模式
- Do not modify ,
app/, or application code. vinext shims allpages/imports — no import rewrites needed.next/* - Do not rewrite imports to
next/*in application code. Imports likevinext/*,next/image,next/linkresolve automatically.next/server - Do not copy webpack/Turbopack config into Vite config. Use Vite-native plugins instead.
- Do not skip the compatibility check. Run before migration to surface issues early.
vinext check - Do not remove unless replacing it with
next.config.jsornext.config.ts. vinext reads it for redirects, rewrites, headers, basePath, i18n, images, and env config..mjs - Do not use or custom worker entries for bindings. Use
getPlatformProxy()instead. This is the modern pattern and works out of the box with vinext andimport { env } from "cloudflare:workers".@cloudflare/vite-plugin - For Cloudflare Workers, prefer the native integration over Nitro. /
vinext deployprovides the best experience with@cloudflare/vite-pluginbindings, KV caching, and image optimization. Nitro works for Cloudflare but the native setup is recommended.cloudflare:workers
- 请勿修改、
app/或应用代码。vinext会自动适配所有pages/导入——无需重写导入路径。next/* - 请勿将导入重写为
next/*。vinext/*、next/image、next/link等导入可自动解析。next/server - 请勿将webpack/Turbopack配置复制到Vite配置中。请使用Vite原生插件替代。
- 请勿跳过兼容性检查。迁移前运行可提前发现问题。
vinext check - 请勿删除,除非用
next.config.js或next.config.ts替代。vinext会读取该文件获取重定向、重写、请求头、basePath、国际化、图片及环境变量配置。.mjs - 请勿使用或自定义Worker入口来访问绑定。请使用
getPlatformProxy()替代。这是现代方案,可与vinext及import { env } from "cloudflare:workers"开箱即用。@cloudflare/vite-plugin - 对于Cloudflare Workers,优先选择原生集成而非Nitro。/
vinext deploy提供最佳体验,支持@cloudflare/vite-plugin绑定、KV缓存及图片优化。Nitro可用于Cloudflare,但推荐使用原生设置。cloudflare:workers