You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following examples show how you can lazy load Riot.js components using modern javascript bundlers like Webpack or Rollup.
You can lazy load any component providing a fallback component during the loading process for example:
<app>
<username={state.name}/>
<sidebar/>
<script>importlazyfrom'@riotjs/lazy'importLoaderfrom'./my-loader.riot'exportdefault { components: {// use a fallback loader user:lazy(Loader, () =>import('./user.riot'))// just lazy load the sidebar sidebar:lazy(() =>import('./sidebar.riot')) } } </script>
</app>
When the component is loaded, Lazy will dispatch a 'load' event from the component root element.
This can be useful e.g. for removing splashscreen on app start:
<app>
<username={state.name} onload={ removeSplashscreen }/>
<script>importlazyfrom'@riotjs/lazy'importLoaderfrom'./my-loader.riot'exportdefault { components: {// use a fallback loader user:lazy(Loader, () =>import('./user.riot')) },removeSplashscreen() {// the splashscreen, in this example, is create in pure html// in the main page, to avoid blank page loadingconstsplashscreen=document.querySelector("#splashscreen");if (!splashscreen) {return; }splashcreen.parentElement.removeChild(splashscreen); } } </script>
</app>
Lazy loading Riot.js components is recommended combined with @riotjs/route
<app>
<router>
<routepath="/user/:name">
<!-- this component will be loaded only when the route will be matched -->
<username={route[0]}/>
</route>
</router>
<script>importlazyfrom'@riotjs/lazy'importLoaderfrom'./my-loader.riot'exportdefault { components: { user:lazy(Loader, () =>import('./user.riot')) } } </script>
</app>