SitecoreAI - siteInfo is closed (and what to do instead)

On a multi-site SitecoreAI implementation I needed a handful of per-site settings on the front end. A few scalars, a couple of links. The kind of thing every multi-site project ends up needing.
The site already has a Settings item, so I added a base template to the JSS Settings template, added my fields, authored the values and published. Then I tried to read them through siteInfo. That cost me an afternoon.
Why I was convinced this would work
Look at what siteInfo already gives you:
query {
site {
siteInfo(site: "mysite") {
robots
sitemap
llmsTxt
}
}
}
robots, sitemap and llmsTxt are all authored on the site's Settings item. So the mental model writes itself: siteInfo exposes the Settings item, so a field added to the Settings item shows up on siteInfo.
It doesn't:
query {
site {
siteInfo(site: "mysite") {
myCustomField
}
}
}
Cannot query field 'myCustomField' on type 'SiteInfo'.
Not a typo, not an unpublished item, not a caching issue. I checked all of those. Twice.
SiteInfo is a type, not your Settings item
SiteInfo is a fixed GraphQL type. Every field on it - robots, sitemap, llmsTxt, dictionary, redirects, errorHandling, routes, rootPath - is a resolver that Sitecore wrote and ships as part of the product. There is no generic mapping underneath that reads whatever fields your Settings template happens to have.
So adding a field to the Settings template changes the item, not the type. A new field on siteInfo is a Sitecore release, not a configuration change.
The Edge schema is template-driven, but only for item(…) and search(…). That's where your settings have to come from.
Reading your settings as ordinary content
The catch: your front end knows the site name, and item(…) wants a path or an ID. And you can't chain those in one GraphQL request, the output of one root field can't be the input of another. So it's two calls.
Call one gets the site root. rootPath is one of the fields siteInfo does have:
query SiteRoot($siteName: String!) {
site {
siteInfo(site: $siteName) {
rootPath
}
}
}
Call two reads the settings beneath it. I keep each group of settings in its own template, as a child item of the site's Settings item. With children(includeTemplateIDs: …), an inline fragment per template and an alias per group, that's still only one request for all of them:
query SiteSettings($path: String!, $language: String!) {
item(path: $path, language: $language) {
social: children(includeTemplateIDs: ["{YOUR-SOCIAL-SETTINGS-TEMPLATE-ID}"]) {
results {
... on SocialSettings {
facebookUrl { jsonValue }
linkedInUrl { jsonValue }
}
}
}
newsletter: children(includeTemplateIDs: ["{YOUR-NEWSLETTER-SETTINGS-TEMPLATE-ID}"]) {
results {
... on NewsletterSettings {
enabled { jsonValue }
signUpPage { jsonValue }
}
}
}
}
}
...with $path being ${rootPath}/Settings.
Two calls is not a problem
I felt bad about the extra round-trip for a while. Then I looked at what the Content SDK does itself: dictionary, redirects and error pages are each a separate service making a separate call behind a cache. Same design, so I'm in good company.
And you can cache both calls:
rootPathonly changes when somebody restructures the content tree. Cache it for the lifetime of the process, and call one practically disappears.- The settings change when an editor changes them. The SDK's own
MemoryCacheClient, with the same timeout as the dictionary cache, works fine there.
Wiring it up in Angular
In our Angular Content SDK head app this is two small functions behind a store, resolved once from an app initializer. Components just read a setting from the store and don't know two requests happened underneath. Two things cost me time there:
- The GraphQL client throws on any error in the response, even when there is valid
datanext to it. So one settings field that fails to resolve itsjsonValuetakes down every group in the request. The partial data is still on the error (error.response.data), so use that instead of giving up. - Run the initializer outside the Angular zone (
ngZone.runOutsideAngular(…)). The SDK's cache schedules asetTimeoutto expire entries, and inside the zone that timer keeps Angular "unstable", which blocks server-side rendering until it fires.
My recommendation
Don't try to add your own settings to siteInfo, and don't put them on the JSS Settings template either. That item is already quite full out of the box.
Instead, create a separate template per group of settings and add those as child items of the site's Settings item. Editors get small, focused items with proper field types and help text. And with the query above, you still read all of them in one request.
And if a setting isn't meant for editors at all, it doesn't belong in the content tree. Put it in your application configuration and skip the round-trip completely :-)
siteInfois a fixed set of Sitecore resolvers, not a view on your Settings item. Put your own settings in separate templates as child items of Settings, and read them withitem(…).
Koen Heye