Measure, Optimize, Monitor
load time
javascript
<Route exact path="/" component={LazyLanding} />
import React from 'react';
import Loadable from 'react-loadable';
import RouterLoading from '../RouterLoading.component';
const LazyLandingPage = Loadable({
loader: () => import('./Landing.page'),
modules: ['./Landing.page'],
webpack: () => [require.resolveWeak('./Landing.page')],
loading: RouterLoading
});
export default (props) => <LazyLandingPage {...props} />;
React
Angular
const routes: Routes = [
{
path: 'customers',
loadChildren: './customers/customers.module#CustomersModule'
},
{
path: 'orders',
loadChildren: './orders/orders.module#OrdersModule'
},
{
path: '',
redirectTo: '',
pathMatch: 'full'
}
];
How about loading it just when I need it?
AKA: on button click
onClick() {
import('../Swal.service').then(SwalService => {
SwalService.default(
/* ... */
);
});
}
React & Angular
javascript
class LazyEditor {
constructor() {
this.state = {
Editor: () => <div />
}
}
componentDidMount() {
import('./MonacoEditor.jsx').then(MonacoEditor => {
this.setState({
Editor: MonacoEditor.default
});
});
}
render() {
return <this.state.Editor />
}
}
Include just the code you actually reference and use!
// package.json
{
"sideEffects": [
"*.css",
"*.scss"
]
}
load time
javascript
load time
javascript
...because now the UX kinda sucks!
self.addEventListener('fetch', function onFetch(event) {
if (event.request.url.indexOf(location.origin) === 0) {
event.respondWith(cacheOrNetwork(event));
}
});
100% SSR - which I didn't have time to implement 😅
or almost SSR - to help eliminate useless requests 😎
<body>
<script>
window.__PRELOADED_STATE__ = {
"user": {
"username": "iampava",
"email": "pava@iampava.com",
"avatarUrl": "/assets/images/avatars/i_avatar.png",
"hasFullSubscription": true,
"notifications": {
"unread": "20",
"list": null
}
},
"allTeams": [],
"currentTeam": null,
"joinTeam": null,
"exercises": {
"current": null
}
}
</script>
<script type="text/javascript" src="/dist/devdrive.7e79696e5c8a4011ba42.js"></script>
</body>
{
"bundlesize": [{
"path": "./dist/**/!(*.worker).js",
"compression": "none",
"maxSize": "300 kB"
}],
"scripts": {
"perf-test": "bundlesize"
}
}
$ lighthouse http://localhost:1234
--output json --output-path ./audit.json
but now what...?