4

First things first - I've read the Next.js docs, passed the tutorial, looked at a number of examples, but at the end of the day, I am still failing to achieve a very simple thing with Next.js.

I have an application that has these locales: en and de.

The routes look like this:

/en/things/[id]
/de/things/[id]

So basically, I need to display some static texts in two languages that have nothing to do with the thing ID. These are just plain texts the same for all IDs. Then I have some AJAX that actually needs the ID from the path.

Now the troubles start:

  1. If I don't have getStaticProps in the things/[id].js, I got this error:
Error: MISSING_MESSAGE: No messages were configured on the provider.
  1. Then I add the getStaticProps like this:
export async function getStaticProps({locale}) {
  return {
    props: {
      messages: await import(`../messages/${locale}.json`)
    }
  }
}

and get this error:

Error: getStaticPaths is required for dynamic SSG pages and is missing for '/things/[id]'.
  1. Then I add getStaticPaths like this:
export const getStaticPaths = () => ({
  paths: [],
  fallback: 'blocking'
})

because I have a million IDs, I cannot pre-generate all the pages. Also, new IDs appear after the deployment of the app, so I cannot list all possible IDs as paths during build time.

Now I get this error:

Error: Error serializing `.messages` returned from `getStaticProps` in "/things/[id]".
Reason: `object` ("[object Module]") cannot be serialized as JSON. Please only return JSON serializable data types.

I cannot believe how such a simple thing can be so hard to achieve in a "framework" that claims to make life easier when working with React. I am trying to migrate an existing application from create-react-app to Next.js, but very simple things are so confusing to achieve.

Please, tell me how to achieve the following very simple thing:

  1. Have one static page with static text in two languages: EN and DE.
  2. Have users access the page like this: /[locale]/things/[id].
  3. Have some AJAX code that actually uses the thing ID from the URI path.
ric980
  • 159
  • 1
  • 2
  • 4
  • I'm facing the same issue! I can do one thing or the other thing, but not both. I decided not to use getStaticPaths becuase I also have thousands of IDs, and I could pre-render just a few in the beginning, but it makes more sense to do it at request time. Now, if someone tries to access an a url like /orders/:id, it gets into an infinite redirect loop to itself (using a very dummy example from the docs as well) – sergioviniciuss Jan 25 '22 at 11:24
  • but if I remove the i18n setup from the configs, it works. – sergioviniciuss Jan 25 '22 at 11:24
  • ah, and in your case, the issue seems to be because you're not returning the locale to the paths in the getStaticPaths. You need to do that. – sergioviniciuss Jan 25 '22 at 14:11

1 Answers1

1

You shouldn't create 2 separate routes for local language instead setup next-i18next it will automatically async your links according to selected local language.

Paiman Rasoli
  • 1,088
  • 1
  • 5
  • 15
  • I don't have 2 routes. I have 1 page/component, 1 route `/things/[id]`. The `en` | `de` part of the path is handled by the config. I am using `"next-intl": "2.3.3"`. – ric980 Jan 01 '22 at 15:05
  • Try the package that I have mentioned it is very easy and has much popularity among the Next.js community and I have personally used it and much easier to configure. – Paiman Rasoli Jan 02 '22 at 04:42
  • 1
    Thanks. I managed to get this working using `next-i18next` and `getServerSideProps`. Still, I am confused about why I had to convert my static page to a server-side rendered page just to get the translations working. Translations are just static texts and shouldn't require server-side rendering. I should be able to have them at build time. – ric980 Jan 02 '22 at 09:25
  • yes next-i18next has component base translation you can use t function to do that. import { useTranslation } from "next-i18next"; now inside component use Hook const { t } = useTranslation("common"); inside return

    {t("news-letter")}

    – Paiman Rasoli Jan 02 '22 at 09:58
  • @ric980 next.js i18n feature is not able to be used when exporting it to a static website (SSG), a workaround can be this: https://dev.to/adrai/static-html-export-with-i18n-compatibility-in-nextjs-8cd – adrai Jan 06 '22 at 13:26
  • @ric i managed using `getServerSideProps` as well. but it hurts runtime performance as It needs to reload the translation at runtime, how did you manage this issue? – Bryan Lumbantobing Jul 18 '22 at 17:55