Next.js 助手
- 作者仓库星标 0
- 作者仓库 skills-registry
Next.js Adapter
Next.js integration. Works with App Router and Pages Router.
oRPC also supports Server Action out of the box.
Server
// app/rpc/[[...rest]]/route.ts
import { RPCHandler } from '@orpc/server/fetch'
import { onError } from '@orpc/server'
const handler = new RPCHandler(router, {
interceptors: [onError(e => console.error(e))],
})
async function handleRequest(request: Request) {
const { response } = await handler.handle(request, {
prefix: '/rpc',
context: {},
})
return response ?? new Response('Not found', { status: 404 })
}
export const HEAD = handleRequest
export const GET = handleRequest
export const POST = handleRequest
export const PUT = handleRequest
export const PATCH = handleRequest
export const DELETE = handleRequest
Pages Router Support
// pages/api/rpc/[[...rest]].ts
import { RPCHandler } from '@orpc/server/node'
const handler = new RPCHandler(router)
export const config = {
api: { bodyParser: false },
}
export default async (req, res) => {
const { matched } = await handler.handle(req, res, {
prefix: '/api/rpc',
context: {},
})
if (matched) return
res.statusCode = 404
res.end('Not found')
}
Disable the body parser to fully utilize oRPC features like Bracket Notation.
Client
// lib/orpc.ts
import { RPCLink } from '@orpc/client/fetch'
const link = new RPCLink({
url: `${typeof window !== 'undefined' ? window.location.origin : 'http://localhost:3000'}/rpc`,
headers: async () => {
if (typeof window !== 'undefined') return {}
const { headers } = await import('next/headers')
return await headers()
},
})
Optimize SSR
// lib/orpc.ts
import type { RouterClient } from '@orpc/server'
import { RPCLink } from '@orpc/client/fetch'
import { createORPCClient } from '@orpc/client'
declare global {
var $client: RouterClient<typeof router> | undefined
}
const link = new RPCLink({
url: () => {
if (typeof window === 'undefined') {
throw new Error('RPCLink is not allowed on server')
}
return `${window.location.origin}/rpc`
},
})
export const client: RouterClient<typeof router> = globalThis.$client ?? createORPCClient(link)
// lib/orpc.server.ts
import 'server-only'
import { headers } from 'next/headers'
import { createRouterClient } from '@orpc/server'
globalThis.$client = createRouterClient(router, {
context: async () => ({ headers: await headers() }),
})
// instrumentation.ts
export async function register() {
await import('./lib/orpc.server')
}
<!-- tomevault:4.0:skill_md:2026-05-22 -->Source: ali-master/skills — distributed by TomeVault.
- 流狐分类
- 工程开发
- 作者声明 Agent
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 作者 / 版本 / 许可
- @tomevault-io · 未声明 license
- 流狐 Token 估算
- 低消耗
- 流狐接入估算
- 即装即用
- 是否需要外部 API Key
- 未发现要求
- 检测到的系统要求
- macOS · Linux · Windows
- 底层运行要求
- Node.js
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 检测到的网络行为
- 允许外网请求
- 安装命令数
- 无(仅作为资料)
档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
作者没有在当前 SKILL.md 中定义固定输出样例。 Server
Server
Pages Router Support
Disable the body parser to fully utilize oRPC features like Bracket Notation.
Client
Client
Optimize SSR
Source: ali-master/skills — distributed by TomeVault. <!-- tomevault:4.0:skillmd:2026-05-22 -->
# Next.js Adapter
[Next.js](https://nextjs.org/) integration. Works with App Router and Pages Router.
> oRPC also supports [Server Action](/docs/server-action) out of the box.
## Server
```ts
// app/rpc/[[...rest]]/route.ts
import { RPCHandler } from '@orpc/server/fetch'
import { onError } from '@orpc/server'
const handler = new RPCHandler(router, {
interceptors: [onError(e => console.error(e))],
})
async function handleRequest(request: Request) {
const { response } = await handler.handle(request, {
prefix: '/rpc',
context: {},
})
return response ?? new Response('Not found', { status: 404 })
}
export const HEAD = handleRequest
export const GET = handleRequest
export const POST = handleRequest
export const PUT = handleRequest
export const PATCH = handleRequest
export const DELETE = handleRequest
```
## Pages Router Support
```ts
// pages/api/rpc/[[...rest]].ts
import { RPCHandler } from '@orpc/server/node'
const handler = new RPCHandler(router)
export const config = {
api: { bodyParser: false },
}
export default async (req, res) => {
const { matched } = await handler.handle(req, res, {
prefix: '/api/rpc',
context: {},
})
if (matched) return
res.statusCode = 404
res.end('Not found')
}
```
> Disable the body parser to fully utilize oRPC features like [Bracket Notation](/docs/openapi/bracket-notation).
## Client
```ts
// lib/orpc.ts
import { RPCLink } from '@orpc/client/fetch'
const link = new RPCLink({
url: `${typeof window !== 'undefined' ? window.location.origin : 'http://localhost:3000'}/rpc`,
headers: async () => {
if (typeof window !== 'undefined') return {}
const { headers } = await import('next/headers')
return await headers()
},
})
```
## Optimize SSR
```ts
// lib/orpc.ts
… 证据边界与执行链路
作者原文负责流程事实;流狐只索引当前章节、要点、文件与命令。
章节 -> Server → Pages Router Support → Client → Optimize SSR
要点 -> [Next.js](https://nextjs.org/) integration. · > oRPC also supports [Server Action](/docs/server-action) out of the box. · > Disable the body parser to fully utilize oRPC features like [Bracket Notation](/docs/openapi/bracket-notation). · const link = new RPCLink({ url: ${typeof window !== 'undefined' ? · export const client: RouterClient<typeof router> = globalThis.$client ?? · --- > Source: [ali-master/skills](https://github.com/ali-master/skills) — distributed by [TomeVault](https://tomevault.io).
文件/命令 -> ${typeof window !== 'undefined' ? window.location.origin : 'http://localhost:3000'}/rpc · ${window.location.origin}/rpc · Next.js · docs/server-action · app/rpc · route.ts · orpc/server/fetch · orpc/server
内容 SHA-256 -> 9292a3b7f0d0
原文结构
适用与边界
原文中的明确线索
${typeof window !== 'undefined' ? window.location.origin : 'http://localhost:3000'}/rpc、${window.location.origin}/rpc、Next.js、docs/server-action、app/rpc、route.ts、orpc/server/fetch、orpc/server