Using SSR only without the need to add getServerSideProps on every page #16264
Unanswered
Charioteer
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Next.js community!
One of my projects requires my team and me to implement a multi-domain frontend which is using an API key for communicating with our backend/API. Since we need an axios instance which is always sending a domain-specific API key for getting ServerSideProps as well as for communicating with our API on the client side, we are using the _app.js' getInitialProps method:
appContext.ctx.req['apiKey']
is set by our custom express server implementation.We've assumed that - as stated here - the usage of
getInitialProps
in_app.js
will cause "every page in your app to be server-side rendered.". In contrast, however, we have to add the following snippet on each and every page to get the API key set correctly. OtherwiseappContext.ctx.req
isundefined
:To be more precise: without the above-mentioned code snippet the correctly rendered page flashes for about 2 seconds and console.log(appContext.ctx.req) prints twice: the first time it prints the correct request object. The second time it prints undefined leading to an error message on our page. We suspect that somehow the initial render is server side but there is some kind of a second render which causes
ctx.req
to beundefined
.Unfortunately we didn't find a way to "tell" Next.js to server-side-render every single page without exceptions. We basically want to opt-out of SSG completly.
Did we miss something? Shouldn't
appContext.ctx.req
beundefined
in this case? Did we misunderstand_app.js
'getInitialProps()
?Thank you in advance!
Beta Was this translation helpful? Give feedback.
All reactions