Lazy Modules
Lazy Modules available from Framework7 version 3.4.0.
Lazy modules provide a way to make your web app startup time much faster, by loading initially only functionality required for home page/view, and load additional modules/components when navigating to pages that use them. This will make your initial app scripts and styles a way more smaller size, which is significant when you build a web app or PWA.
There are two type of modules available with Framework7. ES-modules and "browser modules". To use ES-modules you need to use bundler with import/export
support like Webpack or Rollup. Browser modules are designed only to be used when you don't use any bundler.
Modules API
To load Framework7 modules after it was initialized we need to use following app methods:
app.loadModule(module) - load module
- module - one of the following:
- object with Framework7 Plugin
- function that returns Framework7 Plugin
- string with module name to load (e.g.'searchbar'
)
- string with module path to load (e.g.'path/to/components/searchbar.js'
)
Method returns Promise
app.loadModules(modules) - load modules
- modules - array with modules, where each array item one of the described above
Method returns Promise
ES Modules
This method will only work if you use bundler like Webpack or Rollup.
First of all, we need to realize what modules our app requires to display initial page and import them:
// import core framework with core components only:
import Framework7 from 'framework7';
// import framework7 modules/components we need on initial page
import Searchbar from 'framework7/components/searchbar';
import Accordion from 'framework7/components/accordion';
// install core modules
Framework7.use([Searchbar, Accordion]);
// init app
var app = new Framework7({
// f7 params
});
Later when we need to install additional F7 module we can use dynamic imports:
import('framework7/components/gauge')
.then(module => app.loadModule(module.default))
.then(() => {
// module loaded and we can use gauge api
app.gauge.create(/* ... */)
})
If we need to load few modules at a time:
Promise
.all([
import('framework7/components/gauge'),
import('framework7/components/calendar')
])
.then((modules) => {
// loaded module will be at ".default" prop of import result
var modulesToLoad = modules.map(module => module.default);
return app.loadModules(modulesToLoad);
})
.then(() => {
// modules loaded and we can use their api
app.gauge.create(/* ... */)
app.calendar.create(/* ... */)
})
It may be not very convenient to write it every time so we can make a function for that:
function loadF7Modules(moduleNames) {
var modulesToLoad = moduleNames.map((moduleName) => {
return import(`framework7/components/${moduleName}`);
});
return Promise.all(modulesToLoad)
.then((modules) => {
return app.loadModules(modules.map(module => module.default));
})
}
And we can use it like:
loadF7Modules(['gauge', 'calendar']).then(() => {
// modules loaded and we can use their api
app.gauge.create(/* ... */)
app.calendar.create(/* ... */)
});
If we need to preload modules for specific route then route's async
is the best fit for it:
var routes = [
{
path: '/',
url: './index.html',
},
/* Page where we need Gauge and Calendar modules to be loaded */
{
path: '/gauge-calendar/',
async: function ({ resolve }) {
// load modules
loadF7Modules(['gauge', 'calendar']).then(() => {
// resolve route
resolve({
componentUrl: './gauge-calendar.html',
});
});
}
}
]
The following ES-module components are available for importing (all other components are part of the core):
Component | Path |
---|---|
Dialog | framework7/components/dialog |
Popup | framework7/components/popup |
LoginScreen | framework7/components/login-screen |
Popover | framework7/components/popover |
Actions | framework7/components/actions |
Sheet | framework7/components/sheet |
Toast | framework7/components/toast |
Preloader | framework7/components/preloader |
Progressbar | framework7/components/progressbar |
Sortable | framework7/components/sortable |
Swipeout | framework7/components/swipeout |
Accordion | framework7/components/accordion |
ContactsList | framework7/components/contacts-list |
VirtualList | framework7/components/virtual-list |
ListIndex | framework7/components/list-index |
Timeline | framework7/components/timeline |
Tabs | framework7/components/tabs |
Panel | framework7/components/panel |
Card | framework7/components/card |
Chip | framework7/components/chip |
Form | framework7/components/form |
Input | framework7/components/input |
Checkbox | framework7/components/checkbox |
Radio | framework7/components/radio |
Toggle | framework7/components/toggle |
Range | framework7/components/range |
Stepper | framework7/components/stepper |
SmartSelect | framework7/components/smart-select |
Grid | framework7/components/grid |
Calendar | framework7/components/calendar |
Picker | framework7/components/picker |
InfiniteScroll | framework7/components/infinite-scroll |
PullToRefresh | framework7/components/pull-to-refresh |
DataTable | framework7/components/data-table |
Fab | framework7/components/fab |
Searchbar | framework7/components/searchbar |
Messages | framework7/components/messages |
Messagebar | framework7/components/messagebar |
Swiper | framework7/components/swiper |
PhotoBrowser | framework7/components/photo-browser |
Notification | framework7/components/notification |
Autocomplete | framework7/components/autocomplete |
Tooltip | framework7/components/tooltip |
Gauge | framework7/components/gauge |
Skeleton | framework7/components/skeleton |
Pie Chart | framework7/components/pie-chart |
Area Chart | framework7/components/area-chart |
Typography | framework7/components/typography |
Text Editor | framework7/components/text-editor |
Breadcrumbs | framework7/components/breadcrumbs |