Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
petedavisdev opened this issue Jul 14, 2022 · 3 comments
Labels

Comments

@petedavisdev
Copy link

petedavisdev commented Jul 14, 2022

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

  • 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
@ambrauer
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

ambrauer commented Sep 8, 2022

Fixed by #1150

@ambrauer ambrauer closed this as completed Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants