How to set response status in Next.js app page - Stack Overflow

I have a next.js app with app directory.├── app│ └── (public)│ ├── path1│ │ └──

I have a next.js app with app directory

.
├── app/
│   └── (public)/
│       ├── path1/
│       │   └── page.tsx
│       └── path2/
│           └── page.tsx
├── layout.tsx
└── page.tsx

I want to /path2 return 404 status (not a redirect)

export async function getStaticProps() {
    return {
        notFound: true, // triggers 404
    };
}

export default function Path2Page() {
  return (
      <></>
  );
}

And Error: × "getStaticProps" is not supported in app/. Read more:

My solution atm is set status in middleware, but is there another method?

export function middleware(request: NextRequest) {


  if (request.nextUrl.pathname === '/path2') {
    return NextResponse.next({status: 404});
  }

  return NextResponse.next();

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744961494a4603411.html

相关推荐

  • How to set response status in Next.js app page - Stack Overflow

    I have a next.js app with app directory.├── app│ └── (public)│ ├── path1│ │ └──

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信