将标题发送到客户端哨兵后无法设置标题

发布于 2025-01-24 20:20:00 字数 476 浏览 1 评论 0原文

我在此代码上从sentry.io那里遇到了一个错误,我告诉我,我无法在之后设置标题 他们被发送给客户。 这是下一个JS应用

export async function getServerSideProps(ctx) {
  const { res } = ctx

  res.setHeader('Content-Type', 'text/plain')
  res.write('User-agent: *\n')
  res.write('Disallow: \n')
  res.write('Sitemap: https://kalla.com/sitemap.xml\n')
  res.end()

  return {}
}

const Robots = () => null

export default Robots

I get an error from sentry.io on this code, i's tell me that I can't set headers after
they are sent to the client.
It's a next js app

export async function getServerSideProps(ctx) {
  const { res } = ctx

  res.setHeader('Content-Type', 'text/plain')
  res.write('User-agent: *\n')
  res.write('Disallow: \n')
  res.write('Sitemap: https://kalla.com/sitemap.xml\n')
  res.end()

  return {}
}

const Robots = () => null

export default Robots

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2025-01-31 20:20:00

响应标头仅通过请求发送一次,如果您尝试将其设置并再次发送,则会出现错误。
writehead()可以与setheader()合并,但是writehead()优先于后者。
writehead()acepts以下args:writehead(statuscode,statusmessage,标题),但是接受setheader(键,值)。

 export async function getServerSideProps(ctx) {
  const { res } = ctx

  res.setHeader('Content-Type', 'text/plain')
  res.setHeader('User-agent','*\n')
  res.setHeader('Disallow', '\n')
  res.setHeader('Sitemap', 'https://kalla.com/sitemap.xml\n')
  res.end()

  return {}
}

 const Robots = () => null

 export default Robots

Response header is only sent once with a request, and if you try to set it and send it again you will get an error.
writeHead() can be merged with setHeader(), but the writeHead() has precedence over the latter.
WriteHead() acepts these args :writeHead(statusCode, statusMessage, headers), however accepts setHeader(key, value).

 export async function getServerSideProps(ctx) {
  const { res } = ctx

  res.setHeader('Content-Type', 'text/plain')
  res.setHeader('User-agent','*\n')
  res.setHeader('Disallow', '\n')
  res.setHeader('Sitemap', 'https://kalla.com/sitemap.xml\n')
  res.end()

  return {}
}

 const Robots = () => null

 export default Robots
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文