-
-
Notifications
You must be signed in to change notification settings - Fork 764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getInitialProps is not called unless _app.js extends next/app #615
Comments
Fair point. Not exactly sure how |
So, I looked into this a bit. While there may indeed be issues ahead with hoisting, the main problem here is with the existence of The example in this repo works, because extending In fact, if you follow the second example in the exact NextJs docs that you linked to, you'll find that everything works as expected: import React from 'react'
import App from 'next/app'
import { appWithTranslation } from '../i18n'
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
MyApp.getInitialProps = async (appContext) => {
const appProps = await App.getInitialProps(appContext)
return { ...appProps }
}
export default appWithTranslation(MyApp) |
Thanks for figuring that out. |
please could you update the example repository |
@chiqui3d Update it in what way? |
The example didn't work for me, until I edited the _app.js file thanks to this issue, I almost went crazy. https://github.com/isaachinman/next-i18next/blob/master/examples/simple/pages/_app.js |
@chiqui3d The example is deployed and runs at next-i18next.com. It is also covered by multiple end-to-end tests. Can you be more specific? |
So to apply MyApp.getInitialProps = async (appContext) => {
const appProps = await App.getInitialProps(appContext)
return { ...appProps }
} |
This works for me after use withTranslation const { Component, pageProps } = this.props;
if (Component.WrappedComponent) {
Component.getInitialProps = Component.WrappedComponent.getInitialProps
} |
@isaachinman Is there a way to inject it in a lower-level component but not |
@orifmilod same question to community!
Any ideas? |
Modifying import App from 'next/app';
const MyApp = ({ Component, pageProps, apollo }) => (
<ApolloProvider client={apollo}>
<Component {...pageProps} />
</ApolloProvider>
);
MyApp.getInitialProps = async (appContext) => {
const appProps = await App.getInitialProps(appContext);
return { ...appProps };
};
export default withApollo(appWithTranslation(MyApp)); Works the same way with export default withApollo(withTranslation()(Collections)); |
@norbiu Be aware that this might results in |
It resolve issue, translations work - but build will have warnings related to getInitialProps in appWithTranslation HOC |
Alrighty.. I know what I did wrong here. Kept getting these errors, but again, as mentioned countless times over and over "Read the Docs carefully". First mistake in my case, looking at the Now - I would then realise I would face another issue, but that is solely due to Next.js. https://github.com/vercel/next.js/blob/master/errors/404-get-initial-props.md Next.js does not permit |
Thank you! It works for me. My code: type Props = {
topMenu: TopNavigatorType[]
} & AppProps
function MyApp({ Component, pageProps, topMenu }: Props): JSX.Element {
return (
<>
<AppProvider value={{ menu: topMenu }}>
<Component {...pageProps} />
</AppProvider>
<script src="/lib/baidu.qiao.js" async defer />
</>
)
}
type Init = {
topMenu: TopNavigatorType[]
} & AppInitialProps
MyApp.getInitialProps = async (appContext: AppContextType<Router>) => {
const topMenu = await request.get(NAVIGATION)
const appProps = await App.getInitialProps(appContext)
const props: Init = { topMenu: topMenu.data, ...appProps }
return { ...props }
}
export default MyApp |
excuse me, has anyone seen this |
For those of you who have an app with pages containing MyApp.getInitialProps = async (appContext) => {
// Your static props paths
const staticPropsPaths = [
"/paper/[paperId]/[paperName]",
"/hubs"
]
if (process.browser || !staticPropsPaths.includes(appContext.router.route)) {
const appProps = await App.getInitialProps(appContext)
return { ...appProps }
}
} |
Describe the bug
getInitialProps
is not called unless_app.js
extends App fromnext/app
. This prevents namespace splitting from working. This is a bug with next-i18next because next doesn't require App to be extended; next will callgetInitialProps
even when using a function component for_app.js
. There examples use a function: https://nextjs.org/docs/advanced-features/custom-appOccurs in next-i18next version
v4.0.0
Steps to reproduce
export a function component from
_app.js
instead of a class component that extendsnext/app
.Expected behaviour
I recognize that the docs for next-i18next use a class component, but it's not obvious that doing so is required, and perhaps it shouldn't be required since next doesn't require it.
The text was updated successfully, but these errors were encountered: