Salesforce Commerce Cloud - A headless CMS with React
Iâve recently been involved in building some new things on Salesforce Commerce Cloud (SFCC), a powerful enterprise e-commerce solution (think to Shopify, but Salesforce). Our goal was to build a React front-end that makes use of as much of the Commerce Cloud platform and features as possible.
We looked to use it as a âheadlessâ CMS, where SFCC is our content API, with our React app consuming them and, in turn, rendering the right experience. This has the added benefit of reducing the ramp-up time for our engineers, as our front-end team and SFCC team can work through API contracts and donât need to worry about each otherâs internals and reduces the amount of SFCC knowledge our front-end team needs to do their work.
Sadly this headless approach isnât something Commerce Cloud supports out-of-the-box. It seems to go against a lot of their established patterns, but it is possible! There is a somewhat related PWA project called pwa-kit that uses SFCC, however it doesnât have support for SFCCâs WYSIWYG drag-and-drop page builder called âPage Designerâ, which is a critical requirement for us, so we went our own way.
A note: Given the complex ecosystem and development that comes with SFCC sites, this post is not going to be an in-depth tutorial for how to replicate this set-up. It is more of an overview that hopes to provide you enough information to implement yourself.
Here is a sequence diagram showing, on a high level, our plan:
Set-up
We started with getting our React app deployed onto the SFCC. We can do this by running a build
of our React application and once we have a dist
directory, we can copy and upload its contents to
our SFCC cartridge under the static
directory using VS Code and
the Prophet Debugger plugin.
Our React app bundle ends up in a directory with a path looking something like the following:
[vs-code-project]/cartridges/custom_storefront_ui/cartridge/static/default/ui-app
The ui-app
name here isnât special, a personal preference to keep the static/default
directory organised. From this directory the files are available on the SFCC CDN and subject to
their caching rules.
We then update our page template
ISML
1
file to use the scripts we uploaded to our ui-app
directory. In the same cartridge as above, we
add a series of script
tags pointing to the main entry-points for our React app:
One thing to note here is the lack of chunkhash
in the file names. Normally these files will have
a hash name in them (e.g. runtime.e80b121e.js
), however weâve disabled this in
our NX (the react build) configuration so whenever we run a build, the four files
listed above do not include a hash. We do this to avoid needing to update this main.isml
file
after each React build with what would be the new hashed filenames.
Viewing this page you should now see the React app in the browser. At the moment we havenât used any SFCC content, our next goal is getting some content out of SFCC into our app.
Supporting Page Designer
As I mentioned above, âPage Designerâ is the drag-and-drop page editor that comes with Commerce Cloud, it allows business users to easily configure site pages, localise content and rearrange as needed. To use this feature with our headless approach we need a way to expose an API to provide the page structure from Page Designer to our React application. The way we do this is through page serialization.
Serialize those pages (as JSON)
We can convert a page into a JSON tree by using the
PageMgr.serializePage
function in a SFCC controller. With this you can serialize the page designer structure to a JSON
object and return it as part of an API response, which will look something like the following:
The above example shows a page template with a single region (id: body
) and within that we have a
component called columns
that itself also implements two more regions (left
and right
) with
a component in each. The type_id
âs for the components comes from the
component definition JSON files, for custom components these are the JSON file names found in
this directory (these names will be important in a moment):
[vs-code-project]/cartridges/custom_storefront_ui/cartridge/experience/components/
For SFCC provided components, these use a reverse domain identifier, for example the rich text
boxâs type_id
will be: commerce_assets.editorialRichText
.
From JSON to React components
Now we have a JSON representation of our page that we can expose via an API in SFCC as a controller,
and our React app can make a fetch
request for it. Now the big question is how can we turn that
JSON object into React components?
Thanks to some previous work I have done with other headless CMS providers, (Contentful and Prismic), there was a mapping pattern I could already make use of, which lets us go from JSON structure to React components.
This works by using a dictionary of possible components IDs that then a corresponding React component. Like so:
Here we have a mapping object that contains keys that match each componentâs type_id
from our JSON
page structure, with the value of that key is a React component. We then iterate over our JSON
structure and do the following to render a component based on the type_id
.
We do this in what we call this our PageBuilder
component. It accepts a JSON structure and then
iterates over the regions for that component and maps each type_id
to our components as
defined in the mapping
.
To keep the above example short-and-sweet this only iterates over the first level of regions. To
support nested-regions (components that themselves have regions) you will need to do something
recursive, or have the individual components include the <PageBuilder />
component to render their
included regions.
Future investigations
This approach is great for building rich and engaging user experiences in a common technology from the industry it does come with its drawbacks.
A lot of the powerful front-end features of SFCC are exposed in their templating language ISML, which this approach does not make use of (weâre using serialized json). An example is a lack of support for âcontent slotsâ which offers the ability to add targeted and/or scheduled content to a page.
However, that doesnât mean it is not possible at all, and as these projects progress I will continue to investigate how to add the âcontent slotsâ feature, and others, to make sure we can get the most out of our SFCC project using React.
Iâll be posting follow-ups and more, as part of my #sfcc-with-react series.
Footnotes
-
ISML is a custom templating syntax SFCC uses. Read more in the docs â©