fix(templates): uses context to prevent infinite loop in populateArchiveBlock (#4278)

This commit is contained in:
Tylan Davis
2023-11-27 10:47:03 -05:00
committed by GitHub
parent ad62db01e7
commit cbeb0a8bc7
2 changed files with 10 additions and 4 deletions

View File

@@ -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
? {

View File

@@ -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
? {