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:

// 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/searchbar.js';
import Accordion from 'framework7/components/accordion/accordion.js';

// 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/gauge.js')
  .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/gauge.js'),
    import('framework7/components/calendar/calendar.js')
  ])
  .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}/${moduleName}.js`);
  });
  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 (routeTo, routeFrom, resolve, reject) {
      // 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):

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.

<!DOCTYPE html>
<html>
  <head>
    ...
    <!-- Path to Framework7 Core Library CSS -->
    <link rel="stylesheet" href="path/to/framework7/css/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/js/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/css/framework7.min.css">
    <!-- Include modules required for initial page -->
    <link rel="stylesheet" href="path/to/framework7/components/accordion.css">
    <link rel="stylesheet" href="path/to/framework7/components/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/js/framework7.min.js"></script>
    <!-- Include modules required for initial page -->
    <script type="text/javascript" src="path/to/framework7/components/accordion.js"></script>
    <script type="text/javascript" src="path/to/framework7/components/searchbar.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.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:

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(routeTo, routeFrom, resolve, reject) {
      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):

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