最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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();

}
发布评论

评论列表(0)

  1. 暂无评论