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:

&lt;img src={urlFor(product[0].image).width(200)} /&gt;
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?
May 24, 2022, 8:53 AM
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.
May 24, 2022, 9:05 AM
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.
May 24, 2022, 9:08 AM
It seems like two very different issues though.
May 24, 2022, 9:09 AM
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
May 24, 2022, 9:10 AM
What is the schema for that
previewImageUrl
field?
May 24, 2022, 9:11 AM
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.
May 24, 2022, 9:12 AM
It proably is inside the slug:[object] but I dont know how to target that specifiek array
May 24, 2022, 9:13 AM
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.
May 24, 2022, 9:13 AM
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]?
May 24, 2022, 9:16 AM
slug.current
🙂
May 24, 2022, 9:17 AM
You are amazing !
May 24, 2022, 9:18 AM
Let's say I now want to target variant 2 for this product.
&lt;h1&gt;{product[1].store.slug.current}&lt;/h1&gt;
throws me an error
May 24, 2022, 9:18 AM

Sanity.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.