Sitecore / jss

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

SSR pages return 500 error due to context being undefined #1042

Open ChrisManganaro opened 2 years ago

ChrisManganaro commented 2 years ago

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

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)
pzi commented 2 years ago

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?

illiakovalenko commented 2 years ago

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

illiakovalenko commented 2 years ago

about #1041 - hotfix will be released

ambrauer commented 2 years ago

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 next@12.1.5). Can you confirm if this resolves your issue as well?

cc @illiakovalenko @sc-addypathania