html - NextJS : dev only pages

标签 html next.js hidden

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

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

最佳答案

我不确定最好的方法是什么。我认为您有几种方法。

使用 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

使用 Next.js 10+

您可以在 getServerSidePropsgetStaticProps 中使用 notFound 属性。更多信息,您可以找到doc here

// 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'
  }
}

使用自定义服务器

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

关于html - NextJS : dev only pages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66319803/

相关文章:

html - CSS中div的可扩展宽度

javascript - 名称已存在正则表达式

javascript - Bootstrap Carousel 不工作(一次显示所有图像)

firebase-hosting - 如何在 Firebase 托管上部署 next.js 应用程序?

javascript - NextJs Link 组件包装图像在映射数组时会导致错误,除非在顶级页面组件中使用

javascript - 使用 JavaScript 和 HTML5 显示隐藏图像(与现有帖子相关)?

css - 绝对定位菜单无法在 IE8 的固定标题中显示

ios - UITableView 在 IOS 7 中可见,但在 IOS 6 中不可见

HTML,使用相对 URL 作为 BASE URL?

javascript - 在 NextJS 中将 CSS 模块类声明为变量