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 |
Lazy | framework7/components/lazy |
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 |
Menu | framework7/components/menu |
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 |
Browser Modules
Browser modules are intended to be used in development setup without bundlers (like Webpack or Rollup).
First of all, in our main app layout we need to use so called minimal core Framework7 library instead of framework7-bundle.js
and framework7-bundle.css
scripts and styles that contains whole framework.
<!DOCTYPE html>
<html>
<head>
...
<!-- Path to Framework7 Core Library CSS -->
<link rel="stylesheet" href="path/to/framework7/framework7.min.css">
<!-- Path to your custom app styles-->
<link rel="stylesheet" href="path/to/my-app.css">
</head>
<body>
<div id="app">
...
</div>
<!-- Path to Framework7 Core Library JS-->
<script type="text/javascript" src="path/to/framework7/framework7.min.js"></script>
<!-- Path to your app js-->
<script type="text/javascript" src="path/to/my-app.js"></script>
</body>
</html>
We also need to inclued modules/components that we need on initial page after framework7
styles and script. If we need Searchbar and Accordion then we need to include them in app layout:
<!DOCTYPE html>
<html>
<head>
...
<!-- Path to Framework7 Core Library CSS -->
<link rel="stylesheet" href="path/to/framework7/framework7.min.css">
<!-- Include modules required for initial page -->
<link rel="stylesheet" href="path/to/framework7/components/accordion/accordion.css">
<link rel="stylesheet" href="path/to/framework7/components/searchbar/searchbar.css">
<!-- Path to your custom app styles-->
<link rel="stylesheet" href="path/to/my-app.css">
</head>
<body>
<div id="app">
...
</div>
<!-- Path to Framework7 Core Library JS-->
<script type="text/javascript" src="path/to/framework7/framework7.min.js"></script>
<!-- Include modules required for initial page -->
<script type="text/javascript" src="path/to/framework7/components/accordion/accordion.lazy.js"></script>
<script type="text/javascript" src="path/to/framework7/components/searchbar/searchbar.lazy.js"></script>
<!-- Path to your app js-->
<script type="text/javascript" src="path/to/my-app.js"></script>
</body>
</html>
Now when we init Framework7 app we need to specify where is the rest of modules in lazyModulesPath
parameter (path to /components/
folder):
var app = new Framework7({
// ...
lazyModulesPath: 'path/to/framework7/components',
});
Now we can just load module by its name (it will automatically fetch file with such file name in specified lazyModulesPath
location):
app.loadModules(['calendar', 'gauge']).then(() => {
// modules loaded and we can use their api
app.gauge.create(/* ... */)
app.calendar.create(/* ... */)
});
Note, that browser modules also load modules styles automatically. So loading gauge.js
will also automatically load gauge.css
stylesheet.
So the above expression app.loadModules(['calendar', 'gauge'])
will load the following files:
- path/to/framework7/components/calendar/calendar.lazy.js
- path/to/framework7/components/calendar/calendar.css
- path/to/framework7/components/gauge/gauge.lazy.js
- path/to/framework7/components/gauge/gauge.css
When we need to preload modules for specific route and we use browser modules, then we can just use route's modules
property:
var routes = [
{
path: '/',
url: './index.html',
},
/* Page where we need Gauge and Calendar modules to be loaded */
{
path: '/gauge-calendar/',
modules: ['gauge', 'calendar'], // will load these components before loading route
componentUrl: './gauge-calendar.html',
}
]
Or the same but using async
route like in example with ES modules:
var routes = [
{
path: '/',
url: './index.html',
},
/* Page where we need Gauge and Calendar modules to be loaded */
{
path: '/gauge-calendar/',
modules: ['gauge', 'calendar'], // will load these components before loading route
async: function({ resolve }) {
app.loadModules(['gauge', 'calendar']).then(() => {
resolve({
componentUrl: './gauge-calendar.html',
});
});
},
}
]
The following browser modules-components are available for loading (all other components are part of the core):
Component | Name | Path |
---|---|---|
Dialog | dialog | framework7/components/dialog/dialog.lazy.js |
Popup | popup | framework7/components/popup/popup.lazy.js |
LoginScreen | login-screen | framework7/components/login-screen/login-screen.lazy.js |
Popover | popover | framework7/components/popover/popover.lazy.js |
Actions | actions | framework7/components/actions/actions.lazy.js |
Sheet | sheet | framework7/components/sheet/sheet.lazy.js |
Toast | toast | framework7/components/toast/toast.lazy.js |
Preloader | preloader | framework7/components/preloader/preloader.lazy.js |
Progressbar | progressbar | framework7/components/progressbar/progressbar.lazy.js |
Sortable | sortable | framework7/components/sortable/sortable.lazy.js |
Swipeout | swipeout | framework7/components/swipeout/swipeout.lazy.js |
Accordion | accordion | framework7/components/accordion/accordion.lazy.js |
ContactsList | contacts-list | framework7/components/contacts-list/contacts-list.lazy.js |
VirtualList | virtual-list | framework7/components/virtual-list/virtual-list.lazy.js |
ListIndex | list-index | framework7/components/list-index/list-index.lazy.js |
Timeline | timeline | framework7/components/timeline/timeline.lazy.js |
Tabs | tabs | framework7/components/tabs/tabs.lazy.js |
Panel | panel | framework7/components/panel/panel.lazy.js |
Card | card | framework7/components/card/card.lazy.js |
Chip | chip | framework7/components/chip/chip.lazy.js |
Form | form | framework7/components/form/form.lazy.js |
Input | input | framework7/components/input/input.lazy.js |
Checkbox | checkbox | framework7/components/checkbox/checkbox.lazy.js |
Radio | radio | framework7/components/radio/radio.lazy.js |
Toggle | toggle | framework7/components/toggle/toggle.lazy.js |
Range | range | framework7/components/range/range.lazy.js |
Stepper | stepper | framework7/components/stepper/stepper.lazy.js |
SmartSelect | smart-select | framework7/components/smart-select/smart-select.lazy.js |
Grid | grid | framework7/components/grid/grid.lazy.js |
Calendar | calendar | framework7/components/calendar/calendar.lazy.js |
Picker | picker | framework7/components/picker/picker.lazy.js |
InfiniteScroll | infinite-scroll | framework7/components/infinite-scroll/infinite-scroll.lazy.js |
PullToRefresh | pull-to-refresh | framework7/components/pull-to-refresh/pull-to-refresh.lazy.js |
Lazy | lazy | framework7/components/lazy/lazy.lazy.js |
DataTable | data-table | framework7/components/data-table/data-table.lazy.js |
Fab | fab | framework7/components/fab/fab.lazy.js |
Searchbar | searchbar | framework7/components/searchbar/searchbar.lazy.js |
Messages | messages | framework7/components/messages/messages.lazy.js |
Messagebar | messagebar | framework7/components/messagebar/messagebar.lazy.js |
Swiper | swiper | framework7/components/swiper/swiper.lazy.js |
PhotoBrowser | photo-browser | framework7/components/photo-browser/photo-browser.lazy.js |
Notification | notification | framework7/components/notification/notification.lazy.js |
Autocomplete | autocomplete | framework7/components/autocomplete/autocomplete.lazy.js |
Tooltip | tooltip | framework7/components/tooltip/tooltip.lazy.js |
Gauge | gauge | framework7/components/gauge/gauge.lazy.js |
Skeleton | skeleton | framework7/components/skeleton/skeleton.lazy.js |
Menu | menu | framework7/components/menu/menu.lazy.js |
Pie Chart | pie-chart | framework7/components/pie-chart/pie-chart.lazy.js |
Area Chart | area-chart | framework7/components/area-chart/area-chart.lazy.js |
Typography | typography | framework7/components/typography/typography.lazy.js |
Text Editor | text-editor | framework7/components/text-editor/text-editor.lazy.js |
Breadcrumbs | breadcrumbs | framework7/components/breadcrumbs/breadcrumbs.lazy.js |