Skip to content
Permalink
Browse files

(BUG) failed to load chunk (#1317)

*  failed to load chunk

* added lazy util to load AppRouter

* lazy with retry for only import

* added the retry to all places in code where we do dynamic import

* called it retryImport instead of lazy
  • Loading branch information
StanislavShevchenko committed Feb 7, 2020
1 parent 15f874a commit 9b3295edf72074fa056b8c972e0881db8bfdd17b
@@ -5,6 +5,7 @@ import { DESTINATION_PATH } from './lib/constants/localStorage'
import SimpleStore from './lib/undux/SimpleStore'
import Splash from './components/splash/Splash'
import { delay } from './lib/utils/async'
import retryImport from './lib/utils/retryImport'
import { extractQueryParams } from './lib/share/index'
import logger from './lib/logger/pino-logger'
import { fireEvent, initAnalytics, SIGNIN_FAILED, SIGNIN_SUCCESS } from './lib/analytics/analytics'
@@ -17,7 +18,11 @@ log.debug({ Config })
let SignupRouter = React.lazy(() =>
initAnalytics()
.then(_ =>
Promise.all([import(/* webpackChunkName: "signuprouter" */ './SignupRouter'), handleLinks(), delay(2000)])
Promise.all([
retryImport(() => import(/* webpackChunkName: "signuprouter" */ './SignupRouter')),
handleLinks(),
delay(2000),
])
)
.then(r => r[0])
)
@@ -39,12 +44,12 @@ const handleLinks = async () => {
if (userNameAndPWDArray.length === 2) {
const userName = userNameAndPWDArray[0]
const userPwd = userNameAndPWDArray[1]
const UserStorage = await import('./lib/gundb/UserStorageClass').then(_ => _.UserStorage)
const UserStorage = await retryImport(() => import('./lib/gundb/UserStorageClass')).then(_ => _.UserStorage)

const mnemonic = await UserStorage.getMnemonic(userName, userPwd)

if (mnemonic && bip39.validateMnemonic(mnemonic)) {
const mnemonicsHelpers = import('./lib/wallet/SoftwareWalletProvider')
const mnemonicsHelpers = retryImport(() => import('./lib/wallet/SoftwareWalletProvider'))
const { saveMnemonics } = await mnemonicsHelpers
await saveMnemonics(mnemonic)
await AsyncStorage.setItem('GD_isLoggedIn', true)
@@ -77,10 +82,10 @@ const handleLinks = async () => {

let AppRouter = React.lazy(() => {
log.debug('initializing storage and wallet...')
let walletAndStorageReady = import(/* webpackChunkName: "init" */ './init')
let walletAndStorageReady = retryImport(() => import(/* webpackChunkName: "init" */ './init'))
let p2 = walletAndStorageReady.then(({ init, _ }) => init()).then(_ => log.debug('storage and wallet ready'))

return Promise.all([import(/* webpackChunkName: "router" */ './Router'), p2])
return Promise.all([retryImport(() => import(/* webpackChunkName: "router" */ './Router')), p2])
.then(r => {
log.debug('router ready')
return r
@@ -8,6 +8,7 @@ import { withStyles } from '../../lib/styles'
import SimpleStore from '../../lib/undux/SimpleStore'
import { useErrorDialog } from '../../lib/undux/utils/dialog'
import { useSidemenu } from '../../lib/undux/utils/sidemenu'
import retryImport from '../../lib/utils/retryImport'
import { Icon } from '../common'
import IconWrapper from '../common/modal/IconWrapper'
import { CLICK_DELETE_WALLET, fireEvent, LOGOUT } from '../../lib/analytics/analytics'
@@ -39,7 +40,7 @@ export const deleteAccountDialog = ({ API, showDialog, store, theme }) => {
showButtons: false,
})
try {
const userStorage = await import('../../lib/gundb/UserStorage').then(_ => _.default)
const userStorage = await retryImport(() => import('../../lib/gundb/UserStorage')).then(_ => _.default)

let token = await userStorage.getProfileFieldValue('w3Token')

@@ -7,6 +7,7 @@ import { IS_LOGGED_IN } from '../../lib/constants/localStorage'
import logger from '../../lib/logger/pino-logger'
import { withStyles } from '../../lib/styles'
import { useErrorDialog } from '../../lib/undux/utils/dialog'
import retryImport from '../../lib/utils/retryImport'
import Text from '../common/view/Text'
import Section from '../common/layout/Section'
import CustomButton from '../common/buttons/CustomButton'
@@ -51,12 +52,12 @@ const Mnemonics = ({ screenProps, navigation, styles }) => {
if (userNameAndPWDArray.length === 2) {
const userName = userNameAndPWDArray[0]
const userPwd = userNameAndPWDArray[1]
const UserStorage = await import('../../lib/gundb/UserStorageClass').then(_ => _.UserStorage)
const UserStorage = await retryImport(() => import('../../lib/gundb/UserStorageClass').then(_ => _.UserStorage))

const mnemonic = await UserStorage.getMnemonic(userName, userPwd)

if (mnemonic && bip39.validateMnemonic(mnemonic)) {
const mnemonicsHelpers = import('../../lib/wallet/SoftwareWalletProvider')
const mnemonicsHelpers = retryImport(() => import('../../lib/wallet/SoftwareWalletProvider'))
const { saveMnemonics } = await mnemonicsHelpers

await saveMnemonics(mnemonic)
@@ -4,6 +4,7 @@ import bip39 from 'bip39-light'
import get from 'lodash/get'
import React, { useEffect, useState } from 'react'
import { AsyncStorage } from 'react-native'
import retryImport from '../../lib/utils/retryImport'
import { IS_LOGGED_IN } from '../../lib/constants/localStorage'
import logger from '../../lib/logger/pino-logger'
import { withStyles } from '../../lib/styles'
@@ -126,8 +127,8 @@ const Mnemonics = ({ screenProps, navigation, styles }) => {
*/
async function profileExist(): Promise<any> {
const [Wallet, UserStorage] = await Promise.all([
import('../../lib/wallet/GoodWalletClass').then(_ => _.GoodWallet),
import('../../lib/gundb/UserStorageClass').then(_ => _.UserStorage),
retryImport(() => import('../../lib/wallet/GoodWalletClass').then(_ => _.GoodWallet)),
retryImport(() => import('../../lib/gundb/UserStorageClass').then(_ => _.UserStorage)),
])
const wallet = new Wallet({ mnemonic: mnemonics })
await wallet.ready
@@ -11,6 +11,7 @@ import logger from '../../lib/logger/pino-logger'
import API from '../../lib/API/api'
import SimpleStore from '../../lib/undux/SimpleStore'
import { useDialog } from '../../lib/undux/utils/dialog'
import retryImport from '../../lib/utils/retryImport'
import { showSupportDialog } from '../common/dialogs/showSupportDialog'
import { getUserModel, type UserModel } from '../../lib/gundb/UserModel'
import Config from '../../config/config'
@@ -168,8 +169,8 @@ const Signup = ({ navigation }: { navigation: any, screenProps: any }) => {
//lazy login in background
const ready = (async () => {
log.debug('ready: Starting initialization')
const { init } = await import('../../init')
const login = import('../../lib/login/GoodWalletLogin')
const { init } = await retryImport(() => import('../../init'))
const login = retryImport(() => import('../../lib/login/GoodWalletLogin'))
const { goodWallet, userStorage, source } = await init()

//for QA
@@ -0,0 +1,21 @@
// @flow
const retry = (fn, retriesLeft = 5, interval = 1000) => {
return new Promise((resolve, reject) => {
fn()
.then(resolve)
.catch(error => {
setTimeout(() => {
if (retriesLeft === 1) {
// reject('maximum retries exceeded');
reject(error)
return
}

// Passing on "reject" is the important part
retry(fn, retriesLeft - 1, interval).then(resolve, reject)
}, interval)
})
})
}

export default retry
@@ -0,0 +1,5 @@
// @flow

const retryImport = fn => fn()

export default retryImport
@@ -0,0 +1,6 @@
// @flow
import retry from './retry'

const retryImport = fn => retry(fn)

export default retryImport

0 comments on commit 9b3295e

Please sign in to comment.
You can’t perform that action at this time.