Skip to content

Commit

Permalink
Merge branch 'padms/2657-2' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
padms committed Jan 13, 2025
2 parents f73db04 + 615661e commit da7edc4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
9 changes: 0 additions & 9 deletions web/pages/api/revalidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ export default async function handler(req, res) {
})
}

const revalidateNewsroomPages = async () => {
console.log(new Date(), 'Revalidating: /news')
res.revalidate(`/news`)
console.log(new Date(), 'Revalidating: /no/nyheter')
res.revalidate('/no/nyheter')
}

if (['page', 'landingPage', 'event'].includes(data._type)) {
const routes = await sanityClient.fetch(
groq`*[_type match "route_*" && content._ref == $id]{"slug": slug.current}`,
Expand All @@ -60,8 +53,6 @@ export default async function handler(req, res) {
console.log(new Date(), 'Revalidating: ', data?.slug)
if (data.slug) await res.revalidate(data.slug)
await revalidateHomePages()
// revalidate newsroom pages
if (data._type === 'news') await revalidateNewsroomPages()
return res.json({ revalidated: true, slug: data.slug })
}
} catch (err) {
Expand Down
40 changes: 32 additions & 8 deletions web/pages/api/update-algolia-indexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ export const config = {
},
}

const updateAlgoliaIndex = async (body) => {
const headersList = {
Accept: '*/*',
}
const response = await fetch(ALGOLIA_FUNCTION_URL, {
method: 'POST',
headers: headersList,
body,
})
return response
}

export default async function handler(req, res) {
const signature = req.headers[SIGNATURE_HEADER_NAME]
const body = (await getRawBody(req)).toString()
Expand All @@ -23,6 +35,25 @@ export default async function handler(req, res) {
return res.status(401).json({ success: false, msg: 'Unauthorized!' })
}
const data = JSON.parse(body)

const revalidateNewsroomPages = async () => {
console.log(new Date(), 'Revalidating: /news')
res.revalidate(`/news`)
console.log(new Date(), 'Revalidating: /no/nyheter')
res.revalidate('/no/nyheter')
}

if (data._type === 'news') {
// wait for news index and revalidate...
const response = updateAlgoliaIndex(body)
if (response.status == 204) {
// revalidate newsroom pages
await revalidateNewsroomPages()
return res.status(200).json('Index updated and newsroom revalidated')
} else {
return res.status(400).json(response.body)
}
}
const result = await sanityClient.fetch(
groq`*[_type match "route_*" && content._ref == $id && excludeFromSearch != true][0]{"slug": slug.current}`,
{
Expand All @@ -32,14 +63,7 @@ export default async function handler(req, res) {
try {
if (result?.slug) {
// slug exists for the topic or event
const headersList = {
Accept: '*/*',
}
const response = await fetch(ALGOLIA_FUNCTION_URL, {
method: 'POST',
headers: headersList,
body,
})
const response = updateAlgoliaIndex(body)
if (response.status == 204) {
return res.status(200).json('Index updated')
} else {
Expand Down

0 comments on commit da7edc4

Please sign in to comment.