Created
September 26, 2020 06:27
-
-
Save marteinn/00b96acf83047d8521a14d52cc276b77 to your computer and use it in GitHub Desktop.
Next.js: How to capture and use page props in next/document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Document, { Html, Head, Main, NextScript } from 'next/document'; | |
class CustomDocument extends Document { | |
static async getInitialProps(ctx) { | |
let pageProps = null; | |
const originalRenderPage = ctx.renderPage; | |
ctx.renderPage = () => | |
originalRenderPage({ | |
enhanceApp: (App) => (props) => { | |
pageProps = props.pageProps; | |
return <App {...props} /> | |
}, | |
enhanceComponent: (Component) => Component, | |
}) | |
const initialProps = await Document.getInitialProps(ctx); | |
return { ...initialProps, pageProps } | |
} | |
render() { | |
const { pageProps } = this.props; | |
console.log("This is my page props:"); | |
console.log(pageProps); | |
return ( | |
<Html> | |
<Head /> | |
<body> | |
<Main /> | |
<NextScript /> | |
</body> | |
</Html> | |
) | |
} | |
} | |
export default CustomDocument; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there a way to have the information about the session in the _document.js file?
I need to send the email of a logged user in order to track him in my marketing tool