Skip to content

Commit

Permalink
feat(performance): Show loading indicator while app loads (#221)
Browse files Browse the repository at this point in the history
* feat(performance): lazy-load bootstrap
  • Loading branch information
jeremyckahn committed Dec 11, 2023
1 parent e962c40 commit d697aa0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/Bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// NOTE: This file is a shim to enable the Bootstrap component to be
// lazy-loaded.
import Bootstrap from './Bootstrap.tsx'
export default Bootstrap
2 changes: 1 addition & 1 deletion src/Bootstrap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'test-utils/mocks/mockSerializationService'
import { userSettingsStubFactory } from 'test-utils/stubs/userSettings'

import { Bootstrap, BootstrapProps } from './Bootstrap'
import Bootstrap, { BootstrapProps } from './Bootstrap'

const mockPersistedStorage =
jest.createMockFromModule<jest.Mock<typeof localforage>>('localforage')
Expand Down
4 changes: 3 additions & 1 deletion src/Bootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const getConfigFromSdk = () => {
})
}

export const Bootstrap = ({
const Bootstrap = ({
persistedStorage: persistedStorageProp = localforage.createInstance({
name: 'chitchatter',
description: 'Persisted settings data for chitchatter',
Expand Down Expand Up @@ -264,3 +264,5 @@ export const Bootstrap = ({
</Router>
)
}

export default Bootstrap
13 changes: 10 additions & 3 deletions src/Init.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react'
import { lazy, Suspense, useEffect, useState } from 'react'
import Box from '@mui/material/Box'
import Typography from '@mui/material/Typography'
import { v4 as uuid } from 'uuid'
Expand All @@ -11,7 +11,10 @@ import {
import { WholePageLoading } from 'components/Loading/Loading'
import { ColorMode, UserSettings } from 'models/settings'

import { Bootstrap, BootstrapProps } from './Bootstrap'
import type { BootstrapProps } from './Bootstrap'

// @ts-expect-error
const Bootstrap = lazy(() => import('./Bootstrap.js'))

export interface InitProps extends Omit<BootstrapProps, 'initialUserSettings'> {
getUuid?: typeof uuid
Expand Down Expand Up @@ -74,7 +77,11 @@ const Init = ({ getUuid = uuid, ...props }: InitProps) => {
return <WholePageLoading />
}

return <Bootstrap {...props} initialUserSettings={userSettings} />
return (
<Suspense fallback={<WholePageLoading />}>
<Bootstrap {...props} initialUserSettings={userSettings} />
</Suspense>
)
}

export default Init
6 changes: 5 additions & 1 deletion src/components/Loading/Loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export const WholePageLoading = ({
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100vh',
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0,
},
...(Array.isArray(sx) ? sx : [sx]),
]}
Expand Down

1 comment on commit d697aa0

@vercel
Copy link

@vercel vercel bot commented on d697aa0 Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

chitchatter – ./

chitchatter.vercel.app
chitchatter-jeremyckahn.vercel.app
chitchatter-git-main-jeremyckahn.vercel.app

Please sign in to comment.