Skip to content

Commit

Permalink
feat: add revalidation on demand to webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierniki committed Sep 11, 2023
1 parent 4ba4b4a commit 9e38bed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/[lang]/article/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type ArticlePageProps = { params: { lang: Locale; slug: string } }

export async function generateMetadata({ params: { lang, slug } }: ArticlePageProps): Promise<Metadata | null> {
const { getArticleSummary } = HygraphClient(lang)
const { articles } = await getArticleSummary({ slug }, { next: { revalidate: 60 } })
const { articles } = await getArticleSummary({ slug })
const article = articles[0]

if (!article) return null
Expand Down
4 changes: 3 additions & 1 deletion src/app/api/algolia-webhook/publish/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { algoliaClient } from "../algoliaClient"
import { errorToNextResponse } from "../httpError"
import { NextRequestWithValidBody, validateBody } from "../validateBody"
import { validateSignature } from "../validateSignature"
import { revalidatePath } from "next/cache"

async function handleAlgoliaPublishWebhook(req: NextRequestWithValidBody<z.infer<typeof bodySchema>>) {
const article = req.validBody.data
Expand All @@ -17,9 +18,10 @@ async function handleAlgoliaPublishWebhook(req: NextRequestWithValidBody<z.infer
objectID: article.id,
title,
content: slateToText(content),
slug: slug,
slug,
})

revalidatePath(`/${locale}/article/${slug}`)
return { title, locale }
})
)
Expand Down
7 changes: 5 additions & 2 deletions src/app/api/algolia-webhook/unpublish/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import { algoliaClient } from "../algoliaClient"
import { errorToNextResponse } from "../httpError"
import { NextRequestWithValidBody, validateBody } from "../validateBody"
import { validateSignature } from "../validateSignature"
import { revalidatePath } from "next/cache"

async function handleAlgoliaUnpublishWebhook(req: NextRequestWithValidBody<z.infer<typeof bodySchema>>) {
const article = req.validBody.data

const indexingResults = await Promise.allSettled(
article.localizations.map(async ({ locale }) => {
article.localizations.map(async ({ locale, slug }) => {
const index = algoliaClient.initIndex(`articles-${locale}`)
await index.deleteObject(article.id)

revalidatePath(`/${locale}/article/${slug}`)

return { locale }
})
)
Expand All @@ -31,7 +34,7 @@ export async function POST(req: NextRequest) {

const bodySchema = z.object({
data: z.object({
localizations: z.array(z.object({ locale: z.string() })),
localizations: z.array(z.object({ locale: z.string(), slug: z.string() })),
id: z.string(),
}),
})

0 comments on commit 9e38bed

Please sign in to comment.