Skip to content

Commit

Permalink
The ProductFilters and ProductList queries would only run after the h…
Browse files Browse the repository at this point in the history
…ygraphPageContent query would be resolved, although they don't depend on each other, now they run in parallel.
  • Loading branch information
paales committed Jan 5, 2024
1 parent 9091dbb commit 89b58be
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-moles-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/magento-product': patch
---

The ProductFilters and ProductList queries would only run after the hygraphPageContent query would be resolved, although they don't depend on each other, now they run in parallel.
51 changes: 21 additions & 30 deletions examples/magento-graphcms/pages/[...url].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,38 +166,29 @@ export const getStaticProps: GetPageStaticProps = async ({ params, locale }) =>
}

const pages = hygraphPageContent(staticClient, url, category)
const hasPage = filteredCategoryUid ? false : (await pages).data.pages.length > 0
const hasCategory = Boolean(productListParams && categoryUid)

if (!productListParams || !(hasPage || hasCategory))
return redirectOrNotFound(staticClient, conf, params, locale)

if (!hasCategory) {
return {
props: {
...(await categoryPage).data,
...(await pages).data,
...(await layout).data,
apolloState: await conf.then(() => client.cache.extract()),
},
revalidate: 60 * 20,
}
}
const filters = hasCategory
? staticClient.query({
query: ProductFiltersDocument,
variables: { filters: { category_uid: { eq: categoryUid } } },
})
: undefined
const products = hasCategory
? staticClient.query({
query: ProductListDocument,
variables: {
pageSize: (await conf).data.storeConfig?.grid_per_page ?? 24,
...productListParams,
filters: { ...productListParams?.filters, category_uid: { eq: categoryUid } },
},
})
: undefined

const filters = staticClient.query({
query: ProductFiltersDocument,
variables: { filters: { category_uid: { eq: categoryUid } } },
})
const products = staticClient.query({
query: ProductListDocument,
variables: {
pageSize: (await conf).data.storeConfig?.grid_per_page ?? 24,
...productListParams,
filters: { ...productListParams.filters, category_uid: { eq: categoryUid } },
},
})
const hasPage = filteredCategoryUid ? false : (await pages).data.pages.length > 0
if (!hasCategory && !hasPage) return redirectOrNotFound(staticClient, conf, params, locale)

if ((await products).errors) return { notFound: true }
if ((await products)?.errors) return { notFound: true }

const { category_name, category_url_path } =
(await categoryPage).data.categories?.items?.[0]?.breadcrumbs?.[0] ?? {}
Expand All @@ -210,9 +201,9 @@ export const getStaticProps: GetPageStaticProps = async ({ params, locale }) =>
const result = {
props: {
...(await categoryPage).data,
...(await products).data,
...(await products)?.data,
...(await pages).data,
...(await filters).data,
...(await filters)?.data,
...(await layout).data,
filterTypes: await filterTypes,
params: productListParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function parseParams(
query: string[],
filterTypes: FilterTypes,
search: string | null = null,
): ProductListParams | false {
): ProductListParams | undefined {
const categoryVariables: ProductListParams = { url, filters: {}, sort: {}, search }

const typeMap = filterTypes
Expand Down Expand Up @@ -59,7 +59,7 @@ export function parseParams(
return undefined
}, undefined)

return error ? false : categoryVariables
return error ? undefined : categoryVariables
}

export function extractUrlQuery(params?: { url: string[] }) {
Expand Down

0 comments on commit 89b58be

Please sign in to comment.