next.js: _document -> makeStylesheetInert -> "Cannot read property 'type' of null" if Child is null
What version of Next.js are you using?
10.2.0
What version of Node.js are you using?
14.16.0
What browser are you using?
All
What operating system are you using?
Windows
How are you deploying your application?
Other platform
Describe the Bug
When a Child of the Head is null an exception is thrown: TypeError: Cannot read property 'type' of null.
In the condition a nullsafe is missing
Expected Behavior
if (c && c.type === 'link' &&
c.props['href'] &&
OPTIMIZED_FONT_PROVIDERS.some((url) => c.props['href'].startsWith(url))
)
To Reproduce
In the _document put a null component inside the Head.
import Document, { Html, Head, Main, NextScript } from 'next/document'
const NullComponent = () => null;
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head>
<NullComponent />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 15 (9 by maintainers)
This is a bug, it’s related to the new font optimization (which only runs during build).
I’m also experiencing a similar problem: Node.js: 14.16.0 Next.js: 10.2.0
Error message:
TypeError: Cannot assign to read only property 'children' of object '#<Object>'But in my case, this is the line inside the
_document.tsx’s<Head>that triggers the problem:It wasn’t failing in Next.js 10.0.6.
Our project suddenly started returning 500s in production because of
TypeError: Cannot assign to read only property 'children' of object '#<Object>'.I’ve created a project where I’ve reproduced the issue (https://github.com/atdrago/repro-24783). Here’s the commit where the issue is introduced: https://github.com/atdrago/repro-24783/commit/1ea71f6e8bf8c420b8e0b521a30b3ef6d6592371
Steps to reproduce:
npm i && npm run build && npm starthttp://localhost:3000/foo/bar(/foo/barisn’t special, it can be/foo/anything-here)Some things I noticed:
next-boostgetStaticPathsand navigating to a route that is not caught by thepathsarray, and does not 404getStaticPathsreturns afallbackvalue oftrueor'blocking'(sincefalsewould 404)Node.js: 14.15.0 Next.js: 11.0.1
Please let me know if you have any questions, and thank you for your time and effort looking into this issue!
I’ve created a related issue on
next-boosthere: https://github.com/rjyo/next-boost/issues/42