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

How to handle hydration errors? SEO? #50

Open
tomzag opened this issue Nov 11, 2022 · 9 comments
Open

How to handle hydration errors? SEO? #50

tomzag opened this issue Nov 11, 2022 · 9 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@tomzag
Copy link

tomzag commented Nov 11, 2022

Hi,

when useBrowserDefault is set to true and the users browser language is different as set in defaultLang there will be fired hydrations errors ("Text content does not match server-rendered HTML."). This is expected as the client site renders a different language than generated on the server.

You can reproduce the issue on the example page (https://next-export-i18n-example.vercel.app/) if you change the browser language to something other than german (as here defaultLang is set to de).

Bildschirmfoto 2022-11-11 um 15 17 33

This is my first static next.js project so I'm wondering how to handle this?

I'm also wondering how SEO works for languages that are not set as defaultLang as this languages are not created as static files.

@Nabeeh-AlSawaf
Copy link

any updates on this problem ? i have hydration errors even for same language since translation key is not same as translation value ex:
"some sentence" : "some sentence."
in this example it's only the "." and yet next throws hydration error (which is indeed as expected) and i have no idea how to fix this

@mr-t-Durden
Copy link

I am also very interested in a solution to this problem ;)

@martinkr
Copy link
Owner

martinkr commented Jan 6, 2023

Hi everyone,

Thank you for reaching out. I will look into it.

Cheers

@martinkr martinkr self-assigned this Jan 6, 2023
@EperezOk
Copy link

EperezOk commented Feb 4, 2023

Found a "solution" to this issue, just pass the property suppressHydrationWarning to the element wrapping the translation:

<p suppressHydrationWarning>
    {t("some.key")}
</p>

@martinkr martinkr added this to the 2.2.1 milestone May 19, 2023
@martinkr martinkr added the documentation Improvements or additions to documentation label May 19, 2023
@macedomauriz
Copy link

macedomauriz commented Aug 5, 2023

If SEO is an issue for your project just enter to https://next-export-i18n-example.vercel.app/ and view page source, in every language the body text is the same to search engines regardless of the language you are in, so it is not advisable.

@dagoss
Copy link

dagoss commented Nov 27, 2023

I'm guessing that t(...) can't be used until the client is loaded? I was seeing hydration errors any time I did a browser reload during dev basically saying that the key was not equal to the value from the translation file. I also tried using a string[] in my translation json (e.g. for paragraphs of content), which worked fine until I refreshed, which gave me a t(...).map is not a function. Based on what I was seeing, it looked like t(...) was just returning the key during pre-render.

What I did to get around this is ensure that the client is rendered first using useEffect. This fixed the hydration errors.

import { useState, useEffect } from "react";
import { useTranslation, useLanguageQuery } from "next-export-i18n";

export default TestPage = () => {
  const { t } = useTranslation();
  const [query] = useLanguageQuery();
  const [isClient, setIsClient] = useState<boolean>(false);

  useEffect(() => {
    setIsClient(true);
  }, []);

  return (
    isClient && (
      <div>
        <main>{t("home.hero.title")}</main>
      </div>
    )
  );
};

@martinkr martinkr removed this from the 2.2.1 milestone Dec 1, 2023
@SalahAdDin
Copy link

SalahAdDin commented Jan 17, 2024

I'm guessing that t(...) can't be used until the client is loaded? I was seeing hydration errors any time I did a browser reload during dev basically saying that the key was not equal to the value from the translation file. I also tried using a string[] in my translation json (e.g. for paragraphs of content), which worked fine until I refreshed, which gave me a t(...).map is not a function. Based on what I was seeing, it looked like t(...) was just returning the key during pre-render.

What I did to get around this is ensure that the client is rendered first using useEffect. This fixed the hydration errors.

import { useState, useEffect } from "react";
import { useTranslation, useLanguageQuery } from "next-export-i18n";

export default TestPage = () => {
  const { t } = useTranslation();
  const [query] = useLanguageQuery();
  const [isClient, setIsClient] = useState<boolean>(false);

  useEffect(() => {
    setIsClient(true);
  }, []);

  return (
    isClient && (
      <div>
        <main>{t("home.hero.title")}</main>
      </div>
    )
  );
};

@martinkr But this is not the best solution.

@AgrYpn1a
Copy link

AgrYpn1a commented Mar 4, 2024

@martinkr Do we have any solution for this yet?

@Vanals
Copy link

Vanals commented Sep 17, 2024

Is this a real problem do we really know that Google cannot crawl properly the website if we set the language client side?

An alternative could be to create static pages for each language, just for search engines.

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

No branches or pull requests

10 participants