NextJS在getServerSideProps内部重定向时发送数据

uinbv5nw  于 2022-11-29  发布在  其他
关注(0)|答案(1)|浏览(136)

是否有可能在getServerSideProps函数中以类似于next.config.js的方式使用重定向发送数据(据我所知,您不能在下一个配置文件中传递隐藏的查询)。

export const getServerSideProps = async (context) => {
    const id = context.params.id;
    
    return {
        redirect: {
            destination: '/my-work',
            permanent: false,
            has: [
                {
                    type: 'query',
                    value: id
                }
            ]
        },
        props: {
                
        }
    }
}

我想通过隐藏查询到另一个页面,所以这只作为中间件重定向工作,因为我是从电子邮件模板进入这个页面。但有对象是不工作的getServerSideProps功能。
有没有其他方法可以做到这一点?
谢谢你的帮助!

o3imoua4

o3imoua41#

这是官方文件。

module.exports = {
  async redirects() {
    return [
      // if the header `x-redirect-me` is present,
      // this redirect will be applied
      {
        source: '/:path((?!another-page$).*)',
        has: [
          {
            type: 'header',
            key: 'x-redirect-me',
          },
        ],
        permanent: false,
        destination: '/another-page',
      },
      // if the source, query, and cookie are matched,
      // this redirect will be applied
      {
        source: '/specific/:path*',
        has: [
          {
            type: 'query',
        key: 'page',
        // the page value will not be available in the
        // destination since value is provided and doesn't
        // use a named capture group e.g. (?<page>home)
        value: 'home',
      },
      {
        type: 'cookie',
        key: 'authorized',
        value: 'true',
      },
    ],
    permanent: false,
    destination: '/another/:path*',
  },
  // if the header `x-authorized` is present and
  // contains a matching value, this redirect will be applied
  {
    source: '/',
    has: [
      {
        type: 'header',
        key: 'x-authorized',
        value: '(?<authorized>yes|true)',
      },
    ],
    permanent: false,
    destination: '/home?authorized=:authorized',
  },
  // if the host is `example.com`,
  // this redirect will be applied
  {
    source: '/:path((?!another-page$).*)',
    has: [
      {
        type: 'host',
        value: 'example.com',
      },
    ],
    permanent: false,
    destination: '/another-page',
  },
]

}、}
您可以将参数与它进行比较。有关详细信息,请访问here

相关问题