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

docs: react server components tutorial #1945

Merged
merged 5 commits into from
Jun 11, 2024
Merged

Conversation

vonovak
Copy link
Collaborator

@vonovak vonovak commented May 29, 2024

Description

This is a tutorial for RSC using the existing NextJS example app (see #1944). The idea is to merge that one first and then link it from the guide.

closes #1698
closes #1903
closes #1876

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Examples update

Fixes # (issue)

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

Copy link

vercel bot commented May 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 4, 2024 0:11am

Copy link

github-actions bot commented May 29, 2024

size-limit report 📦

Path Size
./packages/core/dist/index.mjs 2.88 KB (0%)
./packages/detect-locale/dist/index.mjs 723 B (0%)
./packages/react/dist/index.mjs 1.67 KB (0%)
./packages/remote-loader/dist/index.mjs 7.26 KB (0%)

@vonovak vonovak force-pushed the docs/rsc-tutorial branch from 3a37045 to 6ad03dd Compare May 29, 2024 09:59
@vonovak vonovak marked this pull request as ready for review May 29, 2024 10:05
@andrii-bodnar
Copy link
Contributor

@vonovak, excellent and polished writing. I really enjoyed it, thank you! 💪

@andrii-bodnar andrii-bodnar linked an issue May 30, 2024 that may be closed by this pull request
Comment on lines 119 to 132
```tsx title="app/[lang]/components/SomeComponent.tsx"
import { Trans, t } from "@lingui/macro";

export function SomeComponent() {
const { i18n } = useLingui();
return (
<div>
<p>
<Trans>Some Item</Trans>
</p>
<p>{t(i18n)`Other Item`}</p>
</div>
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be it's worth to add an import statement for useLingui / Trans to avoid confusion

Comment on lines 137 to 150
Here's how you could implement server-side `useLingui` yourself using `getI18n`, which is another way to obtain the I18n instance on the server:

```tsx title="useLinguiForRSC.ts"
import { getI18n } from "@lingui/react/server";

export function useLingui() {
const ctx = getI18n();
if (!ctx) {
// throw an informative error
}

return ctx;
}
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need this block? I believe the purpose of it is just informational, but that may bring a bit of cluttering and confusion for the reader. So developer may think that it need to implement this function by itself.

I think the previous paragraph is totally enough to describe the idea behind RSC version of useLingui

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. I removed it, but linked to the implementation. I think it's good to give people understanding of how things work inside.


Most likely, your users will not need to change the language of the application because it will render in their preferred language (obtained from the `accept-language` header in the [middleware](#)), or with a fallback.

To change language, you can either redirect users to a page with the new locale in the url, or you can load another locale [dynamically](/guides/dynamic-loading-catalogs.md). However, you need to keep in mind that if you have server-rendered locale-dependent content, it'd become stale.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no sure we need to advertise dynamic catalogs loading here. For the nextjs the only and single correct way would be to simply navigate to page with correct language, due to nextjs hybrid router + RSC, the only needed parts whould be updated, so pretty much the same result as if we load it dynamically on the FE

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good point. I rewrote it slightly, just pointing out that dynamic loading doesn't make a ton of sense in this scenario

@andrii-bodnar andrii-bodnar merged commit 2f1c1c3 into main Jun 11, 2024
16 checks passed
@andrii-bodnar andrii-bodnar deleted the docs/rsc-tutorial branch June 11, 2024 13:47
Copy link
Contributor

@andrii-bodnar andrii-bodnar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vonovak I just merged the PR too early and didn't notice the broken links. Could you please update them in a separate PR?

Lingui provides support for React Server Components (RSC) as of v4.10.0. In this tutorial, we'll learn how to add internationalization to an application with the Next.js [App Router](https://nextjs.org/docs/app). However, the same principles are applicable to any RSC-based solution.

:::tip Hint
There's a working example available [here](#). We will make references to the important parts of it throughout the tutorial. The example is more complete than this tutorial.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

broken link

:::tip Hint
There's a working example available [here](#). We will make references to the important parts of it throughout the tutorial. The example is more complete than this tutorial.

The example uses both Pages Router and App Router, so you can see how to use Lingui with both in [this commit](#).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

broken link

Why we are not passing the I18n instance directly from `RootLayout` to the client via `LinguiClientProvider`? It's because the I18n object isn't serializable, and cannot be passed from server to client.
:::

Lastly, there's the `appRouterI18n.ts` file, which is only executed on server and holds one instance of I18n object for each locale of our application. See [here](#) how it's implemented in the example.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+

- [Why do nested layouts/pages render before their parent layouts?](https://github.com/vercel/next.js/discussions/53026)
- [On navigation, layouts preserve state and do not re-render](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#layouts)

This means you need to repeat the `setI18n` in every page and layout. Luckily, you can easily factor it out into a simple function call, or create a HOC with which you'll wrap pages and layouts [as seen here](#). Please let us know if there's a known better way.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+


### Changing the active language

Most likely, your users will not need to change the language of the application because it will render in their preferred language (obtained from the `accept-language` header in the [middleware](#)), or with a fallback.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+

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

Successfully merging this pull request may close these issues.

Next.js tutorial NextJS App Router and RSC Support path
3 participants