Adding JustRelate Web Builder to Create-React-App

Adding JustRelate Web Builder to Create-React-App

Facebook’s create-react-app (CRA) offers an easy and well-documented way of bootstrapping a React app. In this tutorial, we will show you how to add the Web Builder to such an app, allowing you to freely combine Web-Builder-managed content with whatever content you wish to deliver separately.

Before you begin, you’ll want to verify the current requirements for create-react-app. This tutorial was last verified with version 4.0.2. For the instructions in this tutorial to work as intended you will need a Web Builder instance, too.

Create the React app

Let’s start! In a terminal, change to where your projects are, then create the React app and initialize the repository. Here we are naming our application “react-scrivito”, but you could use what ever name you wish:

Add the basic configuration, app code and markup

Run:

Now create the Web Builder’s configuration file, “src/config/scrivito.js” and set its contents to:

Replace YOUR-CMS-INSTANCE-ID with exactly that. Please find the instance ID in your JustRelate account at console.justrelate.com.

With a single-site setup (as opposed to a multi-site setup), if your Web-Builder-based content is not served from “/”, add the corresponding routingBasePath to your call to Scrivito.configure.

USING ENVIRONMENT VARIABLES WITH CREATE-REACT-APP

Using environment variables is always a safer option compared to hard-coding sensitive data. When using the Create React App Webpack settings, one must prefix any variable with REACT_APP_ for it to be picked up. For this project, the call to Scrivito.configure would look like this:

Next, add the variable to the “.env” file in your project and on Netlify in your “Build & deploy” settings. Finally, don’t forget to add “.env” to your “.gitignore” file.

Then, add imports for Scrivito-related files to “src/index.js” like so:

In addition to the configuration we provided above, the (not yet existing) CMS object and widget class definitions are imported. We’ll provide a couple of basic CMS classes later on.

What you render here, in this case via the App component, depends on where you started off. For a fresh React app without any relevant content (like ours), it makes sense to render just the Web-Builder-based content, while a fully fledged React app probably requires some kind of routing. There’s a dedicated tutorial on implementing routing using React Router.

To have the app render the current (Web-Builder-based) page (in addition to the default header), change “src/App.js” to the following:

Provide some page and widget classes and components

To be able to create pages and widgets, we require object and widget classes. Assuming that you have a fresh Web Builder, all we need to get started are a Homepage and an Image object class as well as two widget classes, SectionWidget and TextWidget. These classes will suffice to display at least a small portion of the homepage content.

Our “src/index.js” file above imports object classes from “src/Objs” and widget classes from “src/Widgets”. After creating these directories, we will add the following “index.js” file to each of them to have the contents of the class subdirectories imported automatically.

Alternatively, you can download the files as an archive and uncompress it to the “src” directory if you don’t want to create them all manually. The download includes a Page object class and an ImageWidget class in addition to the examples below, as these are commonly used.

Homepage

Image

SectionWidget

TextWidget

Fire up your app!

In the terminal, from the project directory of your app, run:

This opens http://localhost:3000 in your browser. If you specified the ID of a new Web Builder instance in “src/config/scrivito.js”, or haven’t used this URL before, you will need to authorize it in the JustRelate Console on the “Trusted origins” tab of your account.

If your CMS content includes a page object for the “/” URL and at least a text widget (like the example app does), you should see a page with some text. This is the published content, without the Web Builder UI:

Blind text on the homepage (UI not loaded)

To have the Web Builder UI displayed, add ”/scrivito” to the URL: http://localhost:3000/scrivito:

Homepage (UI loaded, published content)

Remember that we defined only two widget classes (section and text), so only content from widgets of these types will be displayed. Now, after clicking “Edit” on the top bar, you will see errors from all the other (not defined) widgets on the page:

Homepage (editing mode)

The missing widget classes causing the “application error” messages are included in the Portal App. You could install it or transfer and adapt the classes as needed. As this tutorial is based on the idea of starting from scratch, you might want to delete the sample content from your Web Builder instance.

Final words

You now have the Web Builder integrated with Facebook’s create-react-app and can start developing your app further. Add page and widget classes for handling Web-Builder-based content, include some styling, and implement routing if you plan to deliver mixed-source content. If you are new to the Web Builder, take a look at the other guides and tutorials we have available.

This tutorial was written using React 16.8.0, and the Scrivito SDK 1.20.0. The directions should work without change with all minor versions going forward, but let us know if you encounter any issue.