Skip to content

Commit

Permalink
feat: improve code splitting example (#105)
Browse files Browse the repository at this point in the history
Updates code splitting example to reflect latest thinking. The
ExtensionProvider40 is now loaded separately from the application
reducing the initial bundle size from 1.1MB to 775kb. The thinking
is that the ExtensionProvider should be loaded as quickly as possible
so that it can communicate with the Looker host as soon as possible.
  • Loading branch information
bryans99 authored Oct 2, 2023
1 parent 0991b0d commit 0f968a9
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 63 deletions.
17 changes: 12 additions & 5 deletions react/typescript/kitchensink/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
*/

import React, { useState } from 'react'
import React, { useState, Suspense } from 'react'
import { ExtensionProvider40 } from '@looker/extension-sdk-react'
import { KitchenSink } from './KitchenSink'
// Components loaded using code splitting
import { AsyncKitchenSink as KitchenSink } from './KitchenSink.async'

// If the Looker server does not support code splitting (version 7.20 and below) replace the above
// imports with the imports below:
// import { KitchenSink } from './KitchenSink'

export const App: React.FC = () => {
const [route, setRoute] = useState('')
Expand All @@ -38,8 +43,10 @@ export const App: React.FC = () => {
}

return (
<ExtensionProvider40 onRouteChange={onRouteChange}>
<KitchenSink route={route} routeState={routeState} />
</ExtensionProvider40>
<Suspense fallback={<></>}>
<ExtensionProvider40 onRouteChange={onRouteChange}>
<KitchenSink route={route} routeState={routeState} />
</ExtensionProvider40>
</Suspense>
)
}
36 changes: 36 additions & 0 deletions react/typescript/kitchensink/src/KitchenSink.async.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
MIT License
Copyright (c) 2022 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import React, { lazy } from 'react'
import type { KitchenSinkProps } from './types'

const KitchenSink = lazy<any>(
async () => import(/* webpackChunkName: "kitchensink" */ './KitchenSink')
)

export const AsyncKitchenSink: React.FC<KitchenSinkProps> = (props) => (
<KitchenSink {...props} />
)
116 changes: 58 additions & 58 deletions react/typescript/kitchensink/src/KitchenSink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

import React, { useEffect, useState, useContext, Suspense } from 'react'
import React, { useEffect, useState, useContext } from 'react'
import { Switch, Route } from 'react-router-dom'
import { intersects } from 'semver'
import {
Expand Down Expand Up @@ -157,65 +157,63 @@ export const KitchenSink: React.FC<KitchenSinkProps> = ({
/>
</Aside>
<Section>
<Suspense fallback={<></>}>
<Switch>
{configurationData.showApiFunctions && (
<Route path={ROUTES.API_ROUTE}>
<ApiFunctions />
</Route>
)}
{configurationData.showCoreSdkFunctions && (
<Route
path={[
ROUTES.CORESDK_ROUTE,
`${ROUTES.CORESDK_ROUTE}?test=abcd`,
]}
>
<CoreSDKFunctions />
</Route>
)}
{configurationData.showEmbedDashboard && (
<Route path={ROUTES.EMBED_DASHBOARD}>
<EmbedDashboard id={configurationData.dashboardId} />
</Route>
)}
{configurationData.showEmbedExplore && (
<Route path={ROUTES.EMBED_EXPLORE}>
<EmbedExplore id={configurationData.exploreId} />
</Route>
)}
{configurationData.showEmbedLook && (
<Route path={ROUTES.EMBED_LOOK}>
<EmbedLook id={configurationData.lookId} />
</Route>
)}
{configurationData.showExternalApiFunctions && (
<Route path={ROUTES.EXTERNAL_API_ROUTE}>
<ExternalApiFunctions />
</Route>
)}
{configurationData.showMiscFunctions && (
<Route path={ROUTES.MISC_ROUTE}>
<MiscFunctions />
</Route>
)}
<Route path={ROUTES.CONFIG_ROUTE}>
<Configure
configurationData={configurationData}
updateConfigurationData={updateConfigurationData}
canPersistContextData={canPersistContextData}
/>
<Switch>
{configurationData.showApiFunctions && (
<Route path={ROUTES.API_ROUTE}>
<ApiFunctions />
</Route>
{configurationData.showMiscFunctions && (
<Route path={ROUTES.MISC_ROUTE}>
<MiscFunctions />
</Route>
)}
<Route>
<Home />
)}
{configurationData.showCoreSdkFunctions && (
<Route
path={[
ROUTES.CORESDK_ROUTE,
`${ROUTES.CORESDK_ROUTE}?test=abcd`,
]}
>
<CoreSDKFunctions />
</Route>
</Switch>
</Suspense>
)}
{configurationData.showEmbedDashboard && (
<Route path={ROUTES.EMBED_DASHBOARD}>
<EmbedDashboard id={configurationData.dashboardId} />
</Route>
)}
{configurationData.showEmbedExplore && (
<Route path={ROUTES.EMBED_EXPLORE}>
<EmbedExplore id={configurationData.exploreId} />
</Route>
)}
{configurationData.showEmbedLook && (
<Route path={ROUTES.EMBED_LOOK}>
<EmbedLook id={configurationData.lookId} />
</Route>
)}
{configurationData.showExternalApiFunctions && (
<Route path={ROUTES.EXTERNAL_API_ROUTE}>
<ExternalApiFunctions />
</Route>
)}
{configurationData.showMiscFunctions && (
<Route path={ROUTES.MISC_ROUTE}>
<MiscFunctions />
</Route>
)}
<Route path={ROUTES.CONFIG_ROUTE}>
<Configure
configurationData={configurationData}
updateConfigurationData={updateConfigurationData}
canPersistContextData={canPersistContextData}
/>
</Route>
{configurationData.showMiscFunctions && (
<Route path={ROUTES.MISC_ROUTE}>
<MiscFunctions />
</Route>
)}
<Route>
<Home />
</Route>
</Switch>
</Section>
</Layout>
</Page>
Expand All @@ -224,3 +222,5 @@ export const KitchenSink: React.FC<KitchenSinkProps> = ({
</>
)
}

export default KitchenSink

0 comments on commit 0f968a9

Please sign in to comment.