Skip to content

Commit b5abc09

Browse files
committed
feat(worker/tasks): ✨ Add website revalidation task
1 parent 0caafd0 commit b5abc09

6 files changed

Lines changed: 61 additions & 3 deletions

File tree

apps/frontend/next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3-
import './.next/types/routes.d.ts'
3+
import './.next/dev/types/routes.d.ts'
44

55
// NOTE: This file should not be edited
66
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.

apps/worker/src/lib/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* Static constant configuration values
33
*/
44

5-
import { remove } from 'winston'
6-
75
export const config = {
86
// The number of worker threads to spawn for processing background jobs
97
workerThreadCount: 5,

apps/worker/src/lib/website.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export async function revalidateWebsitePage(
2+
paths: string[] = [],
3+
tags: string[] = [],
4+
): Promise<{ revalidated: boolean }> {
5+
try {
6+
const res = await fetch(process.env.FRONTEND_URL + '/api/revalidate', {
7+
method: 'POST',
8+
headers: {
9+
'Content-Type': 'application/json',
10+
'User-Agent': 'BuildTheEarth Worker',
11+
Accept: 'application/json',
12+
Authorization: `Bearer ${process.env.FRONTEND_SECRET}`,
13+
},
14+
body: JSON.stringify({ paths, tags }),
15+
})
16+
17+
const data = await res.json()
18+
return { revalidated: data.revalidated || res.ok }
19+
} catch (e: any) {
20+
console.error(e)
21+
return { revalidated: false }
22+
}
23+
}

apps/worker/src/tasks/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { SyncClaimOsmTask } from './claims/syncClaimOsm.task'
88
import { SendDiscordDmTask } from './discord/sendDm.task'
99
import { SendDiscordLogTask } from './discord/sendLog.task'
1010
import { SyncDiscordRolesTask } from './discord/syncDiscordRoles.task'
11+
import { RevalidateWebsitePageTask } from './website/revalidatePage.task'
1112

1213
export const taskRegistry: Record<string, BaseTask> = {}
1314

@@ -24,3 +25,4 @@ register(new PurgeVerificationsTask())
2425
register(new RemindApplicationsTask())
2526
register(new SyncClaimOsmTask())
2627
register(new SyncDiscordRolesTask())
28+
register(new RevalidateWebsitePageTask())
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Job } from 'bullmq'
2+
import { z } from 'zod'
3+
import { revalidateWebsitePage } from '../../lib/website'
4+
import { BaseTask } from '../base.task'
5+
6+
const revalidateWebsitePagePayloadSchema = z.object({
7+
paths: z.array(z.string()).optional(),
8+
tags: z.array(z.string()).optional(),
9+
})
10+
11+
type RevalidateWebsitePagePayload = z.infer<typeof revalidateWebsitePagePayloadSchema>
12+
13+
/**
14+
* This task sends a revalidation request to the frontend server for specific website pages. It can either revalidate specific paths or whole tags.
15+
* @summary Revalidates specific website pages
16+
*/
17+
export class RevalidateWebsitePageTask extends BaseTask<typeof revalidateWebsitePagePayloadSchema> {
18+
readonly name = 'REVALIDATE_WEBSITE'
19+
readonly schema = revalidateWebsitePagePayloadSchema
20+
21+
async execute(data: RevalidateWebsitePagePayload, job: Job) {
22+
const { paths, tags } = data
23+
24+
if (paths && paths.length < 1 && tags && tags.length < 1) {
25+
return
26+
}
27+
28+
const { revalidated } = await revalidateWebsitePage(paths, tags)
29+
30+
if (!revalidated) {
31+
throw new Error('Failed to revalidate website page')
32+
}
33+
}
34+
}

packages/shared/src/utils/redis/redisEvents.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export enum RedisEvent {
77
BUILDTEAM_WEBHOOK = 'BUILDTEAM_WEBHOOK',
88
SYNC_CLAIM_OSM = 'SYNC_CLAIM_OSM',
99
SYNC_DISCORD_ROLES = 'SYNC_DISCORD_ROLES',
10+
REVALIDATE_WEBSITE = 'REVALIDATE_WEBSITE',
1011
}
1112

1213
/**

0 commit comments

Comments
 (0)