MuseMVP 文档
权限控制

Middleware VS Proxy

为什么模板保留 middleware.ts,以及如何在非 Vercel 部署时迁移为 proxy.ts。

MuseMVP 默认保留 src/middleware.ts,目标是兼容多部署环境并保持请求入口逻辑最小化。

为什么使用 middleware.ts 而不是 proxy.ts

  • 模板需要兼容多部署目标(如 Vercel 与 Cloudflare Workers 与 Node.js),保留 middleware.ts 作为统一入口更稳妥。
  • 当前中间件只处理请求入口层能力(如 locale 路由、rewrite/redirect、matcher 过滤),不承载业务鉴权。
  • 使用统一入口可降低平台差异带来的维护成本与回归风险。

边界原则

Middleware 只做请求形态处理;登录态和业务跳转放在 layout/page 或服务端逻辑中。


如果不部署在 Cloudflare Workers 或 Node.js,可自行改为 proxy.ts(附实例 Prompt)

迁移时建议保持行为不变,只调整文件约定:

  • middleware.ts 重命名为 proxy.ts
  • 导出函数名从 middleware 改为 proxy
  • 保留原有 matcher 与 rewrite/redirect 顺序

可直接复制下面 Prompt 给 AI(并在末尾粘贴你的 middleware.ts 代码):

I'm working in a Next.js (latest version) project.
All other code already follows the latest Next.js conventions.

I currently have a middleware.ts file, but in the latest version this should be implemented as proxy.ts.

Please:

Convert the following middleware.ts logic into the correct proxy.ts format.

Ensure the implementation follows the latest Next.js best practices.

Adjust any API differences if necessary.

Keep the behavior exactly the same.

Assume the project uses the App Router.

Here is my current middleware.ts:

// paste middleware.ts code here

Return:

The complete proxy.ts file

A short explanation of any necessary changes