@tolgee/react 5.29.4 → 5.29.6-prerelease.1efc495b.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,
@@ -20,11 +21,31 @@ export const getProviderInstance = () => {
20
21
 
21
22
  let LAST_TOLGEE_INSTANCE: TolgeeInstance | undefined = undefined;
22
23
 
24
+ export type SSROptions = {
25
+ /**
26
+ * Hard set language to this value, use together with `staticData`
27
+ */
28
+ language?: string;
29
+ /**
30
+ * If provided, static data will be hard set to Tolgee cache for initial render
31
+ */
32
+ staticData?: TolgeeStaticData;
33
+ };
34
+
23
35
  export interface TolgeeProviderProps {
24
36
  children?: React.ReactNode;
25
37
  tolgee: TolgeeInstance;
26
38
  options?: ReactOptions;
27
39
  fallback?: React.ReactNode;
40
+ /**
41
+ * use this option if you use SSR
42
+ *
43
+ * You can pass staticData and language
44
+ * which will be set to tolgee instance for the initial render
45
+ *
46
+ * Don't switch between ssr and non-ssr dynamically
47
+ */
48
+ ssr?: SSROptions | boolean;
28
49
  }
29
50
 
30
51
  export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
@@ -32,9 +53,8 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
32
53
  options,
33
54
  children,
34
55
  fallback,
56
+ ssr,
35
57
  }) => {
36
- const [loading, setLoading] = useState(!tolgee.isLoaded());
37
-
38
58
  // prevent restarting tolgee unnecesarly
39
59
  // however if the instance change on hot-reloading
40
60
  // we want to restart
@@ -56,6 +76,17 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
56
76
  }
57
77
  }, [tolgee]);
58
78
 
79
+ let tolgeeSSR = tolgee;
80
+
81
+ if (ssr) {
82
+ const { language, staticData } = (
83
+ typeof ssr === 'boolean' ? {} : ssr
84
+ ) as SSROptions;
85
+ tolgeeSSR = useTolgeeSSR(tolgee, language, staticData);
86
+ }
87
+
88
+ const [loading, setLoading] = useState(!tolgeeSSR.isLoaded());
89
+
59
90
  const optionsWithDefault = { ...DEFAULT_REACT_OPTIONS, ...options };
60
91
 
61
92
  const TolgeeProviderContext = getProviderInstance();
@@ -63,7 +94,7 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
63
94
  if (optionsWithDefault.useSuspense) {
64
95
  return (
65
96
  <TolgeeProviderContext.Provider
66
- value={{ tolgee, options: optionsWithDefault }}
97
+ value={{ tolgee: tolgeeSSR, options: optionsWithDefault }}
67
98
  >
68
99
  {loading ? (
69
100
  fallback
@@ -76,7 +107,7 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
76
107
 
77
108
  return (
78
109
  <TolgeeProviderContext.Provider
79
- value={{ tolgee, options: optionsWithDefault }}
110
+ value={{ tolgee: tolgeeSSR, options: optionsWithDefault }}
80
111
  >
81
112
  {loading ? fallback : children}
82
113
  </TolgeeProviderContext.Provider>
@@ -25,6 +25,8 @@ function getTolgeeWithDeactivatedWrapper(
25
25
  * It also ensures that the first render is done without wrapping and so it avoids
26
26
  * "client different than server" issues.
27
27
  *
28
+ * If no language data and static data are provided no action is taken
29
+ *
28
30
  * @param tolgeeInstance initialized Tolgee instance
29
31
  * @param language language that is obtained outside of Tolgee on the server and client
30
32
  * @param staticData static data for the language