Sitecore Headless Services 20.0.0を使用している場合、ページを最初に訪問した際に、Next.jsベースのJavaScriptサービス(JSS)のアプリケーションがページを予想通りに表示しない場合があります。他のページに移動してから当初アクセスしたページに戻るとレンダリングが修正されます。事象が発生すると、下記のエラーがログに出力される場合があります:
TypeError: Cannot read properties of undefined (reading 'context')
at Object.children (C:\nextjs-app\node_modules\@sitecore-jss\sitecore-jss-react\dist\cjs\enhancers\withSitecoreContext.js:15:220)
at a.b.render (C:\nextjs-app\node_modules\react-dom\cjs\react-dom-server.node.production.min.js:44:213)
at a.b.read (C:\nextjs-app\node_modules\react-dom\cjs\react-dom-server.node.production.min.js:41:83)
at Object.exports.renderToString (C:\nextjs-app\node_modules\react-dom\cjs\react-dom-server.node.production.min.js:52:138)
at Object.renderPage (C:\nextjs-app\node_modules\next\dist\server\render.js:804:45)
at Object.defaultGetInitialProps (C:\nextjs-app\node_modules\next\dist\server\render.js:391:51)
at Function.getInitialProps (C:\nextjs-app\.next\server\pages\_document.js:273:16)
at Object.loadGetInitialProps (C:\nextjs-app\node_modules\next\dist\shared\lib\utils.js:65:29)
at documentInitialProps (C:\nextjs-app\node_modules\next\dist\server\render.js:817:48)
at renderDocument (C:\nextjs-app\node_modules\next\dist\server\render.js:841:55)
本問題は、デフォルトのNext.jsバージョン12.1.6のアプリケーションをproductionモードで実行する際に発生します:
jss start:production
本問題を修正するには、 Next.jsのバージョンを12.1.5などの以前のバージョンにダウングレードしてください:
npm install next@12.1.5