question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling useTranslation may cause runtime error (Next 12.2 on React 18.2 with Streaming SSR)

See original GitHub issue

🐛 Bug Report

Please forgive me for listing the issue in this repository, although it is possible that this problem is caused by Next or React.

The following runtime error occurs when useTranslation is called on a component doing Streaming SSR in Next.js 12.2.

Unhandled Runtime Error

Error: This Suspense boundary received an update before it finished hydrating. This caused the boundary to switch to client rendering. The usual way to fix this is to wrap the original update in startTransition.

If useTranslation is not called, this problem does not occur.
However, this only occurs with developer builds and not with production builds.
Also, if the strict mode of React is turned off, it will not occur even in developer builds.

To Reproduce

CodeSandbox

import { GetServerSideProps, NextPage } from "next";
import { SSRConfig, useTranslation } from "next-i18next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { Suspense } from "react";

type Props = SSRConfig;

let data: string;

const SampleComponent = (): JSX.Element => {
  if (!data) {
    throw new Promise(async (resolve) => {
      // The reason for the 200 ms stop is that this problem does not occur when the response time is around 100 ms.
      await new Promise((resolve) => setTimeout(resolve, 200));

      data = "resolved!";

      resolve(true);
    });
  }

  return <div>{data}</div>;
};

const IndexPage: NextPage<Props> = () => {
  // Runtime Error occurs when calling the hook of next-i18next (react-i18next)
  const { t } = useTranslation(["common"]);

  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <SampleComponent />
      </Suspense>
    </div>
  );
};

export const getServerSideProps: GetServerSideProps<Props> = async (
  context
) => {
  return {
    props: {
      ...(await serverSideTranslations(context.locale ?? "en"))
    }
  };
};

export default IndexPage;

Expected behavior

Runtime error (This Suspense boundary received an update before it finished hydrating.) is not occurred.

Your Environment

  • runtime version: node v16.15.0
  • React version: 18.2.0
  • Next.js version: 12.2.5
  • i18next version: 21.8.14
  • next-i18next version: 11.3.0
  • os: Ubuntu-20.04

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
AppleNectarcommented, Aug 12, 2022

@adrai @jamuhl Oh my god…

I apologize for the lack of verification… I have tried the code you provided and have confirmed that the same problem occurs. It seems to be a problem on the Next.js side, so I will consider putting an issue in the repository there.

Thank you for responding so quickly. Thank you so much.

0reactions
stale[bot]commented, Sep 9, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useTranslation (hook) - react-i18next documentation
The useTranslation hook will trigger a Suspense if not ready (eg. pending load of translation files). You can set useSuspense to false if...
Read more >
React 18: Hydration failed because the initial UI does not ...
Importing and running some of the packages can cause this error too. ... I have react 18.2.0 with next 12.2.4 and I fix...
Read more >
next-i18next - Bountysource
The following runtime error occurs when useTranslation is called on a component doing Streaming SSR in Next.js 12.2.
Read more >
Advanced Features: Internationalized Routing - Next.js
Next.js has built-in support for internationalized (i18n) routing since v10.0.0 . You can provide a list of locales, the default locale ...
Read more >
Next.js Internationalization with next-i18next and ... - YouTube
In this tutorial, we're going to take a look at setting up a Next.js app with 18next and Next's internationalized routing feature.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found