Lab - Create a Mini App
Introduction
This lab will walk you through setting up a local environment running a lean, single-page React app to integrate with a Stencil storefront and the Buyer Portal app. You’ll use this app to enhance your storefront using data provided by the Buyer Portal.
In this lab, you will:
- Copy a mini app starting repository
- Run a local dev server
- Use storefront scripts to load the app
Prerequisites
- A BigCommerce sandbox store or trial store
- B2B Edition enabled in your store
- A Stencil storefront channel, enabled for B2B
- A command-line interface on your local machine
- Node.js 20 or later
- Git CLI
For following along in the code in future lessons, it’s also helpful to make sure you have a code editor, like VS Code, to open your project once installed.
Node Version Manager (nvm) is the recommended way to install Node.js and allows you to install and use different versions.
With nvm installed, you can install a specific version:
nvm install 20
… and switch to the Node.js runtime of any version you have installed:
nvm use 20
Whether you use nvm or install Node.js via another method, make sure you are running a compatible version:
node -v
The Single-Page App
The application you’ll be setting up is a small, single-page React app. There are multiple frameworks available for building single-page apps. For our use case, we only need to be able to inject React components into an existing page, so we’re using one of the simplest options. The app is bootstrapped using Vite, a popular build tool for modern web projects.
The setup for a similar app of your own is simple, as described in React documentation. Our mini-app was created with npm create vite@latest -- --template react-ts.
The app’s starting state has been stripped back further from the Vite starter template, including removing the default HTML page and App component, as well as tweaking Vite config to make main.tsx an explicit entry point and allow cross-origin requests to the dev server.
See the initial modifications here.
Step 1: Copy the Project
- Install
degit.
degit is a tool for making copies of Git repositories.
- Copy the starter project, replacing the path to the working directory with your own path.
- Navigate to the new project working directory.
- Initialize a Git repository for your project.
From here on, we’ll leave managing your project’s source control to you.
Step 2: Run the Dev Server
- Install the project dependencies.
- Start the dev server.
Take note of the localhost port displayed in your terminal output. (e.g., http://localhost:5173) Port 5173 is typical, but your port might be different if this port is already taken.
Step 3: Update Storefront Settings
In order to load and initialize the app in your storefront, we’ll take the same strategy as the Buyer Portal itself, using Script Manager.
- Log into your BigCommerce control panel.
- Navigate to Script Manager.
- Create a script with the following settings.
- Enter the following script content, replacing the port number with the one displayed in your terminal output, if necessary.
- Save the new script.
- Navigate to your Stencil storefront. If your app is loading correctly, you should see the message “B2B custom app initialized” in the console of your browser developer tools.
![]()
Troubleshooting
If you don’t see the right message in the console:
- Make sure you copied the correct project boilerplate.
src/main.tsxshould containconsole.log('B2B custom app initialized'); - Make sure the dev server process is running in your terminal.
- Make sure the port number indicated in the terminal output matches your script in Script Manager.
- Try browsing directly to the script URL (e.g.,
http://localhost:5173/src/main.tsx) in your browser to verify it is served correctly. - Check for any errors in the browser developer tools console.
Step 4: Implement a useB2B Hook
Our mini-app will need to access the window.b2b object but can only do so after the Buyer Portal has initialized. The cleanest way to handle this is to centralize the logic into a custom React hook.
- Open
src/hooks/useB2B.tsand implement the logic in the existinguseB2Bhook.
useB2B.ts contains some simple type definitions to let TypeScript know what window.b2b looks like. The hook we’ve implemented simply uses a check on a short interval to set a piece of React state as soon as window.b2b.utils is available.
With this hook in place, any component in the app simply needs to invoke it to access b2b and all its methods. By virtue of standard React patterns, any such components will re-render as necessary as soon as the state value in the hook is updated. These componens won’t need to concern themselves with the details of waiting for the Buyer Portal to initialize.
- Open
src/main.tsxand remove theconsole.logstatement.
- Import the hook and initialize a temporary React component in order to test the hook.
The above code creates a root element and appends it to the bottom of the page, then uses a standard pattern to render React into that element.
With this in place, the messsage logged to your browser console should now be “B2B utils are available”, indicating that the Buyer Portal is initialized and the app is ready to use.