Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
torjusb opened this issue Jan 16, 2024 · 5 comments
Open

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

torjusb opened this issue Jan 16, 2024 · 5 comments

Comments

@torjusb
Copy link

torjusb commented Jan 16, 2024

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
Copy link
Member

rdunk commented Jan 16, 2024

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
Copy link

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

@cody
Copy link

cody commented Apr 15, 2024

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
Copy link
Member

rdunk commented Apr 15, 2024

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

🤦 Sorry about that!

@danieljb
Copy link

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

remix-sanity-preview-kit.mov

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants