jss
jss copied to clipboard
[Next.js] Version 19.0.2 - Build error - null values received in graphql-sitemap-service.js
Description
When running npm run build
we get the following error:
> Build error occurred
TypeError: Cannot read properties of null (reading 'url')
at C:\dev\sitecore-website\rcn-learn\node_modules\@sitecore-jss\sitecore-jss-nextjs\dist\cjs\services\graphql-sitemap-service.js:135:72
I added console.log(results);
on line 135 of the above file and got:
[
{ url: { path: '/Search/,-w-,' } },
null,
null,
null,
{ url: { path: '/Events' } },
{ url: { path: '/Examples' } },
{ url: { path: '/My-Favourites' } },
{ url: { path: '/Search' } },
{ url: { path: '/Examples/graphql/sample-2/App-Route' } },
{ url: { path: '/Examples/styleguide/custom-route-type' } },
{ url: { path: '/Examples/graphql/sample-2' } },
{ url: { path: '/Examples/styleguide' } },
{ url: { path: '/Examples/graphql/sample-1' } },
{ url: { path: '/' } },
{ url: { path: '/Examples/graphql' } }
]
As you can see, we are getting some null values returned. These need to be filtered out to enable the build to complete.
Expected behavior
There should not be null values returned to graphql-sitemap-service.js or the build should complete, ignoring the null values.
Steps To Reproduce
- Open a Next.js project that uses Sitecore JSS Version 19.0.2 and Sitecore 10.2
- Run npm run build
- If your graphql servace is returning some null values, you will see this error
> Build error occurred
TypeError: Cannot read properties of null (reading 'url')
at C:\dev\sitecore-website\rcn-learn\node_modules\@sitecore-jss\sitecore-jss-nextjs\dist\cjs\services\graphql-sitemap-service.js:135:72
Possible Fix
- Make sure the graphql doesn't return nulls to the graphql-sitemap-service.js
- Handle it gracefully when it does. Maybe just filter them out:
return results.filter((item) => item?.url).map((item) => formatStaticPath(item.url.path.replace(/^\/|\/$/g, '').split('/'), language));
Your Environment
- Sitecore Version: 10.2
- JSS Version: 19.0.2
- Browser Name and version: not relevant
- Operating System and version (desktop or mobile): not relevant
- Link to your project (if available): n/a
Hi @petedavisdev - Thanks for the information, I've added this to our backlog to investigate / fix in a future release.
Just out of curiosity, did you figure out why the GraphQL query was returning null values and/or the solution? e.g. bad index, solved by a re-index
Hi @ambrauer,
I found a fix for the issue are some digging into the source code. The cause of the issue is that the item had been deleted but the item was still in the index. Even after rebuilding the index it remained
A small change to the following file will solve it Sitecore.Services.GraphQL.EdgeSchema.Services.SearchService, Sitecore.Services.GraphQL.EdgeSchema
After this line
IEnumerable<Item> source2 = results.Hits.Select<SearchHit<ContentSearchResult>, Item>((Func<SearchHit<ContentSearchResult>, Item>)(searchHit => searchHit.Document.GetItem()));
Filter out null items e.g.
source2 = source2.Where(i => i != null);
I patched in the change and nulls won't be returned in the json
Fixed by #1150