jss
jss copied to clipboard
SSR pages return 500 error due to context being undefined
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
- Clone
https://github.com/ChrisManganaro/sitecore-nextjs-ssr
- this was started with the next sample app -
npm i
- Config a sitecore host
- Run
npm run start:production
- 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)
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?
withSitecoreContext
related issue I added to the backlog, we will triage that
about #1041 - hotfix will be released
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