Skip to content

Commit 5dcbc7c

Browse files
author
Alex Patterson
committed
allow large files in algolia
1 parent 4df2960 commit 5dcbc7c

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

‎app/api/algolia/route.tsx‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ if (!algoliaIndex) throw "Missing NEXT_PUBLIC_ALGOLIA_INDEX";
1313
const algolia = algoliasearch(algoliaAppId, algoliaAdminApiKey);
1414
const index = algolia.initIndex(algoliaIndex);
1515

16+
function toAlgoliaObject(sanityDoc: any) {
17+
const doc = { ...sanityDoc };
18+
delete doc.content;
19+
20+
return sanityDoc?.content
21+
// loop through each block
22+
?.map((block: any, i: number) => {
23+
// if it's not a text block with children,
24+
// return nothing
25+
if (block._type !== 'block' || !block.children) {
26+
return {
27+
...doc,
28+
objectID: `${doc._id}-${i}`
29+
}
30+
}
31+
// loop through the children spans, and join the
32+
// text strings
33+
return {
34+
...doc,
35+
objectID: `${doc._id}-${i}`,
36+
content: block.children.map((child: any) => child.text).join('')
37+
}
38+
})
39+
}
40+
1641
export async function POST(request: Request) {
1742
if (!secret)
1843
return Response.json(
@@ -43,7 +68,6 @@ export async function POST(request: Request) {
4368
// ...,
4469
// "objectID":_id,
4570
// "slug": slug.current,
46-
// "content": pt::text(content),
4771
// "guest": null,
4872
// "spotify": null,
4973
// "created": select(before() == null && after() != null => _id),
@@ -59,12 +83,13 @@ export async function POST(request: Request) {
5983
delete sanityDoc.deleted;
6084
delete sanityDoc.updated;
6185

86+
const algoliaDoc = toAlgoliaObject(sanityDoc);
87+
6288
try {
6389
if (created) {
64-
await index.saveObject(sanityDoc).wait();
90+
await index.saveObjects(algoliaDoc).wait();
6591
} else if (updated) {
66-
// await index.partialUpdateObject(sanityDoc).wait();
67-
await index.saveObject(sanityDoc).wait();
92+
await index.saveObjects(algoliaDoc).wait();
6893
} else {
6994
await index.deleteObject(sanityDoc.objectID).wait();
7095
}

0 commit comments

Comments
 (0)