fix: cache
This commit is contained in:
parent
66bb076485
commit
21590cd101
6 changed files with 32 additions and 3 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import { CollectionConfig } from 'payload'
|
import { CollectionConfig } from 'payload'
|
||||||
import { isAdminOrEmployee } from '@/collections/access/admin'
|
import { isAdminOrEmployee } from '@/collections/access/admin'
|
||||||
import { canMutateOccurrenceOfAssignedEvent } from '@/collections/access/assigned'
|
import { canMutateOccurrenceOfAssignedEvent } from '@/collections/access/assigned'
|
||||||
|
import { revalidateTagHook } from '@/utils/revalidate'
|
||||||
|
|
||||||
export const EventOccurrences: CollectionConfig = {
|
export const EventOccurrences: CollectionConfig = {
|
||||||
slug: 'eventOccurrence',
|
slug: 'eventOccurrence',
|
||||||
|
|
@ -91,4 +92,8 @@ export const EventOccurrences: CollectionConfig = {
|
||||||
update: canMutateOccurrenceOfAssignedEvent(),
|
update: canMutateOccurrenceOfAssignedEvent(),
|
||||||
delete: isAdminOrEmployee(),
|
delete: isAdminOrEmployee(),
|
||||||
},
|
},
|
||||||
|
hooks: {
|
||||||
|
afterChange: [revalidateTagHook('events')],
|
||||||
|
afterDelete: [revalidateTagHook('events')],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import {
|
||||||
unassignedAdditions,
|
unassignedAdditions,
|
||||||
} from '@/collections/access/assigned'
|
} from '@/collections/access/assigned'
|
||||||
import { regenerateOccurrencesForEvent } from '@/jobs/generateEventOccurrences'
|
import { regenerateOccurrencesForEvent } from '@/jobs/generateEventOccurrences'
|
||||||
|
import { revalidateTagHook } from '@/utils/revalidate'
|
||||||
|
|
||||||
export const Events: CollectionConfig = {
|
export const Events: CollectionConfig = {
|
||||||
slug: 'event',
|
slug: 'event',
|
||||||
|
|
@ -431,6 +432,8 @@ export const Events: CollectionConfig = {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
revalidateTagHook('events'),
|
||||||
],
|
],
|
||||||
beforeDelete: [
|
beforeDelete: [
|
||||||
async ({ id, req }) => {
|
async ({ id, req }) => {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { CollectionConfig } from 'payload'
|
import { CollectionConfig } from 'payload'
|
||||||
|
import { revalidateTagHook } from '@/utils/revalidate'
|
||||||
import {
|
import {
|
||||||
canCreateAssignedWorship,
|
canCreateAssignedWorship,
|
||||||
canMutateAssignedWorship,
|
canMutateAssignedWorship,
|
||||||
|
|
@ -151,4 +152,8 @@ export const Worship: CollectionConfig = {
|
||||||
update: canMutateAssignedWorship(),
|
update: canMutateAssignedWorship(),
|
||||||
delete: canMutateAssignedWorship(),
|
delete: canMutateAssignedWorship(),
|
||||||
},
|
},
|
||||||
|
hooks: {
|
||||||
|
afterChange: [revalidateTagHook('worship')],
|
||||||
|
afterDelete: [revalidateTagHook('worship')],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ const getUpcomingOccurrences = unstable_cache(
|
||||||
}) as Promise<PaginatedDocs<EventOccurrence>>
|
}) as Promise<PaginatedDocs<EventOccurrence>>
|
||||||
},
|
},
|
||||||
['fetchUpcomingOccurrences'],
|
['fetchUpcomingOccurrences'],
|
||||||
{ revalidate: CACHE_TTL },
|
{ tags: ['events'], revalidate: CACHE_TTL },
|
||||||
)
|
)
|
||||||
|
|
||||||
export async function fetchUpcomingOccurrences(
|
export async function fetchUpcomingOccurrences(
|
||||||
|
|
@ -146,7 +146,7 @@ const getPastOccurrences = unstable_cache(
|
||||||
}) as Promise<PaginatedDocs<EventOccurrence>>
|
}) as Promise<PaginatedDocs<EventOccurrence>>
|
||||||
},
|
},
|
||||||
['fetchPastOccurrences'],
|
['fetchPastOccurrences'],
|
||||||
{ revalidate: CACHE_TTL },
|
{ tags: ['events'], revalidate: CACHE_TTL },
|
||||||
)
|
)
|
||||||
|
|
||||||
export async function fetchPastOccurrences(
|
export async function fetchPastOccurrences(
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ const getWorship = unstable_cache(
|
||||||
}) as Promise<PaginatedDocs<Worship>>
|
}) as Promise<PaginatedDocs<Worship>>
|
||||||
},
|
},
|
||||||
['fetchWorship'],
|
['fetchWorship'],
|
||||||
{ revalidate: CACHE_TTL },
|
{ tags: ['worship'], revalidate: CACHE_TTL },
|
||||||
)
|
)
|
||||||
|
|
||||||
export const fetchWorship = async (
|
export const fetchWorship = async (
|
||||||
|
|
|
||||||
16
src/utils/revalidate.ts
Normal file
16
src/utils/revalidate.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { revalidateTag } from 'next/cache'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Payload hook that invalidates a Next cache tag.
|
||||||
|
*
|
||||||
|
* Safe to use for docs created/deleted programmatically (e.g. by
|
||||||
|
* regenerateMassesForChurch / regenerateOccurrencesForEvent), since the
|
||||||
|
* Local API runs collection hooks. The jobs queue can execute outside a
|
||||||
|
* Next request scope where revalidateTag throws — swallow that; the
|
||||||
|
* fetch-side TTL covers the gap there.
|
||||||
|
*/
|
||||||
|
export const revalidateTagHook = (tag: string) => () => {
|
||||||
|
try {
|
||||||
|
revalidateTag(tag)
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue