diff --git a/.changeset/smart-weeks-flow.md b/.changeset/smart-weeks-flow.md new file mode 100644 index 0000000000..cf3641301a --- /dev/null +++ b/.changeset/smart-weeks-flow.md @@ -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 diff --git a/packages/hygraph-dynamic-rows/lib/hygraphDynamicRows.ts b/packages/hygraph-dynamic-rows/lib/hygraphDynamicRows.ts index 1d52a95cc1..d073357759 100644 --- a/packages/hygraph-dynamic-rows/lib/hygraphDynamicRows.ts +++ b/packages/hygraph-dynamic-rows/lib/hygraphDynamicRows.ts @@ -1,9 +1,3 @@ -/** - * - Boven de product description zetten? in rowrenderer zetten - * - Hoe gaan we dit optioneel maken? - * - Hoe gaan we dit upgradebaar maken? management sdk - */ - import { HygraphPagesQuery } from '@graphcommerce/graphcms-ui' import { ApolloClient, NormalizedCacheObject } from '@graphcommerce/graphql' import { @@ -85,6 +79,8 @@ function matchCondition( return false } +type Page = HygraphPagesQuery['pages'][number] + /** * Fetch the page content for the given urls. * @@ -119,14 +115,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) @@ -142,6 +141,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 }], + }, + } }