Skip to content

Commit

Permalink
refactor: minor improvements to RSC (#1920)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak committed Apr 18, 2024
1 parent 5d9845a commit c01d733
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
8 changes: 4 additions & 4 deletions packages/react/src/TransRsc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { getI18n } from "./server"
export function TransRsc(
props: TransProps
): React.ReactElement<any, any> | null {
const i18n = getI18n()
if (!i18n) {
const ctx = getI18n()
if (!ctx) {
throw new Error(
"You tried to use `Trans` in Server Component, but i18n instance for RSC hasn't been setup.\nMake sure to call `setI18n` in root of your page"
"You tried to use `Trans` in Server Component, but i18n instance for RSC hasn't been setup.\nMake sure to call `setI18n` in the root of your page."
)
}
return <TransNoContext {...props} lingui={{ i18n }} />
return <TransNoContext {...props} lingui={ctx} />
}
11 changes: 4 additions & 7 deletions packages/react/src/index-rsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ import { getI18n } from "./server"
export { TransRsc as Trans } from "./TransRsc"

export function useLingui(): I18nContext {
const i18n = getI18n()
if (!i18n) {
const ctx = getI18n()
if (!ctx) {
throw new Error(
"You tried to use `useLingui` in Server Component, but i18n instance for RSC hasn't been setup.\nMake sure to call `setI18n` in root of your page"
"You tried to use `useLingui` in a Server Component, but i18n instance for RSC hasn't been setup.\nMake sure to call `setI18n` in the root of your page."
)
}

return {
i18n,
_: i18n.t.bind(i18n),
}
return ctx
}
32 changes: 22 additions & 10 deletions packages/react/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
/**
* This is an entry point for React Server Components (RSC)
*
* The RSC uses a static analysis to find any non-valid function calls in the import graph.
* That means this entry point and it's children should not have any Provider/Context calls.
* RSC uses static analysis to find any non-valid function calls in the import graph.
* That means this entry point and its children must not have any Provider/Context calls.
*/
import type { I18nContext } from "./I18nProvider"

export { TransNoContext } from "./TransNoContext"
export type {
TransProps,
Expand All @@ -15,15 +17,18 @@ export type {
import type { I18n } from "@lingui/core"
import React from "react"

let cache: (() => { current: I18n | null }) | null = null
type CtxValueRef = { current: I18nContext | null }
let cache: (() => CtxValueRef) | null = null

const getLinguiCache = () => {
// make lazy initialization of React.cache
// so it will not execute when module just imported
if (!cache) {
cache = React.cache((): { current: I18n | null } => ({
current: null,
}))
cache = React.cache(
(): CtxValueRef => ({
current: null,
})
)
}

return cache()
Expand All @@ -45,8 +50,15 @@ const getLinguiCache = () => {
* setI18n(i18n);
* ```
*/
export function setI18n(i18n: I18n) {
getLinguiCache().current = i18n
export function setI18n(
i18n: I18n,
defaultComponent?: I18nContext["defaultComponent"]
) {
getLinguiCache().current = {
i18n,
_: i18n._.bind(i18n),
defaultComponent,
}
}

/**
Expand All @@ -62,6 +74,6 @@ export function setI18n(i18n: I18n) {
* }
* ```
*/
export function getI18n(): I18n | null {
return getLinguiCache().current
export function getI18n(): I18nContext | null {
return getLinguiCache()?.current
}

0 comments on commit c01d733

Please sign in to comment.