Skip to content
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

Support getInitialProps to translate 500 error page #1058

Closed
dohomi opened this issue Mar 12, 2021 · 2 comments
Closed

Support getInitialProps to translate 500 error page #1058

dohomi opened this issue Mar 12, 2021 · 2 comments

Comments

@dohomi
Copy link

dohomi commented Mar 12, 2021

Is your feature request related to a problem? Please describe.

Currently it is not possible to use serverSideTranslations in combination with getInitialProps to translate 500 error messages.

Additional context

If I try to use getInitialProps serverSideTranslations I receive following error:

ModuleNotFoundError: Module not found: Error: Can't resolve 'fs' 
// pages/500.js
export default function Error({ err }) {
  return <div>Error happened..</div>
}

export const getInitialProps = async ({ locale, err }) => {
  return {
    err,
    ...(await serverSideTranslations(locale, ['common', 'layout']))
  }
}
@dohomi
Copy link
Author

dohomi commented Mar 12, 2021

Ok I solved it on a second attempt, in case anyone bumps into it here my _error.tsx page:

import { GetStaticProps, NextPage } from 'next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'

const Error: NextPage<{ statusCode?: number }> = ({ statusCode }) => {
  return (
    <>
      <div>
        {statusCode
          ? `An error ${statusCode} occurred on server`
          : 'An error occurred on client'}
      </div>
    </>
  )
}

Error.getInitialProps = async ({ err, res }) => {
  // eslint-disable-next-line no-nested-ternary
  const statusCode = res ? res.statusCode : err ? err.statusCode : 404
  return { statusCode }
}
export const getStaticProps: GetStaticProps = async ({ locale }) => ({
  props: {
    ...(await serverSideTranslations(locale, ['common', 'layout']))
  }
})

export default Error

@dohomi dohomi closed this as completed Mar 12, 2021
@mercteil
Copy link

Which is weird, because Next.js instantly tells me I am not able to use getInitProps AND getStaticProps within a page component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants