Skip to content

Commit 8ea31d8

Browse files
Merge pull request #156 from decentraland/feat/lazy-load-babylon-and-unity
feat: Lazy load Babylon and Unity
2 parents c1871ad + ba6edc1 commit 8ea31d8

8 files changed

Lines changed: 42 additions & 20 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@
7777
"typescript": "^5.2.2",
7878
"vite": "^6.3.5"
7979
}
80-
}
80+
}

scripts/prebuild.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const packageJson = JSON.parse(fs.readFileSync('./package.json').toString())
1313
const publicPackageJson = JSON.parse(fs.readFileSync('./public/package.json').toString())
1414

1515
// set version
16-
ENV_CONTENT['REACT_APP_WEBSITE_VERSION'] = packageJson.version
16+
ENV_CONTENT['VITE_REACT_APP_WEBSITE_VERSION'] = packageJson.version
1717
publicPackageJson.version = packageJson.version
1818

1919
// set public url

src/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { StrictMode } from 'react'
1+
import React, { StrictMode, Suspense } from 'react'
22
import { createRoot } from 'react-dom/client'
3-
import { Preview } from './components/Preview'
4-
import { UnityPreview } from './components/UnityPreview'
3+
const UnityPreview = React.lazy(() => import('./components/UnityPreview/UnityPreview'))
54
import { WebGPUProvider } from './contexts/WebGPUContext'
65
import { useWebGPU } from './hooks/useWebGPU'
76
import { detectWebGPU } from './lib/webgpu'
87
import { initSentry } from './lib/sentry'
98
import './index.css'
109

10+
const Preview = React.lazy(() => import('./components/Preview/Preview'))
11+
1112
// Initialize Sentry as early as possible to capture all errors
1213
initSentry()
1314

@@ -19,7 +20,7 @@ const App = () => {
1920
// Use UnityPreview if WebGPU is available and Unity is requested, otherwise use Preview
2021
const shouldUseUnity = isUnityPreview && isAvailable
2122

22-
return shouldUseUnity ? <UnityPreview /> : <Preview />
23+
return <Suspense fallback={null}>{shouldUseUnity ? <UnityPreview /> : <Preview />}</Suspense>
2324
}
2425

2526
// Initialize the app with WebGPU detection

src/lib/babylon/face.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AbstractMesh, Color3, Orientation, PBRMaterial, Scene, StandardMaterial, Texture } from '@babylonjs/core'
22
import { PreviewConfig, BodyShape, WearableCategory, WearableDefinition } from '@dcl/schemas'
3-
import { hexToColor } from '../color'
3+
import { hexToColor } from './utils'
44
import { Asset, loadMask, loadTexture } from './scene'
55
import { isCategory } from './utils'
66

src/lib/babylon/scene.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
PreviewType,
3636
WearableDefinition,
3737
} from '@dcl/schemas'
38-
import { hexToColor } from '../color'
38+
import { hexToColor } from './utils'
3939
import { isIOs } from '../env'
4040
import { getWearableRepresentation } from '../representation'
4141
import { createSceneController } from '../scene'

src/lib/babylon/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@ import { BodyPartCategory, HideableWearableCategory, WearableCategory, WearableD
22
import { SocialEmoteAnimation } from '@dcl/schemas/dist/dapps/preview/social-emote-animation'
33
import { AnimationGroup, TransformNode, Color3, PBRMaterial, StandardMaterial } from '@babylonjs/core'
44
import { getWearableRepresentationOrDefault, isTexture } from '../representation'
5+
import { parseHex } from '../color'
56
import { Asset } from './scene'
67

8+
export function hexToColor(hex: string) {
9+
const parsed = parseHex(hex)
10+
const color = new Color3(
11+
parseInt(parsed.slice(0, 2), 16) / 256,
12+
parseInt(parsed.slice(2, 4), 16) / 256,
13+
parseInt(parsed.slice(4, 6), 16) / 256
14+
)
15+
return color
16+
}
17+
718
export function isCategory(category: WearableCategory) {
819
return (wearable: WearableDefinition) => wearable.data.category === category
920
}

src/lib/color.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Color3 } from '@babylonjs/core'
21
import { Color3 as RGB } from '@dcl/schemas'
32

43
export function formatHex(color: string) {
@@ -17,13 +16,3 @@ export function numberToHex(value: number) {
1716
export function colorToHex(color: RGB) {
1817
return numberToHex(color.r) + numberToHex(color.g) + numberToHex(color.b)
1918
}
20-
21-
export function hexToColor(hex: string) {
22-
const parsed = parseHex(hex)
23-
const color = new Color3(
24-
parseInt(parsed.slice(0, 2), 16) / 256,
25-
parseInt(parsed.slice(2, 4), 16) / 256,
26-
parseInt(parsed.slice(4, 6), 16) / 256
27-
)
28-
return color
29-
}

src/lib/sentry.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import * as Sentry from '@sentry/browser'
22
import { config } from '../config'
3+
import {
4+
browserTracingIntegration,
5+
dedupeIntegration,
6+
globalHandlersIntegration,
7+
linkedErrorsIntegration,
8+
} from '@sentry/browser'
9+
import { Env } from '@dcl/ui-env'
310

411
let sentryClient: ReturnType<typeof Sentry.init>
512

@@ -8,7 +15,21 @@ export function initSentry() {
815

916
sentryClient = Sentry.init({
1017
dsn: SENTRY_DSN,
11-
environment: import.meta.env.MODE,
18+
environment: config.get('ENVIRONMENT'),
19+
release: `${config.get('SENTRY_RELEASE_PREFIX', 'auth')}@${import.meta.env.VITE_REACT_APP_WEBSITE_VERSION}`,
20+
enabled: !config.is(Env.DEVELOPMENT),
21+
defaultIntegrations: false,
22+
integrations: [
23+
globalHandlersIntegration(),
24+
linkedErrorsIntegration(),
25+
dedupeIntegration(),
26+
browserTracingIntegration({
27+
enableLongTask: false,
28+
enableLongAnimationFrame: false,
29+
enableInp: false,
30+
enableElementTiming: false,
31+
}),
32+
],
1233
// Tag every event so we know it came from the iframe renderer
1334
initialScope: {
1435
tags: {

0 commit comments

Comments
 (0)