rdunk / sanity-blocks-vue-component

[DEPRECATED] Vue component for rendering block content from Sanity
MIT License
71 stars 10 forks source link

Allow for empty or undefined value #5

Closed silvio-e closed 3 years ago

silvio-e commented 5 years ago

Hey! :)

Would it make sense to avoid an error when retrieving an empty or undefined value for :blocks?

I have the situation that the speaker.description could be accidentally empty because sanity doesn't have a default or empty value for empty fields:

<BlockContent
  :blocks="speaker.description"
/>

Which would lead in nuxt.js to a server error.

I could of course check like:

<BlockContent
  v-if="speaker.description"
  :blocks="speaker.description"
/>

But I would like to avoid doing so everywhere and everytime I use the component. Looking forward to your feedback!

rdunk commented 5 years ago

Would the empty value of speaker.description be undefined, null, {} or [] in your example?

I would say in the case of undefined or null, then an error should be thrown as technically the prop value type is incorrect, and conditionally rendering the component with v-if would be the correct approach.

If either {} or [] are being passed as the prop value, then an error should probably not be thrown, if this is the case let me know and I'll take a proper look.

silvio-e commented 5 years ago

Thanks for your reply!

Yes, speaker.description could be undefined as Sanity wouldn't deliver a default value. So that would mean two things we can do to prevent that but that would be so much repetition all over our projects:

1. Solution

Setting always an empty value with correct type for the prop:

*[...]{
  ...,
  "description": coalesce(descritption, [])
}

But writing that in every query I'm not looking forward to.

2. Solution

Like I wrote above, checking with `v-if="speaker.description".

But also that would be in every place we use it.

If we forget one of these points a content editor could accidentally break the page when she forgets/ignores a content field.

So therefore I thought it would be handy if an undefined could be "ignored". :)

rdunk commented 5 years ago

Could this be solved with the following (untested)?

<BlockContent :blocks="speaker.description || []" />

If not, I will give some more thought to ignoring undefined, but it does seem to me that the conditional rendering approach is the 'proper' way to do things here.