Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions scripts/sync-algolia.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}