Next.js 重写和 HTTP 代理中间件有什么区别?

What’s the difference between Next.js rewrites and HTTP proxy middleware?

只是想找出 Next.js 重写和使用 http-proxy-middleware 设置代理之间的区别。我有一个 Next.js 项目,在 API 中设置了一些代理,想知道我是否可以换掉代理以进行重写。

有什么区别(如果有的话)?有什么我想念的吗?

rewrites 是代理请求的便捷方式,无需在服务器中设置您自己的逻辑 - Next.js 会为您处理。

就像 http-proxy-middleware 一样,它们允许您将传入请求路径映射到不同的目的地。主要区别是rewrites也适用于client-side路由,当使用Next.js'built-in路由器(通过next/linknext/router)到在页面之间导航。

来自 rewrites 文档:

Rewrites act as a URL proxy and mask the destination path, making it appear the user hasn't changed their location on the site.

(...) Rewrites are applied to client-side routing, a <Link href="/about"> will have the rewrite applied in the above example.