Skip to content

Instantly share code, notes, and snippets.

@marteinn
Created September 26, 2020 06:27
Show Gist options
  • Save marteinn/00b96acf83047d8521a14d52cc276b77 to your computer and use it in GitHub Desktop.
Save marteinn/00b96acf83047d8521a14d52cc276b77 to your computer and use it in GitHub Desktop.
Next.js: How to capture and use page props in next/document
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;
@ranran2121
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment