fix(templates): uses context to prevent infinite loop in populateArchiveBlock (#4278)
This commit is contained in:
@@ -2,7 +2,7 @@ import type { AfterReadHook } from 'payload/dist/collections/config/types'
|
||||
|
||||
import type { Page, Product } from '../payload-types'
|
||||
|
||||
export const populateArchiveBlock: AfterReadHook = async ({ doc, req: { payload } }) => {
|
||||
export const populateArchiveBlock: AfterReadHook = async ({ doc, context, req: { payload } }) => {
|
||||
// pre-populate the archive block if `populateBy` is `collection`
|
||||
// then hydrate it on your front-end
|
||||
|
||||
@@ -16,10 +16,13 @@ export const populateArchiveBlock: AfterReadHook = async ({ doc, req: { payload
|
||||
}>
|
||||
}
|
||||
|
||||
if (archiveBlock.populateBy === 'collection') {
|
||||
if (archiveBlock.populateBy === 'collection' && !context.isPopulatingArchiveBlock) {
|
||||
const res: { totalDocs: number; docs: Product[] } = await payload.find({
|
||||
collection: archiveBlock?.relationTo || 'products',
|
||||
limit: archiveBlock.limit || 10,
|
||||
context: {
|
||||
isPopulatingArchiveBlock: true,
|
||||
},
|
||||
where: {
|
||||
...((archiveBlock?.categories?.length || 0) > 0
|
||||
? {
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { AfterReadHook } from 'payload/dist/collections/config/types'
|
||||
|
||||
import type { Page, Post } from '../payload-types'
|
||||
|
||||
export const populateArchiveBlock: AfterReadHook = async ({ doc, req: { payload } }) => {
|
||||
export const populateArchiveBlock: AfterReadHook = async ({ doc, context, req: { payload } }) => {
|
||||
// pre-populate the archive block if `populateBy` is `collection`
|
||||
// then hydrate it on your front-end
|
||||
|
||||
@@ -16,10 +16,13 @@ export const populateArchiveBlock: AfterReadHook = async ({ doc, req: { payload
|
||||
}>
|
||||
}
|
||||
|
||||
if (archiveBlock.populateBy === 'collection') {
|
||||
if (archiveBlock.populateBy === 'collection' && !context.isPopulatingArchiveBlock) {
|
||||
const res: { totalDocs: number; docs: Post[] } = await payload.find({
|
||||
collection: archiveBlock.relationTo,
|
||||
limit: archiveBlock.limit || 10,
|
||||
context: {
|
||||
isPopulatingArchiveBlock: true,
|
||||
},
|
||||
where: {
|
||||
...(archiveBlock?.categories?.length > 0
|
||||
? {
|
||||
|
||||
Reference in New Issue
Block a user