How does exactly getInitialProps works in the custom app #38067
-
I read the following facts about getInitialProps in the documentation:
And also in the example for custom app we have this piece of code with some useful comments:
So having read that, what do I want to do?:
As we saw before, that would fore every page to perform a server side operation (in this case the reading of a cookie), and then continue the normal behaviour. As it was said that Automatic Static Optimization is disabled, I decided to use The thing is that when If it is like that, maybe the documentation is a little bit confusing in that matter and should be corrected or maybe I took something wrong for granted. Tell me please if what I'm trying to do is possible or not, or the only way of doing that is forgetting about ssg and use ssr. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
When a user comes to your site, When you have
Page PropsThere's an often overlooked element in import type { AppProps } from "next/app";
function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
}
MyApp.getInitialProps = () => {};
export default MyApp; When a page runs, If all of your pages had Build timeSince Why? and How? Well, it simply calls I think in your case, Depending on the use case, another solution, would be to pre-render all possible values of this cookie. For example this path: You'd implement, Then This wouldn't work for user driven cookies. It makes no sense to pre-render a page per user, but in such a case, we could use something like
I know that in some cases, the above might be over-simplistic, because there might multiple props sources (CMS data perhaps), but then you'd need to design this without Here's an Maybe you have some more ideas? Would love to keep on discussing this one. |
Beta Was this translation helpful? Give feedback.
getInitialProps
is a bit wild, because, it may run, client side and server side.When a user comes to your site,
getInitialProps
runs server side, after that, it runs on the client, for every SPA like page transition, unless the page it transitions to, runsgetServerSideProps
.When you have
getInitialProps
in the_app
file, you'll see thisWarning
:Page Props
There's an often overlooked element in
_app
and that's what I am calling here,pageProps
: