在 nextJS 13(中间件)中重定向

问题描述 投票:0回答:1

我正在处理我的中间件,我想在用户登录后重定向。问题是 api 没有达到终点并且卡在中间件部分(根据我的观察)并且页面没有重定向也是。

import { NextRequest, NextResponse } from "next/server";

export function middleware(req) {
  const url = req.url;

  if (url.includes("login")) {
    return NextResponse.redirect("localhost:3000/home", req.url);
  }

  return NextResponse.next();
}

export const config = {
  matcher: ["/", "/api/login"],
};
next.js middleware
1个回答
0
投票

所以 NextResponse.redirect 没有将字符串作为它的第二个参数。

我认为您正在寻找更像这样的东西: NextResponse.redirect(new URL('/home', req.url))

这将构造一个新的 URL 对象,这将创建 url https://localhost:3000/home。

© www.soinside.com 2019 - 2024. All rights reserved.