Came across a weird issue with a client's Sitecore instance recently where on CD instances, internal link URLs generated in Navigation or redirects are absolute URLs including hostnames but they are CM hostname instead of CDs'.
The Sitecore version is 10.1. SXA and Commerce Storefront. On Docker and AKS.
After lots of digging and debugging Sitecore DLLs within docker containers, found the issue and another finding.
CD generates URLs with CM hostname is because SXA site settings don't have a language set
Debugging through the code of LinkManager.GetItemUrl, it reaches Sitecore.Links.UrlBuilders.Helpers.OptionsDecoratedSiteResolver.ResolveSite at some point like below:
if SkipResolving is true, it takes site context from the default which is correct.
However, it is false in this case and it goes to SiteResolver to resolve the site via item only. So there is a chance that the item is resolved to the wrong site since both CM and CD are pointing to the same Sitecore item path and who gets resolved is all dependent on the order of the sites.
But why SkipResolving is false?
Out of all the code here, LanguageMatchesContextSite is false because of ContextSite.Language is empty but item.Language.Name is "en".
It's all because "Language" field of site settings item(such as "/sitecore/content/tenant/Sites/siteA/Settings/Site Grouping/siteA-CM") is blank... After setting this field to "en", the issue is no longer presented.
Must set "TargetHostname" if "Hostname" contains pipes or wildcards in SXA site settings
This one was not causing a direct issue however could potentially be one. The GetTargetHostName method on SiteInfo is like below:
It's easy to see that if Hostname contains any wildcards or pipes and TargetHostName is blank, it would just return an empty string. Which would definitely cause issues somewhere on the site.