diff --git a/frontend/build/webpack.config.js b/frontend/build/webpack.config.js index 92986a57c..a83d48c07 100644 --- a/frontend/build/webpack.config.js +++ b/frontend/build/webpack.config.js @@ -83,6 +83,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 41691a823..ae29b0c2f 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 4f01a8a0f..b9b3e86a2 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, moviesError, customFiltersError, tagsError, @@ -20,6 +21,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 (moviesError) { errorMessage = getErrorMessage(moviesError, 'Failed to load movie from API'); } else if (customFiltersError) { @@ -52,6 +55,7 @@ function ErrorPage(props) { ErrorPage.propTypes = { version: PropTypes.string.isRequired, isLocalStorageSupported: PropTypes.bool.isRequired, + hasTranslationsError: PropTypes.bool.isRequired, moviesError: 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 4c25771d3..745f8fc59 100644 --- a/frontend/src/Components/Page/PageConnector.js +++ b/frontend/src/Components/Page/PageConnector.js @@ -232,6 +232,7 @@ class PageConnector extends Component { render() { const { + hasTranslationsError, isPopulated, hasError, dispatchFetchMovies, @@ -246,11 +247,12 @@ class PageConnector extends Component { ...otherProps } = this.props; - if (hasError || !this.state.isLocalStorageSupported) { + if (hasTranslationsError || hasError || !this.state.isLocalStorageSupported) { return ( ); } @@ -271,6 +273,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 696d5ad28..79e7c3566 100644 --- a/frontend/src/Utilities/String/translate.js +++ b/frontend/src/Utilities/String/translate.js @@ -1,22 +1,27 @@ import createAjaxRequest from 'Utilities/createAjaxRequest'; function getTranslations() { - let localization = null; - const ajaxOptions = { - async: false, + return createAjaxRequest({ + global: false, dataType: 'json', - url: '/localization', - success: function(data) { - localization = data.Strings; - } - }; + url: '/localization' + }).request; +} - createAjaxRequest(ajaxOptions); +let translations = {}; - return localization; -} +export function fetchTranslations() { + return new Promise(async(resolve) => { + try { + const data = await getTranslations(); + translations = data.Strings; -const translations = getTranslations(); + resolve(true); + } catch (error) { + resolve(false); + } + }); +} export default function translate(key, args) { const translation = translations[key] || key; diff --git a/frontend/src/index.js b/frontend/src/index.js index 59911154e..acdfc6517 100644 --- a/frontend/src/index.js +++ b/frontend/src/index.js @@ -1,8 +1,7 @@ import { createBrowserHistory } from 'history'; import React from 'react'; import { render } from 'react-dom'; -import createAppStore from 'Store/createAppStore'; -import App from './App/App'; +import { fetchTranslations } from 'Utilities/String/translate'; import './preload'; import './polyfills'; @@ -10,12 +9,18 @@ import 'Styles/globals.css'; import './index.css'; const history = createBrowserHistory(); +const hasTranslationsError = !await fetchTranslations(); + +const { default: createAppStore } = await import('Store/createAppStore'); +const { default: App } = await import('./App/App'); + const store = createAppStore(history); render( , document.getElementById('root') );