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.

How to handle SSR

See original GitHub issue

I really like this library, but I have some problems getting it to work as I like using SSR (with nextjs). Basically, what I want is to render only the messages of the requested locale and have SSR do it’s work. Using Nextjs I can use a getInitialProps function, but the data is required to be serializable and that’s where the problem comes in; the compiled bundles contain pluralization functionality.

I think my three options are:

  • ignore pluralization
  • include all messages on the client (I don’t want to do this)
  • include the pluralization library in the client

Am I missing something? Any help/ideas would be appreciated.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
rajuashokcommented, May 13, 2021

Just checking in here, looks like this is still an issue with this lib and it does not handle SSR and SSG on nextjs? Will try it out myself when I get a chance.

3reactions
TommasoAmicicommented, Sep 1, 2021

I have been using lingui with nextjs on https://fantaasta.com and I can get it to SSG pages with the default locale, but translations are done client-side. Ideally translated pages should be SSG too.

This is my setup:

// _app.tsx
import { dynamicActivate, plurals } from "@/lib/i18n";
import { messages } from "@/locale/it/messages";
import { Page } from "@/types/page";
import { i18n } from "@lingui/core";
import { I18nProvider } from "@lingui/react";
import type { AppProps } from "next/app";
import { useRouter } from "next/router";
import { useEffect } from "react";

i18n.load("it", messages);
i18n.loadLocaleData("it", { plurals: plurals["it"] });
i18n.activate("it");

function MyApp({ Component, pageProps }: AppProps & { Component: Page }) {
  const { locale } = useRouter();

  useEffect(() => {
    dynamicActivate(locale);
  }, [locale]);

  return <I18nProvider i18n={i18n}><Component {...pageProps} /></I18nProvider>;
}
export default MyApp;
// i18n.ts
import { i18n } from "@lingui/core";
import { en, it } from "make-plural/plurals";

export const plurals: { [language: string]: any } = {
  en,
  it,
};

/**
 * We do a dynamic import of just the catalog that we need
 * @param locale any locale string
 */
export const dynamicActivate = async (locale: string | undefined) => {
  const selectedLocale = locale ?? "it";
  const { messages } = await import(`@/locale/${selectedLocale}/messages`);
  i18n.load(selectedLocale, messages);
  i18n.loadLocaleData(selectedLocale, { plurals: plurals[selectedLocale] });
  i18n.activate(selectedLocale);
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging and error management best practices in SSR apps
Handling SSR errors on the server side · Catching errors in data fetchers · Error status codes · Redirect to error pages ·...
Read more >
Server-Side Rendering (SSR) - Vue.js
Teleports require special handling during SSR. If the rendered app contains Teleports, the teleported content will not be part of the rendered string....
Read more >
A beginner's guide to React Server-Side Rendering (SSR)
In this lesson, we are going to talk about server-side rendering (SSR), its benefits, and its pitfalls. Then we will set up a...
Read more >
What is server-side rendering and how does it improve site ...
Server-side rendering (SSR) addresses the performance and search engine optimization issues of single-page JavaScript applications.
Read more >
Server-Side Rendering - Vite
Vite provides built-in support for server-side rendering (SSR). ... When building an SSR app, you likely want to have full control over your...
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