Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twelve-moles-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Make metafields optional within the ProductProvider. Fixes #1127
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function ProductProvider({
data: product,
initialVariantId,
}: ProductProviderProps) {
const metafields = useParsedMetafields(product.metafields);
const metafields = useParsedMetafields(product.metafields || {});

// @ts-expect-error The types here are broken on main, need to come back and fix them sometime
const providerValue = useMemo<ProductContextType>(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function useParsedMetafields(
/** A [MetafieldConnection](https://shopify.dev/api/storefront/reference/common-objects/metafieldconnection). */
metafields?: PartialDeep<MetafieldConnection>
) {
metafields?.edges?.[0]?.node;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure what this was doing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return useMemo(() => {
if (!metafields) {
throw new Error(`'useParsedMetafields' needs metafields`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function ProductPrices() {
export default function ProductDetails({product}) {
const initialVariant = flattenConnection(product.variants)[0];

const productMetafields = useParsedMetafields(product.metafields);
const productMetafields = useParsedMetafields(product.metafields || {});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the default product details page shows metafields, maybe we do want it to error?

const sizeChartMetafield = productMetafields.find(
(metafield) =>
metafield.namespace === 'my_fields' && metafield.key === 'size_chart',
Expand Down