11 Days of Salesforce Storefront Reference Architecture (SFRA) — Day 4: Creating a Basic Cartridge
Tags: sfcc, sfra, salesforce
Published: 2020-05-18 19:55:03

---
title: 11 Days of Salesforce Storefront Reference Architecture (SFRA) — Day 4: Creating a Basic Cartridge
published: true
date: 2020-05-07 16:04:26 UTC
tags: sfcc,sfra,salesforce
series: 11 Days of Salesforce Storefront Reference Architecture (SFRA)
canonical_url: https://medium.com/perimeterx/11-days-of-salesforce-storefront-reference-architecture-sfra-day-4-creating-a-basic-cartridge-2d8e9cf7cf86
---

### 11 Days of Salesforce Storefront Reference Architecture (SFRA) — Day 4: Creating a Basic Cartridge

![](https://cdn-images-1.medium.com/max/1024/1*TybDBBjdzBWEE2w47DHVpA.jpeg)<figcaption>Photo by <a href="https://unsplash.com/@www_erzetich_com?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Blaz Erzetic</a> on <a href="https://unsplash.com/s/photos/component?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption>

With all our preparations behind us, it’s time to get down to business and create our very first cartridge. Cartridges are a core foundation of the SFCC ecosystem. In fact, most of the business logic of SFCC is organized in cartridges. Really, in its core, a cartridge is really just a way to package and deploy code (and data) to an SFCC instance. For example, a cartridge can contain new controllers or scripts that add functionality to a site. For this post, we will create a cartridge that will add a new controller and a new template to the demo site.

### Creating a New Cartridge

The easiest way to create a new cartridge is to use a boilerplate. SFCC provides us with a boilerplate as part of the SFRA command-line tools (the sgmf-scripts package we installed on [day 2](https://dev.to/pxjohnny/11-days-of-salesforce-storefront-reference-architecture-sfra-day-2-setting-up-your-32g8-temp-slug-8741800)).

1. Create a new folder, cartridgesHub, and cd to it.
2. Run the `sgmf-scripts` command to create a new cartridge called `magicCartridge_: sgmf-scripts —-createCartridge magicCartridge`
3. Browse to the newly created cartridge (`cd cartridges/magicCartridge/cartridge`). If you list the content of the folder, you’ll see that the SFRA CLI tool created a set of folders and files for us:

![](https://cdn-images-1.medium.com/max/1024/1*UeozV2SSjoTW6pTyjOIgKA.png)<figcaption>The content of a newly created cartridge, created by the SFRA CLI tool.</figcaption>

Each folder in the boilerplate serves a different integration point for the cartridge:

- client — used for client-side resources, such as CSS or JS files
- controllers — used for adding new server-side controllers.
- models — used for adding new data models.
- templates — used for adding new ISML templates.

### Adding a New Controller

If you are coming from SGJC then you are no stranger to the concept of controllers. To make sure everyone is on the same page, controllers essentially (as the name suggests) control the flow of the storefront. When you browse to a storefront page, a controller picks up the request and, based on the action, decides which template to render. For this example, we will create a controller called Magic which will have a single action — Show, thus responding to a request coming to the /magic-show route.

1. cd to the controllers folder of the newly created cartridge.
2. Create a new file called Magic.js with the following content:

{% gist https://gist.github.com/pxjohnny/b882eec66315dcb8637b31bee9748a2a %}

So what are we seeing here?

**Line 1:** Imports the server module, which in our case, is mostly used for routing.

**Lines 3–6:** Adds a “listener” for the `Show` route (i.e `/magic-show`). If the route is hit — the `magic` template will be rendered to the page.

**Line 7:** Calls the `next` method, that will pass the request to the next middleware in the chain. When the request reaches a middleware that does not call `next`, the request will be considered as handled and the response object will return to the caller.

> 🐘To learn more about middleware, consult ExpressJS’s [Using Middleware](https://expressjs.com/en/guide/using-middleware.html) guide.

**Line 10:** Exports the module using the server’s class exports method.

### Uploading the Cartridge

Now that we have a new controller in our cartridge, it’s time to test it out! To upload our new cartridge:

1. cd back to the cartridges hub root folder (`/cartridgesHub`).
2. Fill in the details in `dw.json` (use the same credentials as the `dw.json` file in `storefront-reference-architecture`).
3. Upload the cartridge using the SFRA CLI tools: `sgmf-scripts — uploadCartridge magicCartridge`.

If all went well you should see the success message printed out for you:

![](https://cdn-images-1.medium.com/max/1008/1*UMnWs-prgqAceQ9jBm7SYA.png)<figcaption>Yay success 🎉</figcaption>

> 🐘 You can also use NPM to upload the cartridge by running `npm run uploadCartridge` instead of using the CLI tools.

### Registering the Cartridge

Before we can use our new cartridge, we have to register it.   
Registering the cartridge is basically the equivalent of “deploying” it, meaning the site you registered the cartridge on will be able to use its resources.

1. Open your Business Manager and navigate to **Administration** > **Sites** > **Manage Sites** >  **RefArch**.
2. Click on the **Settings**  tab.
3. You should now see all the cartridges currently registered to the site in the **Cartridges** line. To register our cartridge, simply add its name to the list of Cartridges and click apply:

![](https://cdn-images-1.medium.com/max/1024/1*luBi4Xu2OMQ8xILB5_VOJQ.png)<figcaption>Cartridges registered to the RefArch site</figcaption>

> 🐘 The order of the cartridges in the Cartridges flow is **crucial**. The more to the left a cartridge is positioned, the more powerful it is. What does powerful mean?   
> In the case where there are two controllers with the same name in more than one cartridge, the controller in the left-most cartridge will override the controller in the other cartridges and will be used by the platform.

With the cartridge registered, let’s test our new controller:

Navigate to the site’s URL by using the instance URL: [https://<instance name>.demandware.net/on/demandware.store/Sites-RefArch-Site/default/Magic-Show](https://perimeterx01-tech-prtnr-na03-dw.demandware.net/on/demandware.store/Sites-RefArch-Site/default/Magic-Show)

If the controller loaded up correctly, you should get the following page:

![](https://cdn-images-1.medium.com/max/1024/1*A_6ljX2W04TpjucN7FQCEw.png)

### Adding the Magic Template

When testing our new controller we reach the above error page. The reason for that is simple; in our controller we specified a template to render called `magic`, but we never actually added it to the cartridge.

1. cd to the `templates/default` folder of our magicCartridge cartridge.
2. Add a new file called `magic.isml` with the following content:

{% gist https://gist.github.com/pxjohnny/f8f978888683ef52cb5b51194c30ace5 %}

3. cd back to the cartridges hub root folder (`/cartridgesHub`).

4. Upload the cartridge using the SFRA CLI tools: `sgmf-scripts — uploadCartridge magicCartridge`.

Refresh the page again and you should be greeted with a magical surprise :)

That will do it for our basic cartridge, we will keep the magic (pun intended) alive tomorrow when we use custom site preferences with our cartridge.

As always, looking forward to your comments either here or on [Twitter](https://twitter.com/fullstackj) 😎

* * *