Code splitting and lazy loading for faster load times.

master
Håvar Aambø Fosstveit 2019-11-04 14:34:27 +01:00
parent 8bfdcab709
commit 673c3f989e
3 changed files with 107 additions and 12 deletions

View File

@ -1,15 +1,30 @@
import io from 'socket.io-client'; // import io from 'socket.io-client';
import * as mediasoupClient from 'mediasoup-client'; // import * as mediasoupClient from 'mediasoup-client';
import WebTorrent from 'webtorrent'; // import WebTorrent from 'webtorrent';
import createTorrent from 'create-torrent'; // import createTorrent from 'create-torrent';
import saveAs from 'file-saver'; // import saveAs from 'file-saver';
import Logger from './Logger'; import Logger from './Logger';
import hark from 'hark'; import hark from 'hark';
import ScreenShare from './ScreenShare'; // import ScreenShare from './ScreenShare';
import Spotlights from './Spotlights'; // import Spotlights from './Spotlights';
import { getSignalingUrl } from './urlFactory'; import { getSignalingUrl } from './urlFactory';
import * as requestActions from './actions/requestActions'; import * as requestActions from './actions/requestActions';
import * as stateActions from './actions/stateActions'; import * as stateActions from './actions/stateActions';
let WebTorrent;
let createTorrent;
let saveAs;
let mediasoupClient;
let io;
let ScreenShare;
let Spotlights;
const { const {
turnServers, turnServers,
requestTimeout, requestTimeout,
@ -98,7 +113,7 @@ export default class RoomClient
this._forceTcp = forceTcp; this._forceTcp = forceTcp;
// Torrent support // Torrent support
this._torrentSupport = WebTorrent.WEBRTC_SUPPORT; this._torrentSupport = null;
// Whether simulcast should be used. // Whether simulcast should be used.
this._useSimulcast = useSimulcast; this._useSimulcast = useSimulcast;
@ -171,7 +186,7 @@ export default class RoomClient
// @type {Map<String, mediasoupClient.Consumer>} // @type {Map<String, mediasoupClient.Consumer>}
this._consumers = new Map(); this._consumers = new Map();
this._screenSharing = ScreenShare.create(device); this._screenSharing = null;
this._screenSharingProducer = null; this._screenSharingProducer = null;
@ -1130,8 +1145,66 @@ export default class RoomClient
stateActions.setMyRaiseHandStateInProgress(false)); stateActions.setMyRaiseHandStateInProgress(false));
} }
async _loadDynamicImports()
{
({ default: WebTorrent } = await import(
/* webpackPrefetch: true */
/* webpackChunkName: "webtorrent" */
'webtorrent'
));
({ default: createTorrent } = await import(
/* webpackPrefetch: true */
/* webpackChunkName: "create-torrent" */
'create-torrent'
));
({ default: saveAs } = await import(
/* webpackPrefetch: true */
/* webpackChunkName: "file-saver" */
'file-saver'
));
({ default: ScreenShare } = await import(
/* webpackPrefetch: true */
/* webpackChunkName: "screensharing" */
'./ScreenShare'
));
({ default: Spotlights } = await import(
/* webpackPrefetch: true */
/* webpackChunkName: "spotlights" */
'./Spotlights'
));
mediasoupClient = await import(
/* webpackPrefetch: true */
/* webpackChunkName: "mediasoup" */
'mediasoup-client'
);
({ default: io } = await import(
/* webpackPrefetch: true */
/* webpackChunkName: "socket.io" */
'socket.io-client'
));
}
async join({ joinVideo }) async join({ joinVideo })
{ {
await this._loadDynamicImports();
this._torrentSupport = WebTorrent.WEBRTC_SUPPORT;
this._screenSharing = ScreenShare.create(this._device);
this._signalingSocket = io(this._signalingUrl); this._signalingSocket = io(this._signalingUrl);
this._spotlights = new Spotlights(this._maxSpotlights, this._signalingSocket); this._spotlights = new Spotlights(this._maxSpotlights, this._signalingSocket);

View File

@ -1,8 +1,11 @@
import React from 'react'; import React, { useEffect, Suspense } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import Room from './Room';
import JoinDialog from './JoinDialog'; import JoinDialog from './JoinDialog';
import LoadingView from './LoadingView';
import { ReactLazyPreload } from './ReactLazyPreload';
const Room = ReactLazyPreload(() => import(/* webpackChunkName: "room" */ './Room'));
const App = (props) => const App = (props) =>
{ {
@ -10,6 +13,13 @@ const App = (props) =>
room room
} = props; } = props;
useEffect(() =>
{
Room.preload();
return;
}, []);
if (!room.joined) if (!room.joined)
{ {
return ( return (
@ -19,7 +29,9 @@ const App = (props) =>
else else
{ {
return ( return (
<Room /> <Suspense fallback={<LoadingView />}>
<Room />
</Suspense>
); );
} }
}; };

View File

@ -0,0 +1,10 @@
import React from 'react';
export const ReactLazyPreload = (importStatement) =>
{
const Component = React.lazy(importStatement);
Component.preload = importStatement;
return Component;
};