Watch a live product demo 👀 See how Sanity powers richer commerce experiences

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– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing