NextJS:仅限开发页面

Ceq*_*iel 5 html hidden next.js

我想创建一个/storybook页面。但此页面仅用于开发目的。我的意思是,我不希望在生产模式下访问此页面。

我们怎样才能做到这一点?

And*_*eng 13

我不确定执行此操作的最佳方法是什么。我认为你有几种方法。

使用 Next.js 9.5+

您可以使用它rewrite来确保流量/storybook会转到您的 404 页面。

// next.config.js
// more configs
  async rewrites() {
    return [
      {
        source: '/storybook',
        destination: process.env.NODE_ENV === 'production' ? '/404' : '/storybook',
      }
    ];
  },
// more configs
Run Code Online (Sandbox Code Playgroud)

使用 Next.js 10+

notFound您可以在您的getServerSideProps或中使用该属性getStaticProps。有关更多信息,您可以在此处找到该文档

// or getServerSideProps
export function getStaticProps() {
  return {
    // returns the default 404 page with a status code of 404 in production
     notFound: process.env.NODE_ENV === 'production'
  }
}
Run Code Online (Sandbox Code Playgroud)

使用自定义服务器

取决于服务器框架,您可以在那里重定向到 404。