From ad2721dc55f3233e4c299babe5744418bc530418 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 14 Jul 2023 16:55:25 -0700 Subject: [PATCH] Fixed: Ensure translations are fetched before loading app --- frontend/build/webpack.config.js | 4 ++++ frontend/src/App/App.js | 7 ++++--- frontend/src/Components/Page/ErrorPage.js | 4 ++++ frontend/src/Components/Page/PageConnector.js | 5 ++++- frontend/src/Utilities/String/translate.js | 8 +++++--- frontend/src/index.js | 10 ++++++++++ 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/frontend/build/webpack.config.js b/frontend/build/webpack.config.js index 82601a415..9201a5e76 100644 --- a/frontend/build/webpack.config.js +++ b/frontend/build/webpack.config.js @@ -84,6 +84,10 @@ module.exports = (env) => { hints: false }, + experiments: { + topLevelAwait: true + }, + plugins: [ new webpack.DefinePlugin({ __DEV__: !isProduction, diff --git a/frontend/src/App/App.js b/frontend/src/App/App.js index 781b2ca10..ea29231c2 100644 --- a/frontend/src/App/App.js +++ b/frontend/src/App/App.js @@ -7,13 +7,13 @@ import PageConnector from 'Components/Page/PageConnector'; import ApplyTheme from './ApplyTheme'; import AppRoutes from './AppRoutes'; -function App({ store, history }) { +function App({ store, history, hasTranslationsError }) { return ( - + @@ -25,7 +25,8 @@ function App({ store, history }) { App.propTypes = { store: PropTypes.object.isRequired, - history: PropTypes.object.isRequired + history: PropTypes.object.isRequired, + hasTranslationsError: PropTypes.bool.isRequired }; export default App; diff --git a/frontend/src/Components/Page/ErrorPage.js b/frontend/src/Components/Page/ErrorPage.js index fbbc1af25..e7b436d61 100644 --- a/frontend/src/Components/Page/ErrorPage.js +++ b/frontend/src/Components/Page/ErrorPage.js @@ -7,6 +7,7 @@ function ErrorPage(props) { const { version, isLocalStorageSupported, + hasTranslationsError, seriesError, customFiltersError, tagsError, @@ -19,6 +20,8 @@ function ErrorPage(props) { if (!isLocalStorageSupported) { errorMessage = 'Local Storage is not supported or disabled. A plugin or private browsing may have disabled it.'; + } else if (hasTranslationsError) { + errorMessage = 'Failed to load translations from API'; } else if (seriesError) { errorMessage = getErrorMessage(seriesError, 'Failed to load series from API'); } else if (customFiltersError) { @@ -49,6 +52,7 @@ function ErrorPage(props) { ErrorPage.propTypes = { version: PropTypes.string.isRequired, isLocalStorageSupported: PropTypes.bool.isRequired, + hasTranslationsError: PropTypes.bool.isRequired, seriesError: PropTypes.object, customFiltersError: PropTypes.object, tagsError: PropTypes.object, diff --git a/frontend/src/Components/Page/PageConnector.js b/frontend/src/Components/Page/PageConnector.js index a3127eddf..37c7bf8d0 100644 --- a/frontend/src/Components/Page/PageConnector.js +++ b/frontend/src/Components/Page/PageConnector.js @@ -220,6 +220,7 @@ class PageConnector extends Component { render() { const { + hasTranslationsError, isPopulated, hasError, dispatchFetchSeries, @@ -232,11 +233,12 @@ class PageConnector extends Component { ...otherProps } = this.props; - if (hasError || !this.state.isLocalStorageSupported) { + if (hasTranslationsError || hasError || !this.state.isLocalStorageSupported) { return ( ); } @@ -257,6 +259,7 @@ class PageConnector extends Component { } PageConnector.propTypes = { + hasTranslationsError: PropTypes.bool.isRequired, isPopulated: PropTypes.bool.isRequired, hasError: PropTypes.bool.isRequired, isSidebarVisible: PropTypes.bool.isRequired, diff --git a/frontend/src/Utilities/String/translate.js b/frontend/src/Utilities/String/translate.js index cef8ce047..802824524 100644 --- a/frontend/src/Utilities/String/translate.js +++ b/frontend/src/Utilities/String/translate.js @@ -10,9 +10,11 @@ function getTranslations() { let translations = {}; -getTranslations().then((data) => { - translations = data.strings; -}); +export function fetchTranslations() { + return getTranslations().then((data) => { + translations = data.strings; + }); +} export default function translate(key, tokens) { const translation = translations[key] || key; diff --git a/frontend/src/index.js b/frontend/src/index.js index 8c0c0752f..e9284a7de 100644 --- a/frontend/src/index.js +++ b/frontend/src/index.js @@ -2,6 +2,7 @@ import { createBrowserHistory } from 'history'; import React from 'react'; import { render } from 'react-dom'; import createAppStore from 'Store/createAppStore'; +import { fetchTranslations } from 'Utilities/String/translate'; import App from './App/App'; import './preload'; @@ -12,11 +13,20 @@ import './index.css'; const history = createBrowserHistory(); const store = createAppStore(history); +let hasTranslationsError = false; + +try { + await fetchTranslations(); + +} catch (error) { + hasTranslationsError = true; +} render( , document.getElementById('root') );