Lazy Modules - 图1

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.

Firs of all, we need to realize what modules our app requires to display initial page and import them:

  1. // import core framework with core components only:
  2. import Framework7 from 'framework7';
  3. // import framework7 modules/components we need on initial page
  4. import Searchbar from 'framework7/components/searchbar/searchbar.js';
  5. import Accordion from 'framework7/components/accordion/accordion.js';
  6. // install core modules
  7. Framework7.use([Searchbar, Accordion]);
  8. // init app
  9. var app = new Framework7({
  10. // f7 params
  11. });

Later when we need to install additional F7 module we can use dynamic imports:

  1. import('framework7/components/gauge/gauge.js')
  2. .then(module => app.loadModule(module.default))
  3. .then(() => {
  4. // module loaded and we can use gauge api
  5. app.gauge.create(/* ... */)
  6. })

If we need to load few modules at a time:

  1. Promise
  2. .all([
  3. import('framework7/components/gauge/gauge.js'),
  4. import('framework7/components/calendar/calendar.js')
  5. ])
  6. .then((modules) => {
  7. // loaded module will be at ".default" prop of import result
  8. var modulesToLoad = modules.map(module => module.default);
  9. return app.loadModules(modulesToLoad);
  10. })
  11. .then(() => {
  12. // modules loaded and we can use their api
  13. app.gauge.create(/* ... */)
  14. app.calendar.create(/* ... */)
  15. })

It may be not very convenient to write it every time so we can make a function for that:

  1. function loadF7Modules(moduleNames) {
  2. var modulesToLoad = moduleNames.map((moduleName) => {
  3. return import(`framework7/components/${moduleName}/${moduleName}.js`);
  4. });
  5. return Promise.all(modulesToLoad)
  6. .then((modules) => {
  7. return app.loadModules(modules.map(module => module.default));
  8. })
  9. }

And we can use it like:

  1. loadF7Modules(['gauge', 'calendar']).then(() => {
  2. // modules loaded and we can use their api
  3. app.gauge.create(/* ... */)
  4. app.calendar.create(/* ... */)
  5. });

If we need to preload modules for specific route then route’s async is the best fit for it:

  1. var routes = [
  2. {
  3. path: '/',
  4. url: './index.html',
  5. },
  6. /* Page where we need Gauge and Calendar modules to be loaded */
  7. {
  8. path: '/gauge-calendar/',
  9. async: function (routeTo, routeFrom, resolve, reject) {
  10. // load modules
  11. loadF7Modules(['gauge', 'calendar']).then(() => {
  12. // resolve route
  13. resolve({
  14. componentUrl: './gauge-calendar.html',
  15. });
  16. });
  17. }
  18. }
  19. ]

The following ES-module components are available for importing (all other components are part of the core):

ComponentPath
Dialogframework7/components/dialog/dialog.js
Popupframework7/components/popup/popup.js
LoginScreenframework7/components/login-screen/login-screen.js
Popoverframework7/components/popover/popover.js
Actionsframework7/components/actions/actions.js
Sheetframework7/components/sheet/sheet.js
Toastframework7/components/toast/toast.js
Preloaderframework7/components/preloader/preloader.js
Progressbarframework7/components/progressbar/progressbar.js
Sortableframework7/components/sortable/sortable.js
Swipeoutframework7/components/swipeout/swipeout.js
Accordionframework7/components/accordion/accordion.js
ContactsListframework7/components/contacts-list/contacts-list.js
VirtualListframework7/components/virtual-list/virtual-list.js
ListIndexframework7/components/list-index/list-index.js
Timelineframework7/components/timeline/timeline.js
Tabsframework7/components/tabs/tabs.js
Panelframework7/components/panel/panel.js
Cardframework7/components/card/card.js
Chipframework7/components/chip/chip.js
Formframework7/components/form/form.js
Inputframework7/components/input/input.js
Checkboxframework7/components/checkbox/checkbox.js
Radioframework7/components/radio/radio.js
Toggleframework7/components/toggle/toggle.js
Rangeframework7/components/range/range.js
Stepperframework7/components/stepper/stepper.js
SmartSelectframework7/components/smart-select/smart-select.js
Gridframework7/components/grid/grid.js
Calendarframework7/components/calendar/calendar.js
Pickerframework7/components/picker/picker.js
InfiniteScrollframework7/components/infinite-scroll/infinite-scroll.js
PullToRefreshframework7/components/pull-to-refresh/pull-to-refresh.js
Lazyframework7/components/lazy/lazy.js
DataTableframework7/components/data-table/data-table.js
Fabframework7/components/fab/fab.js
Searchbarframework7/components/searchbar/searchbar.js
Messagesframework7/components/messages/messages.js
Messagebarframework7/components/messagebar/messagebar.js
Swiperframework7/components/swiper/swiper.js
PhotoBrowserframework7/components/photo-browser/photo-browser.js
Notificationframework7/components/notification/notification.js
Autocompleteframework7/components/autocomplete/autocomplete.js
Tooltipframework7/components/tooltip/tooltip.js
Gaugeframework7/components/gauge/gauge.js
Skeletonframework7/components/skeleton/skeleton.js
Menuframework7/components/menu/menu.js
Viframework7/components/vi/vi.js
Typographyframework7/components/typography/typography.js

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.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. ...
  5. <!-- Path to Framework7 Core Library CSS -->
  6. <link rel="stylesheet" href="path/to/framework7/css/framework7.min.css">
  7. <!-- Path to your custom app styles-->
  8. <link rel="stylesheet" href="path/to/my-app.css">
  9. </head>
  10. <body>
  11. <div id="app">
  12. ...
  13. </div>
  14. <!-- Path to Framework7 Core Library JS-->
  15. <script type="text/javascript" src="path/to/framework7/js/framework7.min.js"></script>
  16. <!-- Path to your app js-->
  17. <script type="text/javascript" src="path/to/my-app.js"></script>
  18. </body>
  19. </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:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. ...
  5. <!-- Path to Framework7 Core Library CSS -->
  6. <link rel="stylesheet" href="path/to/framework7/css/framework7.min.css">
  7. <!-- Include modules required for initial page -->
  8. <link rel="stylesheet" href="path/to/framework7/components/accordion.css">
  9. <link rel="stylesheet" href="path/to/framework7/components/searchbar.css">
  10. <!-- Path to your custom app styles-->
  11. <link rel="stylesheet" href="path/to/my-app.css">
  12. </head>
  13. <body>
  14. <div id="app">
  15. ...
  16. </div>
  17. <!-- Path to Framework7 Core Library JS-->
  18. <script type="text/javascript" src="path/to/framework7/js/framework7.min.js"></script>
  19. <!-- Include modules required for initial page -->
  20. <script type="text/javascript" src="path/to/framework7/components/accordion.js"></script>
  21. <script type="text/javascript" src="path/to/framework7/components/searchbar.js"></script>
  22. <!-- Path to your app js-->
  23. <script type="text/javascript" src="path/to/my-app.js"></script>
  24. </body>
  25. </html>

