You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Next.js项目中添加自定义X-Robots-Tag请求头?

合并Next.js配置以添加X-Robots-Tag请求头

直接将headers异步方法合并到你现有的nextConfig对象中即可,以下是完整的配置文件:

/** @type {import('next').NextConfig} */

const webpack=(config)=> {
  config.module.rules.push({
    test: /\.svg$/i,
    issuer: /\.[jt]sx?$/,
    use: ['@svgr/webpack'],
  })

  return config
}
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  webpack: webpack,
  images: {
    domains: ['images.unsplash.com','images.prismic.io'],
  },
  // 新增的headers配置
  async headers() {
    return [
      {
        source: '/:path*',
        headers: [
          {
            key: 'X-Robots-Tag',
            value: 'noindex',
          },
        ],
      },
    ];
  },
}

module.exports = nextConfig

说明

  • 原有的webpack配置、图片域名、React严格模式等设置全部保留
  • 新增的headers方法会为全站所有路径(/:path*匹配规则)返回X-Robots-Tag: noindex响应头,实现阻止搜索引擎收录的需求
  • 若仅需给特定路径添加该头,可修改source字段的匹配规则(比如/about/:path*仅匹配关于页下的所有路径)

内容的提问来源于stack exchange,提问作者Zaki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 04:40:25