Articles



Simplify GROQ query with optional fields

May 21, 2023

πŸ•‘
about a minute read
πŸ’‘
Tip
In working with Sanity and GROQ, there might be instances where you need to filter data using an optional field, and want a default value to kick in when null values are returned. For instance, consider an __i18n field added after a significant amount of c...

Filtering data within a GROQ query

May 20, 2023

πŸ•‘
about a minute read
πŸ’‘
Tip
When dealing with multiple sets of similar data, it's not uncommon to encounter overlapping filters. Let's say you need to pull up all your articles, then categorize them into two subsets: English and French. You might think you need three separate queries...

GROQ webhooks in Sanity

May 6, 2023

πŸ•‘
about a minute read
πŸ’‘
Tip
The webhooks within Sanity CMS offer an amazingly powerful way to send data to 3rd parties when changes to content occur. However, the webhooks often require extra data retrieval to ensure the consuming service has all the necessary information for appropr...

Create a count for related items using GROQ

Oct 25, 2022

πŸ•‘
less than a minute read
πŸ’‘
Tip
I'm using the count() function of GROQ queries to display the number of posts for a tag on my tags page, but it can be used for anything that would have a varying number of items. To count items using GROQ is as simple as using the count() function. Of c...

Formatting percentage values with JavaScript Intl

Jan 5, 2021

πŸ•‘
less than a minute read
πŸ’‘
Tip
Javascript contains an Internationalisation API that you can use to format numbers to a user's locale. Setting the style to percent is all that is needed. Note: A value of 1 will return 100% because 1x anything is 100%! To specify the decimal places use ...

Formatting currency with JavaScript Intl

Dec 17, 2020

πŸ•‘
about a minute read
πŸ’‘
Tip
JavaScript's Internationalization API allows you to format currency. It takes the locale and options to configure the currency we're using. The options require the style to be set to currency and a currency option to specify the unit type, as this is retur...

Getting user locale with JavaScript

Nov 24, 2020

πŸ•‘
about a minute read
πŸ’‘
Tip
To get a user's prefered locale we can query the navigator.language. To change your language in Chrome go to chrome://settings/languages and add/edit the list of languages. To get the full list of user languages we can use navigator.languages. This retur...