next-i18next: Error: `pages/404` can not have getInitialProps/getServerSideProps
Describe the bug
After setting up next-i18next
, I have the following error in the terminal on every page (probably because it’s looking for an not existing favicon.ico by default).
Error: `pages/404` can not have getInitialProps/getServerSideProps, https://err.sh/zeit/next.js/404-get-initial-props
But my 404 page is really simple, no getInitialProps/getServerSideProps
export default () => <h1>This is the 404 page</h1>
Note: i’m trying the setup the project for a potential future translation. Therefore, next-i18next
provides a clean way for developers to use the system as string dictionary for UI texts, which later on could be translated.
Obviously, if I go to /404
, I see the error in browser.
Thank you!
Occurs in next-i18next version
From the package-lock.json
and other info
- next-i18next: 4.2.1
- i18next: 19.3.4
- node JS: 13.9.0
- npm: 6.13.7
Steps to reproduce
File structure
└── public
└── static
└── locales
├── en
└── common.json
└── homepage.json
└── src
└── pages
└── components
In i18n.js
Note: I needed to add those properties in the config to be able to make t('key)
working
lng: 'en',
fallbackLng: 'en',
languages: ['en', 'not-existing-yet'],
More specifically lng: 'en'
, the fallbackLng
is for later if we do add more languages, and languages
to be able to access i18n.languages
(otherwise it wasn’t working)
const NextI18Next = require('next-i18next').default
const NextI18NextInstance = new NextI18Next({
defaultLanguage: 'en',
lng: 'en',
fallbackLng: 'en',
languages: ['en', 'not-existing-yet'],
otherLanguages: ['not-existing-yet'],
})
module.exports = NextI18NextInstance;
In _app.js
import React from "react";
import App from "next/app";
// i18n
import { appWithTranslation } from '~/i18n'
class MyApp extends App {
render() {
const { Component, pageProps } = this.props;
return (
<>
<Head>
<title>NextJS Advanced Routing</title>
</Head>
<Component {...pageProps} />
</>
);
}
}
export default appWithTranslation(MyApp);
In index.js
(homepage)
import { withTranslation } from '~/i18n'
const Index = ({ data, t, ...props }) => {
return (
<h1>{ t('homepage:title') }</h1>
)
}
Index.getInitialProps = async ({ query }) => {
// get some data
const data = await getData();
return { data, namespacesRequired: ['common', 'homepage'], }
}
export default withTranslation('homepage')(Index)
Expected behaviour
404 should be working without triggering an error
Screenshots
OS (please complete the following information)
- Device:MBP 2019 15"
- Browser: Chrome 80.0.3987.149
Additional context
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 49
- Comments: 29 (8 by maintainers)
I’ve stumbled upon this discussion but couldn’t find the solution right away here, so I’m posting it for future reference:
This solution uses
"next-i18next": "^8.2.0"
with"next": "^10.2.0"
Just to follow up: I don’t think it’s right to disable the warning. I think the correct thing to do is translate our 404 pages.
I can speak to the NextJs team about this.
Actually this Next’s requirement is absurd, cause if you have translations you must translate your custom 404 as well.
Yeah. I’m facing the same issue now. I like to keep my console clean. But in this particular situation I get or warning from Next.js
You should have custom 404 page because it's good to optimize its as static content
either warning from next-18nextYou have not declared a namespacesRequired array on your page-level component: Custom404
.Frustrating 😞
I came from google. It works! Saved my time. Thank you. Hopefully we do not need getServerSideProps on 404 page.
Any updates?
Feature request opened at https://github.com/vercel/next.js/issues/17004
@Dynkin, you can add your custom 404 page without the getInitialProps. Actually i’m running with this solution. it’s not the best solution but it’s working.
Hello, you are right we should be able to translate the 404 page as well ! (great job on this repo btw)
Is it possible to disable the warning though ? Because all my other pages are correctly setup and I would like to avoid to be spamed by the warning when navigating to the 404 page.
Thank you !
Yeah, @Danetag this is probably an issue to raise with the NextJs team. There’s not much that can be done on the
next-i18next
side of things - we have to get translations somehow.This works for me. On my specific case I need to import both
footer
anderrors
locale.I spend an afternoon fiddling with this I managed to get the following to work
the important part was to use withTranslation to get the translation context and to disable suspense as it is not yet supported. basically this allows a for a fully translated 404 page with client side data fetching for additional stuff like navigation etc.
New to next.js and currently fiddling around to solve the same problem.
I noticed, that if I don’t have a 404.js page, it will fallback to the _error.js page to display the 404 Error and inside _error.js it is possible to use “getInitialProps”.
I am using that for now as a workaround. It solves the translation problem, but it does cause another warning:
Still having this issue using
"next-i18next": "^13.2.2"
and"next": "13.3.1"
I try to createpages/404.tsx
upon building the app, it shows bunch of errors:
Any updates on this? 👀
@CalebLovell As
next-i18next@beta
supports other data-fetching methods, this should no longer be an issue. See #869.