📆 Don't miss our Sanity Showcase virtual meetup on March 20th!

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?
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.
<h1>{product[1].store.slug.current}</h1>
throws me an error
May 24, 2022, 9:18 AM

Sanity– build remarkable experiences at scale

Sanity is a 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.

Was this answer helpful?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Hey all - what’s the quickest way to batch delete all documents of a certain type? It looks like the `sanity` cli tool allows...Jan 29, 2021
Rendering nested block contentJan 18, 2021
In the tutorial, there is samples of what the API returns as JSON. Is there a way to view these ?Jan 11, 2021
Warning: Data for Page Exceeds the Threshold of 128 kBAug 17, 2022
List Arrays - Is it Possible to Fill a List Array Within a Schema Asynchronously?Mar 16, 2021
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
How to get the number of nested/children components in a GROQ query.Jan 19, 2024
TypeError: Cannot Read Properties of Null (Reading 'jsonType')Sep 28, 2022
Error in custom validation rule for schema in Slack threadAug 18, 2023
Issue with fetching data from an array of references and non-references in a groq query.Dec 14, 2023

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle