【问题标题】:next.js redirect on getServerSideProps throwing error about propsnext.js 在 getServerSideProps 上重定向,抛出关于道具的错误
【发布时间】:2021-08-04 01:40:04
【问题描述】:

如果我的表达失败,我只是想从这个页面重定向到另一个页面。我阅读了文档,完全按照他们的做法做了,在网上看了他们所做的,我每次都得到这个。

Error: Additional keys were returned from `getServerSideProps`. Properties intended for your component must be nested under the `props` key, e.g.:

    return { props: { title: 'My Title', content: '...' } }

Keys that need to be moved: redirect.
Read more: https://err.sh/next.js/invalid-getstaticprops-value
This error happened while generating the page. Any console logs will be displayed in the terminal window.

这是我在页面内的代码

export async function getServerSideProps(req) {
  if(req){
    return {
      redirect: {
        permanent: false,
        destination: "/"
      }
    }
  }
}

【问题讨论】:

    标签: reactjs next.js


    【解决方案1】:

    根据您的代码,您希望始终临时重定向到基本 URL。如果是这样的话,在next.config.js中定义逻辑是有意义的

    module.exports = {
      async redirects() {
        return [
          {
            source: '/your-page', // whatever your page is
            destination: '/',
            permanent: false,
          },
        ]
      },
    }
    

    参考

    https://nextjs.org/docs/api-reference/next.config.js/redirects

    【讨论】:

    • 我试过了,没用。我正在使用 9.5 - 将尝试升级到 10
    • @noobCoder 我忘了问版本。是的,在 Next.js 10 中引入了 getServerSideProps 上的重定向。nextjs.org/blog/next-10#redirect-support
    猜你喜欢
    • 2021-04-23
    • 2023-02-10
    • 1970-01-01
    • 2022-08-20
    • 2021-07-03
    • 2021-11-11
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多