diff --git a/website/deploy/deploy.mjs b/website/deploy/deploy.mjs index 21b5944a..07e1b5a1 100644 --- a/website/deploy/deploy.mjs +++ b/website/deploy/deploy.mjs @@ -103,15 +103,39 @@ export async function verifyPublished(manifestBytes, { fetcher = fetch, origin = return paths.length + 1; } +// 这次新增或内容变了的页面(笔记的静态页面、学习站每道题的页面),按 IndexNow 提交给必应等搜索引擎; +// 验证文件是构建写在根目录的 <32 位十六进制>.txt。提交失败不影响发布,只写进报告 +export function changedPages(manifest, previous) { + const before = new Map(((previous && previous.files) || []).map(file => [file.path, file.sha256])); + return manifest.files.filter(file => /^(notes\/[^/]+\.html|notes\/index\.html|learn\/q\/[^/]+\.html)$/.test(file.path) && before.get(file.path) !== file.sha256) + .map(file => ORIGIN + '/' + file.path.replace(/index\.html$/, '').split('/').map(encodeURIComponent).join('/')); +} +export async function submitIndexNow(directory, manifest, previous, fetcher) { + const keyFile = manifest.files.find(file => /^[0-9a-f]{32}\.txt$/.test(file.path)); + const urls = changedPages(manifest, previous).slice(0, 10000); + if (!keyFile || !urls.length) return { submitted: 0 }; + const key = keyFile.path.slice(0, 32); + try { + const response = await fetcher('https://api.indexnow.org/IndexNow', { + method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }, signal: AbortSignal.timeout(30_000), + body: JSON.stringify({ host: new URL(ORIGIN).host, key, keyLocation: `${ORIGIN}/${key}.txt`, urlList: urls }), + }); + return { submitted: urls.length, status: response.status }; + } catch (error) { + return { submitted: 0, error: error instanceof Error ? error.message : '提交失败' }; + } +} + export async function deploySite({ directory, archive = resolve(directory, '../.cache/site-release.zip'), getClient, fetcher = fetch, sleep = ms => new Promise(done => setTimeout(done, ms)), attempts = 12 }) { const manifestBytes = await readFile(resolve(directory, 'asset-manifest.json')); const manifest = JSON.parse(manifestBytes); verificationPaths(manifest); // Reject local defects before any credential or production mutation. - let unchanged = false; + let unchanged = false, previous = null; try { const remote = await download(ORIGIN, 'asset-manifest.json', sha256(manifestBytes), fetcher); unchanged = sha256(remote) === sha256(manifestBytes); + previous = JSON.parse(remote); } catch { /* A missing manifest should trigger a new deployment, never a false skip. */ } let deployment; if (!unchanged) { @@ -141,7 +165,8 @@ export async function deploySite({ directory, archive = resolve(directory, '../. await sleep(20_000); } } - return { projectId: PROJECT.projectId, origin: ORIGIN, + const indexNow = unchanged ? null : await submitIndexNow(directory, manifest, previous, fetcher); + return { projectId: PROJECT.projectId, origin: ORIGIN, ...(indexNow ? { indexNow } : {}), sourceCommit: manifest.sourceCommit, generatorHash: manifest.generatorHash, manifestSha256: sha256(manifestBytes), result: unchanged ? 'unchanged' : 'deployed', diff --git a/website/deploy/deploy.test.mjs b/website/deploy/deploy.test.mjs index e70865a1..c1b8abac 100644 --- a/website/deploy/deploy.test.mjs +++ b/website/deploy/deploy.test.mjs @@ -7,7 +7,7 @@ import { tmpdir } from 'node:os'; import { join, resolve } from 'node:path'; import { promisify } from 'node:util'; import { fileURLToPath } from 'node:url'; -import { deploySite, isTransient, PROJECT } from './deploy.mjs'; +import { changedPages, deploySite, isTransient, PROJECT } from './deploy.mjs'; const executeFile = promisify(execFile); async function saveManifest(directory, files, sourceCommit = 'source') { @@ -114,6 +114,31 @@ test('腾讯云接口的网络错误重试,令牌和权限错误不重试', as assert.equal(isTransient(new Error('腾讯云部署未成功:Failed')), false); }); +test('IndexNow:只提交新增或改动的静态页面,失败不影响发布', async t => { + const files = [{ path: 'notes/Java 容器.html', sha256: 'b' }, { path: 'notes/MySQL.html', sha256: 'same' }, { path: 'notes/index.html', sha256: 'n' }, + { path: 'learn/q/abc.html', sha256: 'q' }, { path: 'notes/Java 容器.md', sha256: 'm' }, { path: '0123456789abcdef0123456789abcdef.txt', sha256: 'k' }]; + const previous = { files: [{ path: 'notes/Java 容器.html', sha256: 'a' }, { path: 'notes/MySQL.html', sha256: 'same' }, { path: 'notes/index.html', sha256: 'n' }] }; + assert.deepEqual(changedPages({ files }, previous), ['https://cyc2018.xyz/notes/Java%20%E5%AE%B9%E5%99%A8.html', 'https://cyc2018.xyz/learn/q/abc.html']); + const { directory, files: built } = await fixture(t); + built.set('notes/new.html', Buffer.from('new page')); + built.set('0123456789abcdef0123456789abcdef.txt', Buffer.from('0123456789abcdef0123456789abcdef')); + for (const [path, data] of built) await writeFile(join(directory, path), data); + await saveManifest(directory, built); + let published = false, sent = null; + const report = await deploySite({ directory, attempts: 1, sleep: async () => {}, + fetcher: async (url, init) => { + if (String(url).startsWith('https://api.indexnow.org/')) { sent = JSON.parse(init.body); throw new TypeError('fetch failed'); } + return published ? new Response(built.get(decodeURIComponent(url.pathname.slice(1)))) : new Response('{}'); + }, + getClient: async () => ({ projects: { get: async () => PROJECT }, deployments: { deploy: async () => { published = true; return { status: 'Success', deploymentId: 'd' }; } } }), + }); + assert.equal(report.result, 'deployed'); + assert.deepEqual(sent.urlList, ['https://cyc2018.xyz/notes/new.html']); + assert.equal(sent.key, '0123456789abcdef0123456789abcdef'); + assert.equal(report.indexNow.submitted, 0); + assert.match(report.indexNow.error, /fetch failed/); +}); + test('项目身份不符时拒绝发布', async t => { const { directory } = await fixture(t); await assert.rejects(deploySite({ directory, fetcher: async () => new Response('{}'),