JavaScript Resources in Commerce

This topic describes general concepts of how JavaScript (JS) components are organized.

To address the problem of slow page loads, we exclude JavaScript from the page headers and we added the ability to use the RequireJS library.

RequireJS improves the perceived page load time because it allows JavaScript to load in the background; in particular, it enables asynchronous JavaScript loading.

Explore resources

Location

You can find Javascript components on the following levels:

data-variant=info
data-slots=text
The library level can only contain core application resources. Do not put custom JS files in the `lib/web` directory.

Specifying JS

We recommend specifying JavaScript resources in the templates rather than in the layout updates, to ensure that the resources are available for body of a page.

Accessing resources

JS resources are accessed using relative paths.

Example 1:

Example 2:

Example 3:

Relative paths are also used in for mapping and setting paths in requirejs-config.js configuration files.

Dependencies

To build a dependency on the third-party plugin, specify a shim in the following configuration files:

RequireJS library

Including RequireJS

To be available for the entire application instance, RequireJS library is included in the following layout files:

Including third-party libraries

To include a 3rd party library and use it within the entire website (using the Slick JS Library as an example):

  1. Download the library and copy slick.min.js to the <theme_path>/web/js folder.

  2. Copy slick.less and slick-theme.less to the <theme_path>/web/css/source folder. Also add both files to <theme_path>/web/css/source/_extend.less.

    @import "slick.less";
    @import "slick-theme.less";
    
  3. Create or update the theme's requirejs-config.js file.

    <theme_path>/requirejs-config.js

    var config = {
     paths: {
         slick: 'js/slick.min'
     },
     shim: {
         slick: {
             deps: ['jquery']
         }
       }
    };
    

After these steps, clear the cache and perform a static content deployment.

Result

We should now be able to use the Slick library, for example, on any list that we want to convert into a slider.

<ul class="my-list">
    <li>1st Element</li>
    <li>2nd Element</li>
    <li>3rd Element</li>
    <li>4th Element</li>
    <li>5th Element</li>
    <li>6th Element</li>
    <li>7th Element</li>
    <li>8th Element</li>
</ul>

<script>
   require([
      'jquery',
      'slick',
      'domReady!'
   ], function ($) {
      $(".my-list").slick({
         dots: true,
         infinite: true,
         speed: 300,
         slidesToShow: 4,
         slidesToScroll: 1
      });
   });
</script>

Slick Slider

Mapping resources

To make configurations more precise and specific to different modules and themes, requirejs-config.js files can be placed in different locations depending on your needs.

All configurations are collected and executed in the following order:

  1. Library configurations.
  2. Configurations at the module level.
  3. Configurations at the theme module level for the ancestor themes.
  4. Configurations at the theme module level for a current theme.
  5. Configurations at the theme level for the ancestor themes.
  6. Configurations at the theme level for the current theme.

The baseUrl parameter for RequireJS is specified in the following files: