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

Expanding body in Groq selector breaks ability to do source mapping #747

Open
rijk opened this issue Jan 24, 2024 · 2 comments
Open

Expanding body in Groq selector breaks ability to do source mapping #747

rijk opened this issue Jan 24, 2024 · 2 comments

Comments

@rijk
Copy link
Contributor

rijk commented Jan 24, 2024

In my custom components, I use encodeDataAttribute to map them to blocks. For example:

<div data-sentry={encodeDataAttribute(['body', _key, 'image', 'image'])}>
   <img ... />
</div>

This works, as long as the Groq query is simple, like:

*[_type == "page" && slug.current == $slug][0]

As soon as the query gets more complex (which is needed to expand references), the encodeDataAttribute breaks:

*[_type == "page" && slug.current == $slug][0]{...} // still works…
*[_type == "page" && slug.current == $slug][0]{..., body} // still works…
*[_type == "page" && slug.current == $slug][0]{..., body[] {...}} // from here, it breaks

Even though nothing actually changes in the document structure returned by the query (I've diffed the two JSON objects and they are identical).

Which versions of Sanity are you using?

% sanity versions
@sanity/cli (global)        3.25.0 (latest: 3.26.0)
@sanity/image-url            1.0.2 (up to date)
@sanity/overlays             2.3.7 (up to date)
@sanity/preview-url-secret   1.5.2 (latest: 1.5.3)
@sanity/react-loader         1.7.6 (up to date)

What operating system are you using?

macOS

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

% npm -v
10.2.3
% node -v
v18.19.0
@rijk
Copy link
Contributor Author

rijk commented Jan 24, 2024

The problem is in the difference in sourceMap I think.

Simple query

sourceMap for simple query (*[_type == "page" && slug.current == $slug][0]{..., body}):

image

Expanded query

sourceMap for expanded query (*[_type == "page" && slug.current == $slug][0]{..., body[]{...}}):

image image

One could argue if mapping all the paths for each block is necessary/a good thing, but most importantly what is lost here is this mapping:

image

@rijk rijk changed the title Custom Groq selector breaks ability to do visual mapping Expanding body in Groq selector breaks ability to do source mapping Jan 24, 2024
@rijk
Copy link
Contributor Author

rijk commented Jan 24, 2024

I've worked around it for now by overwriting the mappings and paths — no idea if this is ill advised, but it seems to work:

const { data, sourceMap } = useQuery<Page | null>(PAGE_QUERY, params, { initial })
const encodeDataAttribute = useEncodeDataAttribute(
  data,
  {
    documents: sourceMap?.documents ?? [],
    // Overwrite the mappings because they break visual editing when you start expanding the body
    // array (see https://github.com/sanity-io/visual-editing/issues/747)
    mappings: {
      $: {
        source: { document: 0, path: 0, type: 'documentValue' },
        type: 'value',
      },
    },
    paths: ['$'],
  },
  client.config().stega.studioUrl
)

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

1 participant