ctx.req is undefined in Document #18235
-
Bug reportDescribe the bugA clear and concise description of what the bug is. To ReproduceSteps to reproduce the behavior, please provide code snippets or a repository: class MyDocument extends Document
{
static async getInitialProps(ctx: DocumentContext)
{
console.log(`MyDocument`, `getInitialProps`, this)
if (1 || typeof window === 'undefined')
{
const state = getNextGlobalState();
state.hostInfo = getNextHost(ctx) ?? state.hostInfo;
const _ctx = getNextPageContext(ctx);
console.dir(ctx)
console.dir(_ctx)
console.dir(state)
}
const initialProps = await Document.getInitialProps(ctx);
return initialProps
} Expected behaviorA clear and concise description of what you expected to happen. ScreenshotsSystem information
Additional contextAdd any other context about the problem here. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @bluelovers. Code like what you have above should be put in |
Beta Was this translation helpful? Give feedback.
Hi @bluelovers.
_document.js
is not the recommended place for code like this. Document is not meant to hold application logic like this. There are also some other caveats](https://nextjs.org/docs/advanced-features/custom-document#caveats) too.Code like what you have above should be put in
_app.js
. That will achieve what you are looking for and will run on every page.