TypeError: Cannot read property '_documentProps' of null
See original GitHub issueBug report
Describe the bug
TypeError: Cannot read property '_documentProps' of null
This gets thrown in the render method of Head
:
render() {
var {
styles,
ampPath,
inAmpMode,
assetPrefix,
hybridAmp,
canonicalBase,
__NEXT_DATA__,
dangerousAsPath,
headTags
} = this.context._documentProps;
Works fine in development but when building and running the build version it crashes
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
I understand this might be something related to my setup. I am using typescript and nextjs.
Expected behavior
No error thrown.
System information
- OS: macOS
- Browser (if applies) chrome (any)
- Version of Next.js: 9.1.4
Additional context
App works fine in 9.0.4 but I have a depenency that is throwing a vulnerability warning…and shouldn’t be a big update (I assumed).
The issue alwways shows itself in the first element inside the Document in pages/_document. In this case its “Head” but if I remove Head then it shows itself in “Main”. Somehow this context is not getting populated…only when building (works in development)
This is the getInitialProps of my _document page :
public static async getInitialProps(context: CustomDocumentContext) {
const sheet = new ServerStyleSheet();
const renderPage = context.renderPage;
try {
context.renderPage = () => {
const enhancer = App => props => {
return sheet.collectStyles(<App {...props} />);
};
return renderPage(enhancer);
};
const initialProps: any = await BaseDocument.getInitialProps(context);
const { language, locale, localeData } = context.req;
return {
...initialProps,
language,
locale,
localeData,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
};
} finally {
// @ts-ignore
sheet.seal();
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (4 by maintainers)
I faced the same issue, not sure if this is related. But, the issue was caused due to improper import.
Instead of
import { Head } from 'next/document'
, it should beimport Head from 'next/head'
, if anybody is trying to importHead
inside individual pages, and not in_document
, as stated in docs.@Shub1427 You’re a beauty, thanks!