@tolgee/react 5.29.5 → 5.29.6-prerelease.fc7efe8f.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import React, { Suspense, useEffect, useState } from 'react';
2
- import { TolgeeInstance } from '@tolgee/web';
2
+ import { TolgeeInstance, TolgeeStaticData } from '@tolgee/web';
3
3
  import { ReactOptions, TolgeeReactContext } from './types';
4
+ import { useTolgeeSSR } from './useTolgeeSSR';
4
5
 
5
6
  export const DEFAULT_REACT_OPTIONS: ReactOptions = {
6
7
  useSuspense: true,
@@ -25,6 +26,14 @@ export interface TolgeeProviderProps {
25
26
  tolgee: TolgeeInstance;
26
27
  options?: ReactOptions;
27
28
  fallback?: React.ReactNode;
29
+ /**
30
+ * Hard set language to this value, use together with `staticData`
31
+ */
32
+ language?: string;
33
+ /**
34
+ * If provided, static data will be hard set to Tolgee cache for initial render
35
+ */
36
+ staticData?: TolgeeStaticData;
28
37
  }
29
38
 
30
39
  export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
@@ -32,9 +41,9 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
32
41
  options,
33
42
  children,
34
43
  fallback,
44
+ staticData,
45
+ language,
35
46
  }) => {
36
- const [loading, setLoading] = useState(!tolgee.isLoaded());
37
-
38
47
  // prevent restarting tolgee unnecesarly
39
48
  // however if the instance change on hot-reloading
40
49
  // we want to restart
@@ -56,6 +65,10 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
56
65
  }
57
66
  }, [tolgee]);
58
67
 
68
+ const tolgeeSSR = useTolgeeSSR(tolgee, language, staticData);
69
+
70
+ const [loading, setLoading] = useState(!tolgeeSSR.isLoaded());
71
+
59
72
  const optionsWithDefault = { ...DEFAULT_REACT_OPTIONS, ...options };
60
73
 
61
74
  const TolgeeProviderContext = getProviderInstance();
@@ -63,7 +76,7 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
63
76
  if (optionsWithDefault.useSuspense) {
64
77
  return (
65
78
  <TolgeeProviderContext.Provider
66
- value={{ tolgee, options: optionsWithDefault }}
79
+ value={{ tolgee: tolgeeSSR, options: optionsWithDefault }}
67
80
  >
68
81
  {loading ? (
69
82
  fallback
@@ -76,7 +89,7 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
76
89
 
77
90
  return (
78
91
  <TolgeeProviderContext.Provider
79
- value={{ tolgee, options: optionsWithDefault }}
92
+ value={{ tolgee: tolgeeSSR, options: optionsWithDefault }}
80
93
  >
81
94
  {loading ? fallback : children}
82
95
  </TolgeeProviderContext.Provider>
@@ -2,6 +2,7 @@ import {
2
2
  getTranslateProps,
3
3
  TolgeeInstance,
4
4
  TolgeeStaticData,
5
+ isSSR,
5
6
  } from '@tolgee/web';
6
7
  import { useEffect, useMemo, useState } from 'react';
7
8
 
@@ -25,6 +26,8 @@ function getTolgeeWithDeactivatedWrapper(
25
26
  * It also ensures that the first render is done without wrapping and so it avoids
26
27
  * "client different than server" issues.
27
28
  *
29
+ * If no language data and static data are provided no action is taken
30
+ *
28
31
  * @param tolgeeInstance initialized Tolgee instance
29
32
  * @param language language that is obtained outside of Tolgee on the server and client
30
33
  * @param staticData static data for the language
@@ -34,11 +37,13 @@ export function useTolgeeSSR(
34
37
  language?: string,
35
38
  staticData?: TolgeeStaticData | undefined
36
39
  ) {
40
+ const enabled = Boolean(language || staticData);
41
+
37
42
  const [noWrappingTolgee] = useState(() =>
38
43
  getTolgeeWithDeactivatedWrapper(tolgeeInstance)
39
44
  );
40
45
 
41
- const [initialRender, setInitialRender] = useState(true);
46
+ const [initialRender, setInitialRender] = useState(enabled);
42
47
 
43
48
  useEffect(() => {
44
49
  setInitialRender(false);
@@ -48,28 +53,32 @@ export function useTolgeeSSR(
48
53
  // we have to prepare tolgee before rendering children
49
54
  // so translations are available right away
50
55
  // events emitting must be off, to not trigger re-render while rendering
51
- tolgeeInstance.setEmitterActive(false);
52
- tolgeeInstance.addStaticData(staticData);
53
- tolgeeInstance.changeLanguage(language!);
54
- tolgeeInstance.setEmitterActive(true);
56
+ if (enabled) {
57
+ tolgeeInstance.setEmitterActive(false);
58
+ tolgeeInstance.addStaticData(staticData);
59
+ tolgeeInstance.changeLanguage(language!);
60
+ tolgeeInstance.setEmitterActive(true);
61
+ }
55
62
  }, [language, staticData, tolgeeInstance]);
56
63
 
57
64
  useState(() => {
58
- // running this function only on first render
59
- if (!tolgeeInstance.isLoaded()) {
60
- // warning user, that static data provided are not sufficient
61
- // for proper SSR render
62
- const missingRecords = tolgeeInstance
63
- .getRequiredRecords(language)
64
- .map(({ namespace, language }) =>
65
- namespace ? `${namespace}:${language}` : language
66
- )
67
- .filter((key) => !staticData?.[key]);
65
+ if (enabled && isSSR()) {
66
+ // running this function only on first render
67
+ if (!tolgeeInstance.isLoaded()) {
68
+ // warning user, that static data provided are not sufficient
69
+ // for proper SSR render
70
+ const missingRecords = tolgeeInstance
71
+ .getRequiredRecords(language)
72
+ .map(({ namespace, language }) =>
73
+ namespace ? `${namespace}:${language}` : language
74
+ )
75
+ .filter((key) => !staticData?.[key]);
68
76
 
69
- // eslint-disable-next-line no-console
70
- console.warn(
71
- `Tolgee: Missing records in "staticData" for proper SSR functionality: ${missingRecords.map((key) => `"${key}"`).join(', ')}`
72
- );
77
+ // eslint-disable-next-line no-console
78
+ console.warn(
79
+ `Tolgee: Missing records in "staticData" for proper SSR functionality: ${missingRecords.map((key) => `"${key}"`).join(', ')}`
80
+ );
81
+ }
73
82
  }
74
83
  });
75
84