Skip to content

Commit

Permalink
fix: lazy load code splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Aug 12, 2022
1 parent 4c427f7 commit 9572e9b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
15 changes: 10 additions & 5 deletions src/preview/PreviewMode.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {memo, useEffect} from 'react'
import {memo, Suspense, useEffect} from 'react'

import {type PreviewSubscriptionProps, PreviewSubscription} from './PreviewSubscription'
import {PreviewSubscriptionWithToken} from './PreviewSubscriptionWithToken'
import {useAuthenticated} from './useAuthenticated'
import {
type PreviewSubscriptionProps,
PreviewSubscription,
PreviewSubscriptionWithToken,
useAuthenticated,
} from '.'

export interface PreviewModeProps extends PreviewSubscriptionProps {
authMode: 'dual' | 'token' | 'cookie'
Expand All @@ -28,7 +31,9 @@ const PreviewModeComponent = ({authMode, onAuth, ...props}: PreviewModeProps) =>
return null
case 'token':
return !props.EventSource && props.token ? (
<PreviewSubscriptionWithToken {...props} token={props.token!} />
<Suspense fallback={null}>
<PreviewSubscriptionWithToken {...props} token={props.token!} />
</Suspense>
) : (
<PreviewSubscription {...props} />
)
Expand Down
2 changes: 1 addition & 1 deletion src/preview/PreviewSubscription.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {memo, useEffect} from 'react'
import {unstable_batchedUpdates} from 'react-dom'

import {type SyncGroqStoreHookProps, useSyncGroqStore} from './useSyncGroqStore'
import {type SyncGroqStoreHookProps, useSyncGroqStore} from '.'

export interface PreviewSubscriptionProps extends SyncGroqStoreHookProps {
onChange: (data: any) => void
Expand Down
5 changes: 4 additions & 1 deletion src/preview/PreviewSubscriptionWithToken.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EventSource from 'eventsource'
import {memo} from 'react'

import {type PreviewSubscriptionProps, PreviewSubscription} from './PreviewSubscription'
import {type PreviewSubscriptionProps, PreviewSubscription} from '.'

// EventSource is a very chonky boi, that's why this is in a separate component
// eslint-disable-next-line no-warning-comments
Expand All @@ -24,3 +24,6 @@ const PreviewSubscriptionWithTokenComponent = ({
}

export const PreviewSubscriptionWithToken = memo(PreviewSubscriptionWithTokenComponent)

// Re-export as default to support React.lazy use cases
export default PreviewSubscriptionWithToken
6 changes: 5 additions & 1 deletion src/preview/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {lazy} from 'react'

import {PreviewMode} from './PreviewMode'

export default PreviewMode
export * from './PreviewMode'
export * from './PreviewSubscription'
export * from './PreviewSubscriptionWithToken'
export type {PreviewSubscriptionWithTokenProps} from './PreviewSubscriptionWithToken'
export * from './useAuthenticated'
export * from './useGroqStore'
export * from './useSyncGroqStore'

export const PreviewSubscriptionWithToken = lazy(() => import('./PreviewSubscriptionWithToken'))
2 changes: 1 addition & 1 deletion src/preview/useSyncGroqStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useSyncExternalStore} from 'use-sync-external-store/shim'

import {type GroqStoreHookProps, useGroqStore} from './useGroqStore'
import {type GroqStoreHookProps, useGroqStore} from '.'

export type SyncGroqStoreHookProps = GroqStoreHookProps

Expand Down

0 comments on commit 9572e9b

Please sign in to comment.