Using JavaScript in a Stencil Theme
Bundling and Minification
A small web application, such as a theme, can include rich user interactions that depend on many small JavaScript and CSS modules. If we were to embed each of the JavaScript modules in a template file with a separate <script> tag, the shopper’s browser would need to make separate HTTP requests to retrieve each module.
In some cases, it would take longer to set up the HTTP request than to download the small JavaScript module, leading to slower load times. On mobile devices, slow load times can be especially frustrating.
Bundling
To solve this problem Stencil, like other modern front-end frameworks, bundles all the JavaScript modules into a single file, allowing the shopper’s browser to make only a single HTTP request. After the browser has downloaded the bundle of JavaScript modules the browser caches them, speeding up the rest of the shopper’s session.
Minification
Beyond reducing the number of HTTP calls required to fetch all the required JavaScript modules, we can reduce the size of the individual JavaScript modules through minification. JavaScript minification removes white space and comments, shortens variable and function names, removes dead code, and more. The goal in all cases is to reduce the amount of bandwidth necessary to transmit the JavaScript module to the browser.
Options for Developers
When you add JavaScript to a theme, use one of the following techniques, so that Stencil will automatically bundle and minify your modules.
- Using npm: Add third-party JavaScript modules to your theme with npm where possible
- Place modules in assets/js/: For a JavaScript module that is not distributed via npm, add this module to your theme by creating a subdirectory within assets/js/ that contains your module
- Theme-specific JavaScript modules: Stencil themes include their own custom JavaScript modules for most page types. You can alter these page-type-specific modules by editing the files in assets/js/theme/.js*
These techniques are outlined in detail in the next sections.
Using npm
Many third-party JavaScript components are distributed with npm. When you use the npm command-line utility to add a JavaScript component to your theme, Stencil will automatically bundle and minify the component.
To enable this bundling and minification, run each module’s npm install command from the root directory of your theme (only for the module you want to add).
Taking over from npm
The npm package manager facilitates the management of third-party JavaScript components by placing each JavaScript component in the correct directories.
However, as a developer you will still need to edit your theme files to wire up the JavaScript component to expose it on your storefront.
Page types and JavaScript API
Stencil themes include an API for running JavaScript on a per-page basis. To properly write JavaScript for your theme, you will have the following page types available to you:
- account_orderstatus
- account_order
- account_addressbook
- shippingaddressform
- account_new_return
- add-wishlist
- account_recentitems
- account_downloaditem
- editaccount
- account_inbox
- account_saved_return
- account_returns
- account_paymentmethods
- account_addpaymentmethod
- account_editpaymentmethod
- login
- createaccount_thanks
- createaccount
- getnewpassword
- forgotpassword
- blog
- blog_post
- brand
- brands
- cart
- checkout
- category
- compare
- page_contact_form
- error
- 404
- giftcertificates
- giftcertificates_balance
- giftcertificates_redeem
- default
- page
- product
- amp_product_options
- search
- rss
- sitemap
- newsletter_subscribe
- wishlist
- wishlists
For further information, you can view a list of all the page types identified by app.js in Cornerstone.
These page types correspond to the pages within your theme. Each page type maps to an ES6 module that extends the base PageManager abstract class.
Example:
Note that the class name (“Blog”) is arbitrary. Mapping in the pageClasses object in assets/js/app.js is what determines which PageManager subclass will load on that page type.
Placing Modules in assets/js/
Developers can freely create subdirectories within assets/js/, to contain new JavaScript modules. The constraint is that all JavaScript files in each module must use the .js file extension.
In some advanced cases, it is possible to use other file types if you reconfigure webpack to transpile them. The blog below demonstrates how to reconfigure webpack to transpile .jsx files so that you can implement React code directly in the theme.
Create Custom BigCommerce Templates with React in Stencil

As a Solution Architect at BigCommerce, I meet clients and agencies who want to incorporate React into their Stencil theme. Maybe you want to utilize your existing component library, or React is just your preferred front-end framework.
Theme-Specific JavaScript Modules
In your theme’s assets/js/theme/ subdirectory, you will find a tree of JavaScript files.
Each file is a JavaScript module. Some modules are for specific page types. Others are common modules that can be used in other modules. Still, others are global modules that are available on every page.
Mapping Page Types to JavaScript Modules
To find the mapping from page types to modules in assets/js/theme/, examine the pageClasses object in the file:assets/js/app.js.
Each =>import(…) function within this class maps a page type to the entry module for that page type. For example, when the cart page type is loaded in the browser the JavaScript module at assets/theme/js/cart.js will be loaded.
Mapping example in app.js
Below is an excerpt of mappings from the Cornerstone base theme’s assets/js/app.js.
Mapping example in cart.js
Inside the cart module (assets/js/theme/cart.js) other modules are imported, and custom JavaScript methods for the cart module are created for the Cart class.
Below is an excerpt from Cornerstone’s assets/js/theme/cart.js file. See the full code here.
Mapping Custom Templates to JavaScript Modules
If you add custom page templates to your theme, you can edit the same assets/js/app.js file to map each custom template to an appropriate JavaScript module.
In order to successfully map your custom module to a custom template file, that file must do the following:
- Inject the custom template
- Load webpack
- Load the main theme bundle
- Load stencilBootstrap
In the following example, we will map a custom JavaScript file, assets/js/theme/custom.js to a custom product page file templates/pages/custom/product/customProd.html.
This is a basic module that creates a class called Custom which extends the PageManager class.
The sample code below is the custom template that would live somewhere in templates/pages/custom/product/*.
It is a good idea to pull in {{>layout/base}} to your custom template file because of these requirements.
Finally, use the customClasses function in assets/js/app.js to map the custom page file to your custom template. Your app.js file should look like this:
Summary
To review, the basics of using JavaScript in your Stencil theme are:
- Stencil automatically bundles and minifies JavaScript modules to optimize page performance
- To insert custom JavaScript on a particular page in your theme, edit the JavaScript module that corresponds to the page’s type
- To add files from third-party JavaScript modules to a theme, use npm where possible
- To add JavaScript modules not distributed via npm, you can create new subdirectories within assets/js/
- Theme-Specific JavaScript modules are provided in the theme’s assets/js/theme/ subdirectory
- To find the mapping between modules in assets/js/theme/ and page types, examine the PageClasses object contained in assets/js/app.js
- You can map JavaScript modules to custom page templates by editing the assets/js/app.js file