🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Error after updating to Sanity 3.4.0, fixed by changing import from rxjs to rxjs/operators.

14 replies
Last updated: Feb 2, 2023
Guys, I have a question, in version 2 I obtained my dataset in this way, now how can I obtain it?
import client from "part:@sanity/base/client";

const { dataset } = client.config();
Feb 1, 2023, 10:01 PM
I could be wrong, but my understanding is that you will set this up in a `sanity.config.js`file at the root of your project.
Strongly recommend that you setup a sample project that uses V3 of the Studio. There are a few to choose from, it takes 2 minutes, and will be incredibly useful as you inspect how different things are done.
Feb 1, 2023, 10:10 PM
Feb 1, 2023, 10:12 PM
Yes, I think I have it perfect, but in another file I need to have the value of the dataset that I have selected, for example staging or production, in order to show a preview with the url corresponding to the dataset.
Feb 1, 2023, 10:15 PM
You can access the client in a few ways. The cheat sheet goes over them!
Feb 1, 2023, 11:17 PM
user M
Is it possible to use this, in a file other than a component?
Feb 1, 2023, 11:29 PM
Good question! This should work for you.
Feb 1, 2023, 11:36 PM
👀
Feb 1, 2023, 11:37 PM
I get this error 😞
Feb 1, 2023, 11:38 PM
Can you share your code?
Feb 1, 2023, 11:39 PM
import { getCliClient } from "sanity/cli";

const previewSecret = "SANITY_PREVIEW_SECRET";

// Replace `remoteUrl` with your deployed <https://www.sanity.io/nextjs-cms|Next.js> site
const remoteUrl = `<https://site.netlify.app>`;
const localUrl = `<http://localhost:3000>`;
const stagingUrl = `<https://staging--site.netlify.app/>`;

const getBaseURL = () => {
  const dataset = "staging";
  const hostname = window.location.hostname;

  if (hostname === "localhost") {
    return localUrl;
  }

  if (dataset === "production") {
    return remoteUrl;
  }

  return stagingUrl;
};

export default function resolveProductionUrl(doc) {
  const baseUrl = getBaseURL();
  const previewUrl = new URL(baseUrl);

  const client = getCliClient({ apiVersion: "2023-1-03" });

  console.log(client);

  previewUrl.pathname = `/api/preview`;
  previewUrl.searchParams.append(`secret`, previewSecret);
  previewUrl.searchParams.append(`type`, doc?._type);
  previewUrl.searchParams.append(`slug`, doc?.slug?.current ?? `/`);

  return previewUrl.toString();
}
Feb 1, 2023, 11:42 PM
Oh in a case like this, the client is provided to you inside of the context that’s passed as the second argument. This is how you’d want to get your client in this case.
Feb 1, 2023, 11:49 PM
👀
Feb 2, 2023, 12:23 AM
It works perfect, thanks for saving my life 😃🙌
Feb 2, 2023, 12:30 AM
Haha, you’re welcome 🙂
Feb 2, 2023, 6:02 PM

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?