Skip to content

Commit

Permalink
fix: handle tolgee instance update (on hot reload) (#3349)
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 authored Jun 25, 2024
1 parent 19a4d7b commit 038e407
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
29 changes: 20 additions & 9 deletions packages/react/src/TolgeeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const getProviderInstance = () => {
return ProviderInstance;
};

let LAST_TOLGEE_INSTANCE: TolgeeInstance | undefined = undefined;

export interface TolgeeProviderProps {
children?: React.ReactNode;
tolgee: TolgeeInstance;
Expand All @@ -33,16 +35,25 @@ export const TolgeeProvider: React.FC<TolgeeProviderProps> = ({
}) => {
const [loading, setLoading] = useState(!tolgee.isLoaded());

// prevent restarting tolgee unnecesarly
// however if the instance change on hot-reloading
// we want to restart
useEffect(() => {
tolgee
.run()
.catch((e) => {
// eslint-disable-next-line no-console
console.error(e);
})
.finally(() => {
setLoading(false);
});
if (LAST_TOLGEE_INSTANCE !== tolgee) {
if (LAST_TOLGEE_INSTANCE) {
LAST_TOLGEE_INSTANCE.stop();
}
LAST_TOLGEE_INSTANCE = tolgee;
tolgee
.run()
.catch((e) => {
// eslint-disable-next-line no-console
console.error(e);
})
.finally(() => {
setLoading(false);
});
}
}, [tolgee]);

const optionsWithDefault = { ...DEFAULT_REACT_OPTIONS, ...options };
Expand Down
2 changes: 1 addition & 1 deletion testapps/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ To run the app in dev mode with in-context translating mode:
2. Generate an API-KEY
3. Copy file `.env` to `.env.development.local`
4. Set `VITE_APP_TOLGEE_API_KEY` to API key obtained in previous step
5. Run `npm run start`
5. Run `npm run develop`
6. Have fun

## To run the app in production mode
Expand Down

0 comments on commit 038e407

Please sign in to comment.