Sitecore / jss

Software development kit for JavaScript developers building web applications with Sitecore Experience Platform
https://jss.sitecore.com
Apache License 2.0
261 stars 275 forks source link

[Next.js] Version 19.0.2 - Build error - null values received in graphql-sitemap-service.js #1102

Closed petedavisdev closed 2 years ago

petedavisdev commented 2 years ago

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

  1. Open a Next.js project that uses Sitecore JSS Version 19.0.2 and Sitecore 10.2
  2. Run npm run build
  3. 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

  1. Make sure the graphql doesn't return nulls to the graphql-sitemap-service.js
  2. 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

ambrauer commented 2 years ago

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

islaytitans commented 2 years ago

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

ambrauer commented 2 years ago

Fixed by #1150