首页 > 解决方案 > 如何使用 getServerSideProps 将用户重定向到另一个页面?

问题描述

我找到了这个解决方案:

export const getServerSideProps = async ({ res }) => {
  res.redirect(301, '/another-page')
  
  return {
    props: {}
  }
}

但我没有看到redirect方法res并得到一个错误

标签: reactjsreact-routernext.js

解决方案


好像您正在使用 Next.js。尝试这样的事情。

export const getServerSideProps = async ({ res }) => {
  res.setHeader("location", "/another-page")
  res.statusCode = 301
  res.end()
  return;
}

推荐阅读