Skip to content

Commit

Permalink
feat: algolia 10kb制限の対応
Browse files Browse the repository at this point in the history
  • Loading branch information
ikmnjrd committed Mar 9, 2024
1 parent 40568a1 commit 06ae9eb
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/scripts/algolia.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import getPostFilesData from '~/utils/getPostFilesData'
import { index } from '~/utils/algoriaClient'

function bytes2(str: string) {
return encodeURIComponent(str).replace(/%../g, 'x').length
}

export const sendAlgoliaIndex = async (): Promise<void> => {
const blogData = await getPostFilesData()
const blogSubset = blogData.map((blog) => {
const { slug, frontmatter, content } = blog
// 無料プランでは1つあたり10KBまでしかたいおうしてくれない
// https://www.algolia.com/doc/guides/sending-and-managing-data/prepare-your-data/in-depth/index-and-records-size-and-usage-limitations/#record-size-limits
const contentByte = bytes2(content)
const slugByte = bytes2(slug)
const fmByte = bytes2(JSON.stringify(frontmatter))

const MAX_BYTE = 10000
const sum = contentByte + slugByte + fmByte

if (sum > MAX_BYTE) {
const hoge = content.slice(0, sum - MAX_BYTE)
return {
slug,
content: hoge,
...frontmatter,
}
}

return {
slug,
content,
Expand Down

0 comments on commit 06ae9eb

Please sign in to comment.