How to Display a Shopify Query in Next.js

12 replies
Last updated: May 24, 2022
Hello all,
I have this query from shopify in sanity:

"previewImageUrl": "<https://cdn.shopify.com/s/files/1/0646/5971/8386/products/CB003SCR_02-600x600-removebg-preview.png?v=1653374204>",
but how do I display this in Nextjs?
before I used to display it like so:

<img src={urlFor(product[0].image).width(200)} />
but this needs a different approach. Does anyone here have a idea on how to do this?

Also the I don't know how to let this query automaticly update price. for example if I update the price on shopify it updates it in Sanity but how should the query be targeted?
AI Update

Looking at your two questions:

1. Displaying Shopify Image URLs in Next.js

Since the previewImageUrl from Shopify is already a CDN URL (not a Sanity image reference), you don't need to use urlFor() - that's only for Sanity-hosted images.

Simply use it directly as an image source:

<img src={product[0].previewImageUrl} width={200} alt={product[0].title} />

Or with Next.js Image component:

import Image from 'next/image'

<Image 
  src={product[0].previewImageUrl} 
  width={200} 
  height={200}
  alt={product[0].title} 
/>

Note: If using Next.js <Image>, you'll need to add cdn.shopify.com to your next.config.js image domains:

module.exports = {
  images: {
    domains: ['cdn.shopify.com'],
  },
}

2. Automatically Updating Prices from Shopify

The price updates depend on how you've configured Sanity Connect for Shopify:

If using Direct Sync:

  • Go to your Sanity Connect settings in Shopify
  • Make sure "Sync data automatically" is enabled under "When to synchronize"
  • Prices will automatically sync whenever you update products in Shopify
  • The synced data includes the priceRange field on product documents

In your GROQ query, access the price like this:

*[_type == "shopify.product"] {
  _id,
  title,
  "image": store.previewImageUrl,
  "price": store.priceRange.minVariantPrice,
  "variants": store.variants[]-> {
    store {
      price,
      compareAtPrice
    }
  }
}

The store object contains all the Shopify-synced data, including pricing that updates automatically when you change prices in Shopify.

Important: Be aware that inventory changes (sales) also trigger sync updates. For high-traffic stores, you may want to consider using a custom sync handler to control how frequently price updates occur and manage API CDN cache invalidation.

Looking at your first code snippet (which is not a query btw), it seems that you have a resolved image URL? So you can just pass that as your image source. The image URL builder is only used when you have an unresolved reference to an asset.
Right, I took this from Sanity raw json inspect. I should target this differntly so price would also be automaticly updated when I change this in Shopify.
It seems like two very different issues though.
Yea it is but Im a bit in conflict with how to do it the right way. I cant seem the find a good example yet
What is the schema for that
previewImageUrl
field?
If I do this:
const query = `*[_type == "product"]{
 ...
}`;
it will out put this:

  {
    _createdAt: '2022-05-23T12:22:52Z',
    _id: 'shopifyProduct-7662284964082',
    _rev: 'ylikTovbLjtSuRLGA50n67',
    _type: 'product',
    _updatedAt: '2022-05-24T08:38:27Z',
    store: {
      createdAt: '2022-05-20T08:58:33+02:00',
      id: 7662284964082,
      isDeleted: false,
      options: [Array],
      previewImageUrl: '<https://cdn.shopify.com/s/files/1/0646/5971/8386/products/CB003SCR_02-600x600-removebg-preview.png?v=1653374204>',
      priceRange: [Object],
      productType: 'Sanitair',
      slug: [Object],
      status: 'active',
      tags: '',
      title: 'CB003S',
      updatedAt: '2022-05-24T10:38:18+02:00',
      variants: [Array]
    }
  }
]
But Im missing the sku for example in there.
It proably is inside the slug:[object] but I dont know how to target that specifiek array
Right, so you can just pass
product.store.previewImageUrl
to the
src
prop of your image. It’s a resolved URL already, so you don’t need the image builder. The URL builder is only needed when you have an unresolved reference to an asset object.
Ah yea that works for the image this is great and simple. And do you know how to target the sku thats inside the slug [object]?
slug.current
🙂
You are amazing !
Let's say I now want to target variant 2 for this product.
<h1>{product[1].store.slug.current}</h1>
throws me an error

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?