Now when we init Framework7 app we need to specify where is the rest of modules in lazyModulesPath parameter (path to /components/ folder):

  1. var app = new Framework7({
  2. // ...
  3. lazyModulesPath: 'path/to/framework7/components',
  4. });

Now we can just load module by its name (it will automatically fetch file with such file name in specified lazyModulesPath location):

  1. app.loadModules(['calendar', 'gauge']).then(() => {
  2. // modules loaded and we can use their api
  3. app.gauge.create(/* ... */)
  4. app.calendar.create(/* ... */)
  5. });

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.js
  • path/to/framework7/components/calendar.css
  • path/to/framework7/components/gauge.js
  • path/to/framework7/components/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:

  1. var routes = [
  2. {
  3. path: '/',
  4. url: './index.html',
  5. },
  6. /* Page where we need Gauge and Calendar modules to be loaded */
  7. {
  8. path: '/gauge-calendar/',
  9. modules: ['gauge', 'calendar'], // will load these components before loading route
  10. componentUrl: './gauge-calendar.html',
  11. }
  12. ]

Or the same but using async route like in example with ES modules:

  1. var routes = [
  2. {
  3. path: '/',
  4. url: './index.html',
  5. },
  6. /* Page where we need Gauge and Calendar modules to be loaded */
  7. {
  8. path: '/gauge-calendar/',
  9. modules: ['gauge', 'calendar'], // will load these components before loading route
  10. async: function(routeTo, routeFrom, resolve, reject) {
  11. app.loadModules(['gauge', 'calendar']).then(() => {
  12. resolve({
  13. componentUrl: './gauge-calendar.html',
  14. });
  15. });
  16. },
  17. }
  18. ]

The following browser modules-components are available for loading (all other components are part of the core):

ComponentNamePath
Dialogdialogframework7/components/dialog.js
Popuppopupframework7/components/popup.js
LoginScreenlogin-screenframework7/components/login-screen.js
Popoverpopoverframework7/components/popover.js
Actionsactionsframework7/components/actions.js
Sheetsheetframework7/components/sheet.js
Toasttoastframework7/components/toast.js
Preloaderpreloaderframework7/components/preloader.js
Progressbarprogressbarframework7/components/progressbar.js
Sortablesortableframework7/components/sortable.js
Swipeoutswipeoutframework7/components/swipeout.js
Accordionaccordionframework7/components/accordion.js
ContactsListcontacts-listframework7/components/contacts-list.js
VirtualListvirtual-listframework7/components/virtual-list.js
ListIndexlist-indexframework7/components/list-index.js
Timelinetimelineframework7/components/timeline.js
Tabstabsframework7/components/tabs.js
Panelpanelframework7/components/panel.js
Cardcardframework7/components/card.js
Chipchipframework7/components/chip.js
Formformframework7/components/form.js
Inputinputframework7/components/input.js
Checkboxcheckboxframework7/components/checkbox.js
Radioradioframework7/components/radio.js
Toggletoggleframework7/components/toggle.js
Rangerangeframework7/components/range.js
Stepperstepperframework7/components/stepper.js
SmartSelectsmart-selectframework7/components/smart-select.js
Gridgridframework7/components/grid.js
Calendarcalendarframework7/components/calendar.js
Pickerpickerframework7/components/picker.js
InfiniteScrollinfinite-scrollframework7/components/infinite-scroll.js
PullToRefreshpull-to-refreshframework7/components/pull-to-refresh.js
Lazylazyframework7/components/lazy.js
DataTabledata-tableframework7/components/data-table.js
Fabfabframework7/components/fab.js
Searchbarsearchbarframework7/components/searchbar.js
Messagesmessagesframework7/components/messages.js
Messagebarmessagebarframework7/components/messagebar.js
Swiperswiperframework7/components/swiper.js
PhotoBrowserphotoframework7/components/browser/photo-browser.js
Notificationnotificationframework7/components/notification.js
Autocompleteautocompleteframework7/components/autocomplete.js
Tooltiptooltipframework7/components/tooltip.js
Gaugegaugeframework7/components/gauge.js
Skeletonskeletonframework7/components/skeleton.js
Menumenuframework7/components/menu.js
Viviframework7/components/vi.js
Typographytypographyframework7/components/typography.js