网页
综合
更新时间 2026-11-06 00:00:00
Proxy
自动蜘蛛
nextjs.org
正文约 2216 字
Starting with Next.js 16, Middleware is now called Proxy to better reflect its purpose. The functionality remains the same.
Proxy allows you to run code before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly.
Some common scenarios where Proxy is effective include:
For simple redirects, consider using the redirects configuration in next.config.ts first. Proxy should be used when you need access to request data or more complex logic.
Proxy is not intended for slow data fetching. While Proxy can be helpful for optimistic checks such as permission-based redirects, it should not be used as a full session management or authorization solution.
Directives such as use cache and use server have no effect in Proxy, because Proxy does not apply directive transforms.
fetch options such as cache, next.revalidate, and next.tags also do not cache responses. To reuse cached data in Proxy, use an external cache or data store. Alternatively, move the work to a supported context, such as a Server Component or Route Handler.
Create a proxy.ts (or .js) file in the project root, or inside src if applicable, so that it is located at the same level as pages or app.
While only one proxy.ts file is supported per project, you can still organize your proxy logic into modules. Break out proxy functionalities into separate .ts or .js files and import them into your main proxy.ts file. This allows for cleaner management of route-specific proxy, aggregated in the proxy.ts for centralized control. By enforcing a single proxy file, it simplifies configuration, prevents potential conflicts, and optimizes performance by avoiding multiple proxy layers.
You can export your proxy function as either a default export or a named proxy export:
The matcher config allows you to filter Proxy to run on specific paths. See the Matcher documentation for more details on path matching.
Read more about using proxy, or refer to the proxy API reference.
Learn more about Proxy
Last updated November 6, 2026
Subscribe to our newsletter
Stay updated on new releases and features, guides, and case studies.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。