Skip to content

Commit

Permalink
Dynamic rows would break page rendering if there was a dynamic row bu…
Browse files Browse the repository at this point in the history
…t no page returned
  • Loading branch information
paales committed Jan 29, 2024
1 parent 2b5c442 commit a93c312
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-weeks-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphcommerce/hygraph-dynamic-rows": patch
---

Dynamic rows would break page rendering if there was a dynamic row but no page returned
27 changes: 24 additions & 3 deletions packages/hygraph-dynamic-rows/lib/hygraphDynamicRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function matchCondition(
return false
}

type Page = HygraphPagesQuery['pages'][number]

/**
* Fetch the page content for the given urls.
*
Expand Down Expand Up @@ -119,14 +121,17 @@ export async function hygraphDynamicRows(

const [pageResult, dynamicResult] = await Promise.all([pageQuery, dynamicRows])

const page = pageResult.data.pages[0] as Page | undefined

// Create a copy of the content array.
const content = pageResult.data.pages[0]?.content ?? []
const content = page?.content ?? []

dynamicResult?.data.dynamicRows.forEach((dynamicRow) => {
const { placement, target, rows, row } = dynamicRow
if (!rows && !row) return

const rowsToMerge = rows.length > 0 ? rows : [row]
const rowsToMerge = rows
if (row && rows.length === 0) rowsToMerge.push(row)

if (!target) {
if (placement === 'BEFORE') content.unshift(...rowsToMerge)
Expand All @@ -142,6 +147,22 @@ export async function hygraphDynamicRows(

if (!content.length) return pageResult

const dynamicPage: Page = {
id: 'dynamic-page',
__typename: 'Page',
metaRobots: 'INDEX_FOLLOW',
metaTitle: '',
metaDescription: '',
url: '',
content: [],
relatedPages: [],
}

// Return the merged page result.
return { data: { ...pageResult.data, pages: [{ ...pageResult.data.pages[0], content }] } }
return {
data: {
...pageResult.data,
pages: [{ ...dynamicPage, ...page, content }],
},
}
}

0 comments on commit a93c312

Please sign in to comment.