jss icon indicating copy to clipboard operation
jss copied to clipboard

SSR pages return 500 error due to context being undefined

Open ChrisManganaro opened this issue 2 years ago • 4 comments

Description

With using withSitecoreContext HOC, pages built using SSR return 500 due to context being undefined - in production mode. In testing this issue, I have only be able to reproduce when running the app in production mode locally

Expected behavior

Pages load normally

Steps To Reproduce

  1. Clone https://github.com/ChrisManganaro/sitecore-nextjs-ssr - this was started with the next sample app
  2. npm i
  3. Config a sitecore host
  4. Run npm run start:production
  5. Load application in a browser

Your Environment

  • Sitecore Version: V10.2
  • JSS Version: V20.0.1
  • Browser Name and version: Brave
  • Operating System and version (desktop or mobile): macOS Big Sur 11.6
  • Link to your project (if available): https://github.com/ChrisManganaro/sitecore-nextjs-ssr
TypeError: Cannot read properties of undefined (reading 'context')
    at Object.children (/sites/nextjss-ssr-test/node_modules/@sitecore-jss/sitecore-jss-react/dist/cjs/enhancers/withSitecoreContext.js:15:220)

ChrisManganaro avatar May 27 '22 02:05 ChrisManganaro

The error happens when trying to set the sitecoreContext to context.context on the wrapped component https://github.com/Sitecore/jss/blob/dev/packages/sitecore-jss-react/src/enhancers/withSitecoreContext.tsx#L35

We also noticed that this only seemed to be an issue on SSR pages on first load.

At build time, pages using getStaticProps build and work fine. E.g. we build a static 404 page and were able to access localhost:3000/404 just fine and were able to navigate around the site fine as well. However, loading an SSR'd page or refreshing a previously loaded page it would cause context to be undefined despite it being set on the provider just fine.

We also believe https://github.com/Sitecore/jss/pull/1041 would fix the issue we are seeing, but likely not the underlying bug of the context being undefined.

If #1041 is successful, would it be possible to get a patch release on v19 and v20 please?

pzi avatar May 27 '22 02:05 pzi

withSitecoreContext related issue I added to the backlog, we will triage that

illiakovalenko avatar May 31 '22 20:05 illiakovalenko

about #1041 - hotfix will be released

illiakovalenko avatar May 31 '22 20:05 illiakovalenko

Hi @pzi @ChrisManganaro - We investigated a similar (withSitecoreContext HOC) issue and we narrowed it down to what appears to be a bug introduced in Next.js version 12.1.6. The fix is to downgrade to the previous version (i.e. npm i [email protected]). Can you confirm if this resolves your issue as well?

cc @illiakovalenko @sc-addypathania

ambrauer avatar Jun 21 '22 20:06 ambrauer