diff --git a/scripts/sync-algolia.mjs b/scripts/sync-algolia.mjs index ceb491f0..d79e95c7 100644 --- a/scripts/sync-algolia.mjs +++ b/scripts/sync-algolia.mjs @@ -11,9 +11,23 @@ if (!adminApiKey) { process.exit(0); } +// Preview builds (pull requests) re-index the whole site on every push and +// burn through the Algolia operation quota; only sync production deploys. +const branch = process.env.CF_PAGES_BRANCH; +if (branch && branch !== "main") { + console.log(`Algolia sync skipped: preview build (branch ${branch})`); + process.exit(0); +} + const content = await readFile("dist/public/algolia-index.json", "utf8"); const documents = JSON.parse(content); const client = algoliasearch(appId, adminApiKey); -await sync(client, { indexName, documents }); -console.log(`Algolia sync: wrote ${documents.length} documents to ${indexName}`); +// A search-index hiccup must never block publishing the docs themselves. +try { + await sync(client, { indexName, documents }); + console.log(`Algolia sync: wrote ${documents.length} documents to ${indexName}`); +} catch (error) { + console.warn(`Algolia sync failed, deploying without re-indexing search: ${error}`); + process.exit(0); +}