TypeError: Cannot read property '_documentProps' of null
μ΄κ²μ Head
μ render λ©μλμμ λ°μν©λλ€:
render() {
var {
styles,
ampPath,
inAmpMode,
assetPrefix,
hybridAmp,
canonicalBase,
__NEXT_DATA__,
dangerousAsPath,
headTags
} = this.context._documentProps;
κ°λ° μ€μλ μ μλνμ§λ§ λΉλ λ²μ μ λΉλνκ³ μ€νν λ μΆ©λν©λλ€.
λμμ μ¬ννλ λ¨κ³λ μ½λ μ€λν« λλ μ μ₯μλ₯Ό μ 곡νμΈμ.
μ΄κ²μ΄ λ΄ μ€μ κ³Ό κ΄λ ¨λ κ²μΌ μ μμμ μ΄ν΄ν©λλ€. μ λ typescriptμ nextjsλ₯Ό μ¬μ©νκ³ μμ΅λλ€.
μ€λ₯κ° λ°μνμ§ μμμ΅λλ€.
μ±μ 9.0.4μμ μ μλνμ§λ§ μ·¨μ½μ± κ²½κ³ λ₯Ό νμνλ μ’ μμ±μ΄ μμ΅λλ€. ν° μ λ°μ΄νΈκ°λμ΄μλ μλ©λλ€.
λ¬Έμ λ νμ νμ΄μ§/_documentμ λ¬Έμ λ΄λΆ 첫 λ²μ§Έ μμμ νμλ©λλ€. μ΄ κ²½μ° "Head"μ΄μ§λ§ Headλ₯Ό μ κ±°νλ©΄ "Main"μ νμλ©λλ€. μ΄λ»κ² λ μ΄ μ»¨ν μ€νΈκ° μ±μμ§μ§ μμ΅λλ€. λΉλ ν λλ§ (κ°λ° μ€)
μ΄κ²μ λ΄ _document νμ΄μ§μ getInitialPropsμ λλ€.
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();
}
}
μ¬μμ°μ΄ μ 곡λμ§ μκΈ° λλ¬Έμ κ·νλ₯Ό λλ / κ·Έκ²μ΄ 무μκ³Ό κ΄λ ¨λ κ²μΈμ§ λ§νλ κ²μ κΈ°λ³Έμ μΌλ‘ λΆκ°λ₯ν©λλ€.
λΉμ μ μν΄λ‘λ₯Ό μ¬μ©νκ³ μμ΅λκΉ?
_document.tsx μ½λμ λν μΌλΆ 리ν©ν°λ§μΌλ‘ μ΄ λ¬Έμ λ₯Ό μμ ν κ² κ°μ΅λλ€.
const originalRenderPage = context.renderPage;
...
try {
context.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheet.collectStyles(<App {...props} />),
});
const initialProps = await BaseDocument.getInitialProps(context);
...
μμ μ μλ λ΄μ©μ κ°κΉμ΅λλ€.
9.0.4μμ 9.1.4λ‘ μ λ°μ΄νΈνλ λμ λ€λ₯Έ λ¬Έμ κ° λ°μν©λλ€..μ΄κ²μ μ΄λ€ μ΄μ λ‘ μ¬μνμ§ μμ μ λ°μ΄νΈμΈ κ² κ°μ΅λλ€.
μ΄μ¨λ ν¬μΈν° μ£Όμ μ κ°μ¬ν©λλ€.
μ¬μμ° λ¦¬ν¬μ§ν 리 μμ΄λ λκΈ°κ° μ΄λ ΅λ€λ κ²μ μκ³ μμ§λ§ λλλ‘ λ€λ₯Έ μ΄μ λ‘ νμ μ΄ νμλκ³ Hello World μλ리μ€μμ μμ μ νμνμ§ μμ΅λλ€. λ¬Όλ‘ μ±μλ μμΉ μλ κ²μ λ°μ‘ν μ μλ λ€λ₯Έ μ½λκ° μμ΅λλ€. νμ κ°λ₯ν©λλ€. νμ κ°λ₯ν ν λ§μ μ 보λ₯Ό λλ²κ·Ένκ³ μ 곡νλ €κ³ λ Έλ ₯ν©λλ€.
λͺ¨λ λμμ μ£Όμ μ κ°μ¬ν©λλ€ μ μλ. λΉμ μ κ΅μ₯νλ€ π
λλ μ΄κ²μ΄ κ΄λ ¨μ΄ μλμ§ νμ€νμ§ μμ λμΌν λ¬Έμ μ μ§λ©΄νμ΅λλ€. νμ§λ§ λ¬Έμ λ λΆμ μ ν μμ μΌλ‘ μΈν΄ λ°μνμ΅λλ€.
λμ import { Head } from 'next/document'
, κ·Έκ²μ΄ μμ΄μΌ import Head from 'next/head'
λκ΅°κ°κ° κ°μ Έ μ€λ €κ³ νλ©΄, Head
, κ·Έλ¦¬κ³ κ°λ³ νμ΄μ§ λ΄μμ _document
μ λͺ
μλ λ°μ κ°μ΄, λ¬Έμ .
@Shub1427 λ―ΈμΈ
@Shub1427 β€οΈ
μμ°, μ΄κ²μ κ±°μ 2μΌ λμ κ°ν μλ μ λ₯Ό ꡬνμ΅λλ€. λλ μ΄κ²μ΄ νμ€ν λ μ λ¬Έμνλμ΄μΌ νλ€κ³ μκ°ν©λλ€.
import Head from 'next/head'
μ¬μ©νλ μ¬λμ λͺ¨λ νκ·Έκ° μλ²κ° μλ ν΄λΌμ΄μΈνΈμμλ§ λ λλ§λλ κ²μ²λΌ 보μ
λλ€.
μ΄ μ€λ₯κ° λ°μν©λλ€.
Unhandled error during request: TypeError: Cannot destructure property 'assetPrefix' of 'this.context._documentProps' as it is undefined.
λ°°ν¬ μ λ΄ _document.jsμ μμ§λ§ λ‘컬μμ μ λλ‘ μλν©λλ€.
@sibasishm κ·Έ μ€λ₯λ₯Ό μ§λ©΄ & μ루μ
μ μ΄λ»κ²νλ κ²μ΄ μμ΅λλ€ this.context
λμ this.context._documentProps
.
μμΈν μ 보 β https://github.com/vercel/next.js/discussions/16162
νμ§λ§ μ§κΈ Type error: Property 'files' does not exist on type 'DocumentProps'.
μ μννλ λμ const { assetPrefix, files } = this.context
Type error: Property 'files' does not exist on type 'DocumentProps'.
λ λ€λ₯Έ μ€λ₯κ° λ°μν©λλ€.
κ°μ₯ μ μ©ν λκΈ
λλ μ΄κ²μ΄ κ΄λ ¨μ΄ μλμ§ νμ€νμ§ μμ λμΌν λ¬Έμ μ μ§λ©΄νμ΅λλ€. νμ§λ§ λ¬Έμ λ λΆμ μ ν μμ μΌλ‘ μΈν΄ λ°μνμ΅λλ€.
λμ
import { Head } from 'next/document'
, κ·Έκ²μ΄ μμ΄μΌimport Head from 'next/head'
λκ΅°κ°κ° κ°μ Έ μ€λ €κ³ νλ©΄,Head
, κ·Έλ¦¬κ³ κ°λ³ νμ΄μ§ λ΄μμ_document
μ λͺ μλ λ°μ κ°μ΄, λ¬Έμ .