Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot destructure property `html` of 'undefined' or 'null' in Next.JS

I've added material UI according to the following example.

https://github.com/mui-org/material-ui/blob/master/examples/nextjs/pages/_document.js

Following is my code.

import React from 'react';
import Document, {Head, Html, Main, NextScript} from "next/document";
import {ServerStyleSheets} from '@material-ui/core/styles';
import theme from "../theme";

export default class MyDocument extends Document {
render() {
    return (
        <Html lang={"en"}>
            <Head>
                <meta name={"theme-color"} content={theme.palette.primary.main}/>
                <link rel={"stylesheet"}
                      href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
                />
            </Head>
            <body>
            <Main/>
            <NextScript/>
            </body>
        </Html>
    );
}
}

MyDocument.getInitialProps = async (ctx) => {
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;

ctx.renderPage = () => {
    originalRenderPage({
        enhanceApp: (App) => (props) => sheets.collect(<App {...props}/>)
    });
};
const initialProps = await Document.getInitialProps(ctx);

return {
    ...initialProps,
    styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()]
};
};

I'm getting the following error when trying to load localhost:3000

wait  - compiling...
event - compiled successfully
TypeError: Cannot destructure property `html` of 'undefined' or 'null'.
at Function.getInitialProps (C:\Work\funny\web\.next\server\pages\_document.js:299:9)
at process._tickCallback (internal/process/next_tick.js:68:7)
event - build page: /next/dist/pages/_error

Any idea?

like image 985
Sanka Darshana Avatar asked Sep 05 '25 03:09

Sanka Darshana


2 Answers

import Head stand-alone from head like this

import Head from 'next/head'
like image 131
ghazaleh javaheri Avatar answered Sep 08 '25 05:09

ghazaleh javaheri


Found the issue. I had wrapped the originalRenderPage with { }

ctx.renderPage = () => originalRenderPage({
        enhanceApp: (App) => (props) => sheets.collect(<App {...props}/>)
});
like image 30
Sanka Darshana Avatar answered Sep 08 '25 03:09

Sanka Darshana