跳到主要内容

IndexNow 开发者使用指南

什么是IndexNow?

IndexNow是一个免费的开放协议,让网站快速通知搜索引擎内容变化,提高索引速度。支持Bing、Yandex等搜索引擎。

为什么需要IndexNow?

传统方式的问题

  • 搜索引擎需要时间发现新页面
  • 更新内容后索引延迟
  • 重要页面可能被忽略

IndexNow的优势

  • 实时通知:立即告诉搜索引擎内容变化
  • 提高效率:减少爬虫请求,节省服务器资源
  • 免费使用:无需付费,简单配置即可

快速开始

1. 生成API密钥

访问 IndexNow官网 生成密钥:

# 示例密钥(请使用自己的)
your-api-key-here

2. 创建密钥文件

在网站根目录创建密钥文件:

# 文件名:your-api-key-here.txt
# 内容:your-api-key-here
echo "your-api-key-here" > public/your-api-key-here.txt

3. 验证密钥文件

确保密钥文件可通过 https://yourdomain.com/your-api-key-here.txt 访问。

实现方式

方式1:GET请求(单个URL)

// 提交单个URL
const url = 'https://yourdomain.com/new-page';
const key = 'your-api-key-here';
const apiUrl = `https://api.indexnow.org/indexnow?url=${encodeURIComponent(url)}&key=${key}`;

fetch(apiUrl)
.then(response => {
if (response.ok) {
console.log('URL提交成功');
}
});

方式2:POST请求(批量URL)

// 批量提交URL
const urls = [
'https://yourdomain.com/page1',
'https://yourdomain.com/page2'
];

const requestBody = {
host: 'yourdomain.com',
key: 'your-api-key-here',
urlList: urls
};

fetch('https://api.indexnow.org/indexnow', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => {
if (response.ok) {
console.log('批量提交成功');
}
});

方式3:Node.js脚本

// submit-urls.js
const https = require('https');

async function submitUrl(url, key) {
const apiUrl = `https://api.indexnow.org/indexnow?url=${encodeURIComponent(url)}&key=${key}`;

return new Promise((resolve, reject) => {
https.get(apiUrl, (res) => {
if (res.statusCode === 200 || res.statusCode === 202) {
console.log(`✅ 提交成功: ${url}`);
resolve(true);
} else {
console.log(`❌ 提交失败: ${url} (${res.statusCode})`);
resolve(false);
}
}).on('error', reject);
});
}

// 使用示例
submitUrl('https://yourdomain.com/new-page', 'your-api-key-here');

最佳实践

✅ 推荐做法

  1. 只提交变化的URL

    // 新页面发布时
    submitUrl('https://yourdomain.com/new-article', apiKey);

    // 重要内容更新时
    submitUrl('https://yourdomain.com/updated-guide', apiKey);
  2. 优先使用单个URL提交

    • 更可靠,错误处理更简单
    • 符合IndexNow设计理念
  3. 合理使用频率

    • 不要频繁提交相同URL
    • 避免过度依赖IndexNow

❌ 避免做法

  1. 批量提交所有页面

    // 不推荐:提交没有变化的页面
    const allPages = getAllPages();
    submitBulkUrls(allPages, apiKey);
  2. 定期自动提交

    // 不推荐:定时提交所有页面
    setInterval(() => {
    submitAllPages();
    }, 24 * 60 * 60 * 1000); // 每天
  3. 过度依赖IndexNow

    • 搜索引擎会自动发现页面
    • IndexNow只是加速工具,不是必需

集成到工作流

1. 部署后自动提交

// 在CI/CD流程中
async function deployAndSubmit() {
// 1. 部署网站
await deployWebsite();

// 2. 提交新页面
const newPages = getNewPages();
for (const page of newPages) {
await submitUrl(page, process.env.INDEXNOW_KEY);
}
}

2. 内容管理系统集成

// 在CMS发布流程中
async function publishArticle(article) {
// 1. 保存文章
await saveArticle(article);

// 2. 生成页面
const pageUrl = generatePage(article);

// 3. 提交到IndexNow
await submitUrl(pageUrl, INDEXNOW_KEY);
}

3. 监控和错误处理

async function submitWithRetry(url, key, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
const success = await submitUrl(url, key);
if (success) return true;
} catch (error) {
console.log(`尝试 ${i + 1} 失败: ${error.message}`);
if (i < maxRetries - 1) {
await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1)));
}
}
}
return false;
}

常见问题

Q: 提交后多久生效?

A: 通常几分钟到几小时,取决于搜索引擎的索引周期。

Q: 可以提交多少URL?

A: 每次最多10,000个URL,但建议只提交真正变化的URL。

Q: 密钥文件丢失怎么办?

A: 重新生成密钥文件并部署到网站根目录。

Q: 支持哪些搜索引擎?

A: 目前支持Bing、Yandex等,Google正在考虑中。

Q: 需要付费吗?

A: 完全免费,无需任何费用。

监控效果

1. 搜索引擎工具

  • Bing Webmaster Tools: 查看URL提交状态
  • Yandex Webmaster: 监控索引情况

2. 搜索验证

# 在搜索引擎中搜索
site:yourdomain.com "页面标题"

3. 日志监控

// 记录提交日志
function logSubmission(url, success, statusCode) {
console.log(`${new Date().toISOString()} - ${url} - ${success ? 'SUCCESS' : 'FAILED'} (${statusCode})`);
}

总结

IndexNow是一个简单有效的工具,可以显著提高网站的索引速度。关键是要:

  1. 正确配置:密钥文件必须可访问
  2. 合理使用:只提交变化的URL
  3. 保持简单:优先使用单个URL提交
  4. 监控效果:定期检查提交状态

记住:IndexNow是加速工具,不是必需功能。专注于内容质量,合理使用IndexNow,效果会更好。


相关链接