logo

该视频仅会员有权观看

立即开通课程「Next.js 前端应用开发实践」权限。

¥
199
/ 年

在中间件里用 NextResponse 重定向(redirect)请求

  • The middleware in Next.js allows the use of next response to redirect requests to a specific URL or rewrite responses, as well as set cookies and headers.
  • To test redirection within the middleware, check if the path matches /articles. If it does, use the next response to redirect.
  • When redirecting, it's important to provide an absolute URL, not just a relative path (e.g., http://localhost:3000/post instead of just /post).
  • The redirect method can take a string or URL object as an argument.
  • To preserve query strings (like ?sort=latest) in the redirected URL, provide the redirect method with a URL object, which can be constructed by cloning the current request URL and modifying its pathname before passing it to next response.