Skip to content

Commit

Permalink
Load the Intl.Segmenter polyfill only if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Nov 14, 2024
1 parent 8f4b932 commit 6492ce0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@
},
"resolutions": {
"strip-ansi": "6.0.1"
}
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
14 changes: 6 additions & 8 deletions src/initializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import LanguageDetector from "i18next-browser-languagedetector";
import Backend from "i18next-http-backend";
import * as Sentry from "@sentry/react";
import { logger } from "matrix-js-sdk/src/logger";
import { shouldPolyfill as shouldPolyfillSegmenter } from "@formatjs/intl-segmenter/should-polyfill";

import { getUrlParams } from "./UrlParams";
import { Config } from "./config/Config";
Expand Down Expand Up @@ -41,10 +42,10 @@ export class Initializer {
return Initializer.internalInstance?.isInitialized;
}

public static initBeforeReact(): void {
// this maybe also needs to return a promise in the future,
// if we have to do async inits before showing the loading screen
// but this should be avoided if possible
public static async initBeforeReact(): Promise<void> {
if (shouldPolyfillSegmenter()) {
await import("@formatjs/intl-segmenter/polyfill-force");
}

//i18n
const languageDetector = new LanguageDetector();
Expand All @@ -54,7 +55,7 @@ export class Initializer {
lookup: () => getUrlParams().lang ?? undefined,
});

i18n
await i18n
.use(Backend)
.use(languageDetector)
.use(initReactI18next)
Expand All @@ -74,9 +75,6 @@ export class Initializer {
order: ["urlFragment", "navigator"],
caches: [],
},
})
.catch((e) => {
logger.error("Failed to initialize i18n", e);
});

// Custom Themeing
Expand Down
22 changes: 13 additions & 9 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
setLogExtension as setLKLogExtension,
setLogLevel as setLKLogLevel,
} from "livekit-client";
import "@formatjs/intl-segmenter/polyfill";
import "@formatjs/intl-durationformat/polyfill";

import { App } from "./App";
Expand Down Expand Up @@ -57,12 +56,17 @@ if (fatalError !== null) {
throw fatalError; // Stop the app early
}

Initializer.initBeforeReact();
Initializer.initBeforeReact()
.then(() => {
const history = createBrowserHistory();

const history = createBrowserHistory();

root.render(
<StrictMode>
<App history={history} />
</StrictMode>,
);
root.render(
<StrictMode>
<App history={history} />
</StrictMode>,
);
})
.catch((e) => {
logger.error("Failed to initialize app", e);
root.render(e.message);
});

0 comments on commit 6492ce0

Please sign in to comment.