Skip to main content

Sitecore XM/XM Cloud - DefaultSitemapQuery errors out if excludepaths has space in item name

Hi Team,

Recently, we had a requirement we needed to prerender pages in NextJS using GetStaticProps but excluding some pages, but it was failing due couple of reasons, I created two blog post around it 

1) Sitecore JSS - Error: `redirect` can not be returned from getStaticProps during prerendering (daivagnananavati.blogspot.com)

2) Sitecore XM/XM Cloud - Exclude specific Sitecore items from prerendering Sitecore JSS (daivagnananavati.blogspot.com)

First blogs explains, what issues we run into while prerendering pages and what is not supported at build time, it is good to understand the concept of prerendering and why redirects does not work while prerendering

and second blog explains once we knew redirect items in GetStaticProps does not work at build time, what are the possibilities and what is that we did to exclude those items from being given to GetStaticProps, so that it ignores those items and does not prerender them and build gets successfully built.

Today, in this blog post I will explain one more bug that exists in Sitecore JSS, In My blog post I have explained how you can use excludePaths property to exclude array of paths which you want to exclude from prerendering, but if redirect items are used in this array & if any redirect items has space in its name, it fails with following error, we needed to exclude redirect items because they are not supported at build time in GetStaticProps

"ClientError: GraphQL.ExecutionError: Provided exclude path <<path to the problematic page with space" is not exist in the <<site name>> site"

When you write a GraphQL query to get your redirect items using a search query, so basically try to find item with specific template, in my case, the query is find all item using redirect template, which is like following

query RedirectQuery(
        $rootItemId: String = "<<your home node GUID>>"
        $language: String = "en"
        $pageSize: Int = 1000
        $after: String
        $redirectTemplateId: String = "<<redirect template GUID>>"
      ) {
        search(
          where: {
            AND: [
              { name: "_path", value: $rootItemId, operator: CONTAINS }
              { name: "_language", value: $language }
              { name: "_templates", value: $redirectTemplateId, operator: EQ }
            ]
          }
          first: $pageSize
          after: $after
        ) {
          total
          pageInfo {
            endCursor
            hasNext
          }
          results {
            url
            {
              path
            }
          }
        }
      }

Now, if you hit this query in GraphQL IDE, it returns all the items which are of redirect type like following 


If you observe the first item path, which says "test-redirect-item", now this item in sitecore has a name with space but the GRAPHQL query and url { path } is automatically converting it and replacing spaces with "-"

This is expected behavior but the problem with this and build fails when "DefaultSitemapQuery" runs when you build the project

In normal scenario everything will work, but in our case we wanted to exclude some items from getStaticProps and for that we used "excludePaths" properties of GraphQLSitemapService, so what ever is the output of above graphql, we were running the loop in our NextJS and creating an string array and were giving it into "excludePaths" property, this whole execution is mentioned on the https://daivagnananavati.blogspot.com/2023/07/sitecore-xmxm-cloud-exclude-specific.html  blog 

Now, issue we run into was, because we had space in Sitecore item name, and our query was returning item path with "-" and "DefaultSitemapQuery" when runs along with "excludePaths" switch, it tries to find item with that "-" and it does not find that item and build fails


Because the build error also printed the default query it is executing, i tried to run the same query in the GraphQL editor to see what is causing it to fail? and i got the same error as shown below


So, now it clearly looked like a bug to me, because in ideal scenario, DefaultSitemapQuery (above) should also replace the item name's space with "-" and then compare but it was not doing that and that is why it was not able to find that item and build was failing.

Solution

One quick fix which I did was, instead of url { path } in my redirect item GraphQL query (first which I showed in this blog post), use path directly so you will need to change the query section like below 


so now instead of the "-" it will show actual item name without replacing which our "DefaultSitemapQuery" expects

but this has another draw back is that, instead of relative path, it shows the full path like "/sitecore/content/<<tenant>>/<<site name>>/Home/test redirect item" item, and that path does not work with "excludePaths" it only needs relative path because it only finds under home node and if you give full path, it will still not work

to get around that, in the .map function, you will need to replace "/sitecore/content/<<tenant>>/<<site name>>/Home" with empty value before pushing in to an array, this full code is available on https://daivagnananavati.blogspot.com/2023/07/sitecore-xmxm-cloud-exclude-specific.html  blog, i am pasting a small screenshot from that blog here so you know which .map function I am talking about, so then you can refer to above blog to connect the "dots"


Now, "DefaultSitemapQuery" which gets executed by default on build time, will be able to successfully ignore all those items and build will not fail and everything will work

On XM or XM Cloud if you are using preview API, you might end up in this issue if you have similar scenario.

NOTE: This behavior was verified with Sitecore and they acknowledge that this is a bug and registered as a bug in bug tracking system with reference number 591500







Comments

Popular posts from this blog

High CPU to completely normal CPU - SXA issue, SXA pages not loading in mobile device

  Hi Team, Today i am going to share one of the nightmarish issue with you all, We are having Sitecore 9.1.1 hosted in azure PaaS environment Our site was working just fine and no noise, but we have been working on a feature release where 7-8 months of development needed to be released to production, Big GO LIVE event right?  Also to make the development smoother we also introduced BLUE/GREEN deployment slots in the same release, so we can easily SWAP slots and go live Everything went well, we went live, we even did load and performance testing on our staging and pre-prod and we were confident enough of results Very next day we started getting "SITE DOWN" alerts, and also product owners and clients mentioned that site is very slow for them in US time and in our morning when we were accessing it, it was working lighting fast so we were clue less at start, but we started digging  1) First thing caught our eyes were HIGH CPU spikes, in US time, also without any traffic CPU u...

One or more exceptions occurred while processing the subscribers to the 'item:creating' event

I was recently installing the packages from one of the QA environment to my local Sitecore instance, "Media library package" to be precise, And it started giving me this below error One or more exceptions occurred while processing the subscribers to the 'item:creating' event Looking at the sitecore logs, it gave me more info on the context and the actual inner exception was following Solution: 'Name' should consist only of letters, digits, dashes or underscore Now it was evident that some of my file names were violating the naming rule, I could see in the log just before exception from where the installer stopped creating items, and that file name had round braces "(" and ")" at the end of it, with my surprise I was able to create the item with those name in the content tree, but below was the solution for it, Solution I am using SC 9.0.1 and in that Go to Sitecore.Marketing.config file residing in "App_Config\Sitecore\Mar...

Every Sitecore Instance using GQL is prone to this scenario of template not found - Sitecore Component With GQL Queries Broke On Production

Hello Team, Today, I wanted to share a real scenario and a solution of it about what happened  suddenly to our components and how we identified it and how we resolved it, and most importantly it could happen to your installations too Scenario We have one Sitecore 10.2 headless instance and two sites, One site is non-sxa which is legacy site and another site is a new site which uses headless SXA. Now, non-sxa site is already live and working fine with all components etc. and the new headless site was in development and it has separate development team, So we have same DEV/UAT/PROD environments. One fine morning they took their code and site on environment but strangely some of the GQL components like header footer of legacy site disappeared.  Troubleshooting Steps 1) We checked the broken component's GQL query  2) We took that query and fired it in GQL IDE, and we observed that, Some of the templates references we used were not found, which was working just fine and we hav...