Request context for Sitecore Search React SDK
Ensure proper context for search requests in a Sitecore XM solution - the subQuery problem
Sitecore SearchNextJSReactSitecore JSS
| Reading Time: 3 Minutes, 30 Seconds
2024-06-04
When using Sitecore Search all requests need several information as context for the search request.
-
Widget Id (rfkid) is required, in the backend each “widget” is configured. Here business rules can be applied such as boosting, blacklisting etc. It is also used for analytics so you can get much more detailed and focused data about search usage.
-
User Context is required for personalization. It is actually managed automatically from cookies. You might want to annotate a used identifier during login such as an id or even email, Facebook id or similar. When making any server side requests you can use a ser context by providing the
user.uuid
property. -
Locale context is required, when managing pages with Sitecore Search it can be enough to provide a
page.uri
. We are mostly using a CMS for the website, often Sitecore XM/XP/DXP, so - instead of creating each uri in Sitecore search - we provide thelocale.country
andlocale.language
properties. Both are required, as localization in Sitecore Search is based on the full locale with both language and country.
Setting Locale context
We need to provide country and language on each request - and they need to match the ones created in Sitecore Search…
We usually also have full locale on the website as there will often arise business requirements eg. products available on certain markets, multiple languages on a specific market, language differences across markets, etc. However, it also happens that we have applications with only a market dimension. It is then necessary to provide a country when interacting with Sitecore Search, so in the underlying system/codebase we actually match a language to a country, even though it is not exposed to the users of the application.
Search React SDK
When using the Search React SDK you can easy and fast build search experiences and there are plenty of customization options. Things like translations, suggestions blocks, and highlighting needs to be added anyway and it is simple to add context here also.
Eg. when using the useSearchResults
hook, the code in the search result list can look like this:
|
|
When using Sitecore JSS (either Sitecore XM Cloud or Sitecore DXP) for creating headless solutions, the country and language can be fetched from the context with this snippet:
|
|
Language and country is always lowercase in Sitecore Search, while in the Sitecore CMS context the country code is (usually) uppercase and matching the underlying CultureInfo
so we ensure it is lowercase.
If there are routes without country, you will of course need to handle it so it will be a bit more complex than the code above.
The subquery problem
When using PreviewSearch the widget itself can actually create additional requests when hovering suggestions. Those queries are creating internally in the SDK by cloning the main query with a useWidgetSubQuery
hook. However, that cloning doesn’t take the locale context into account, so the requests will fail as bad requests.
Luckily, there actually is a solution for this, I was just not aware of it. Instead of specifying locale on each and every we can specify this globally by using PageController
.
|
|
So, instead we have in the search-provider.ts:
|
|
The PageController
has actually been available in the React SDK from the beginning (at least back to version 1.2.0 I checked) so this approach should be available on all implementations.
The initial request is created by the PageController, now including the locale context, and when the subQuery hook clone procedure updates with properties from the parent query, the locale context is kept.
I got the help to use this approach with the help of Sitecore Support, so a great thank you.