sanity-io / visual-editing

https://visual-editing-studio.sanity.build
MIT License
36 stars 20 forks source link

`pt::text` does not produce plain text in `useQuery()` #700

Open torjusb opened 8 months ago

torjusb commented 8 months ago

Describe the bug

When using useQuery() with a query that includes the pt::text function, the useQuery will seemingly ignore the function, and just return the full object. When trying to print the result in JSX, React will crash with Objects are not valid as a React child (found: object with keys {_key, _type, children, markDefs, style})

To Reproduce

Steps to reproduce the behavior:

  1. Clone https://github.com/torjusb/sanity-nextjs-preview-pt
  2. Configure a Sanity project in .env.local, add NEXT_PUBLIC_SANITY_PROJECT_ID, NEXT_PUBLIC_SANITY_DATASET and SANITY_API_READ_TOKEN
  3. Run the project, npm install && npm run dev.
  4. Add a Post in Sanity, make sure you give it a body text.
  5. Preview the post in Presentation mode.
  6. See error

Expected behavior

The post should preview correctly, showing the body text as plain text.

Which versions of Sanity are you using?

@sanity/cli (global)  3.16.2 (latest: 3.24.1)
@sanity/image-url      1.0.2 (up to date)
@sanity/overlays       2.3.5 (latest: 2.3.6)
@sanity/react-loader   1.7.4 (up to date)
@sanity/vision        3.24.1 (up to date)
sanity                3.24.1 (up to date)

What operating system are you using?

MacOS Sonoma (14.0)

Which versions of Node.js / npm are you running?

v19.9.0

Additional context

GROQ query which will crash live preview:

export const POST_QUERY = groq`*[_type == "post" && slug.current == $slug][0] {
  ...,
  "pt": pt::text(body)
}`;
rdunk commented 8 months ago

Thanks for reporting this, I'm able to replicate it.

@stipsan I guess this is an applySourceDocuments concern?

As far as I can tell CSM will provide a mapping for values returned using a pt::text function, but the mapping type is simply value, and the mapping source will match exactly a mapping of the same field queried without pt::text. So there is no obvious context from the CSM to indicate that the value should be returned from the result rather than the cached document.

rbsam176 commented 5 months ago

I am also experiencing this issue - is there a workaround?

cody commented 5 months ago

Thanks for reporting this, I'm able to replicate it.

@cody I guess this is an applySourceDocuments concern?

As far as I can tell CSM will provide a mapping for values returned using a pt::text function, but the mapping type is simply value, and the mapping source will match exactly a mapping of the same field queried without pt::text. So there is no obvious context from the CSM to indicate that the value should be returned from the result rather than the cached document.

Howdy! Please ensure you’re tagging the correct Cody. I’m not involved with this one. Thanks!

rdunk commented 5 months ago

Howdy! Please ensure you’re tagging the correct Cody. I’m not involved with this one. Thanks!

🤦 Sorry about that!

danieljb commented 5 months ago

Hey, I am having the same issue using preview-kit in a Remix setup:

https://github.com/sanity-io/visual-editing/assets/630609/e9dd497b-ed67-471e-b233-f5fad6d4d953

Here’s a repository to reproduce the behavior shown in the video above. There’s also an open discussion on the Sanity Slack channel.