# Create a React Micro Frontend
# Prerequisites
- A working instance of Entando.
- Use the Entando CLI to verify all dependencies are installed with the command
ent check-env develop
.
# Create React App
We'll use Create React App (opens new window) to generate a simple app in seconds.
- Create 'my-widget' directory structure with the following:
npx create-react-app my-widget --use-npm
This is the expected output:
my-widget
├── README.md
├── node_modules
├── package.json
├── .gitignore
├── public
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── robots.txt
└── src
├── App.css
├── App.js
├── App.test.js
├── index.css
├── index.js
├── logo.svg
├── serviceWorker.js
└── setupTests.js
- Start the app
cd my-widget
npm start
# Wrap with Custom Element
- Add a new file
src/WidgetElement.js
with the following custom element to wrap the entire React app
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
class WidgetElement extends HTMLElement {
connectedCallback() {
this.mountPoint = document.createElement('div');
this.appendChild(this.mountPoint);
ReactDOM.render(<App />, this.mountPoint);
}
}
customElements.define('my-widget', WidgetElement);
export default WidgetElement;
The React root
node is programatically generated in the connectedCallback
method when the custom element is added to the DOM.
TIP
connectedCallback
is a lifecycle hook that runs each time the element is added to the DOM. (opens new window)
Custom Elements
- Must contain a hyphen
-
in the name. (opens new window) - Cannot be a single word.
- Should follow
kebab-case
for naming convention.
# Import Custom Element
- Open
src/index.js
. The initial file looks like:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
- Replace the entire file with these two lines
import './index.css';
import './WidgetElement';
# Test Micro Frontend
Open
public/index.html
Replace
<div id="root"></div>
with the custom element<my-widget />
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<my-widget />
...
</body>
Congratulations!
You’re now running React
in a containerized micro frontend.
# Build the Resource URL
Add your micro frontend to Entando by uploading the JavaScript and CSS files to the public
folder. This is the way Entando makes files available to the public.
# Add Widget
First, add a widget to get the resource URL for the public
folder. Then use the same widget to add the Micro Frontend to Entando.
Go to
Components > Micro frontends & Widgets
in the App BuilderClick
Add
in the lower right corner
- Enter the following:
Title: My Widget
→ enter both English and Italian languagesCode: my_widget
→ dashes are not allowedGroup: Free Access
Icon
: → upload an icon of your choice- In the center panel under
Custom UI
, enter the following:
<#assign wp=JspTaglibs[ "/aps-core"]>
<@wp.resourceURL />
- Click
Save
TIP
<#assign wp=JspTaglibs[ "/aps-core"]>
gives you access to the @wp
object where you can use environment variables like resourceURL
.
# Add Page
Next, add the widget to a page to view the Resource URL
.
If you're getting started with a new install of Entando, add the widget to the Home
page.
For Experienced Entando users: Add a new page → Add your widget to the page
Go to
Pages
→Management
Next to the
Home
folder, underActions
, →Edit
In the
Title
field, chooseMy Widget
In the Code field, choose
my_widget
Under Page groups, in the Owner group field, choose
Free Access
Scroll down to
Page Template
and selectSingle Frame Page
. Leave all other fields blank or in the default setting.Click
Save and Design
. You are now in the page Designer.In the Search field of the right sidebar, type
My Widget
. It will show as an option.Drag and drop
My Widget
into theSample Frame
in the body of the pageClick
Publish
In the top right corner, click
View Published Page
. This will take you to a blank home page with your widget.Copy the
Resource URL
at the top. For example, this is the URL in a quickstart environment set up via the Getting Started guide:
/entando-de-app/cmsresources/
# Build It
With the Resource URL where the new React App will be hosted, you are ready to build.
Create an
.env.production
file in the root ofmy-widget
projectAdd the
PUBLIC_URL
into the file.
PUBLIC_URL=/entando-de-app/cmsresources/my-widget
Notes
/entando-de-app/cmsresources/
is the Resource URL for your Entando application/my-widget
is the public folder that's created to host the files.
# npm build
Open a command line and navigate to the project root of your
my-widget
Run the command:
npm run build
- Rename the following files generated in the
build
directory
Example of Generated Build File | Rename to | Function |
---|---|---|
build/static/js/2.f14073bd.chunk.js | static/js/vendor.js | Third-party libraries |
build/static/js/runtime-main.8a835b7b.js | static/js/runtime.js | Bootstrapping logic |
build/static/js/main.4a514a6d.chunk.js | static/js/main.js | App |
build/static/css/main.5f361e03.chunk.css | static/css/main.css | Stylesheet |
Generated Build Files
The JavaScript and CSS files are renamed so App Builder can deploy the new versions of the micro frontend without having to update the Custom UI
field of the widget.
If you want to use the original file names with the content hashes to avoid potential caching issues in your browser (opens new window), update the Custom UI
field of your widget when deploying new versions of your micro frontend. The Custom UI
settings will be covered in the next section.
Additional Deployment Options
- Install the micro frontend from a bundle in the
Entando Component Repository
. - Add the micro frontend to
Entando App Builder
. - Load the micro frontend from an API.
# Host Micro Frontend
Now you are ready to host the micro frontend in Entando.
# Create Public Folder
Navigate to
Entando App Builder
in your browserClick
Administration
at the lower left hand side of the screenClick the
File browser
tabChoose the
public
folderClick
Create folder
Enter
my-widget
Click
Save
Click
my-widget
Create the same folder structure as your generated build directory
my-widget/static/css
my-widget/static/js
my-widget/static/media
- Upload the renamed files in the corresponding
js
andcss
folders
my-widget/static/css/main.css
my-widget/static/js/main.js
my-widget/static/js/runtime.js
my-widget/static/js/vendor.js
Note: You can drag and drop the files in your browser
- Upload the
React
logo
my-widget/static/media/logo.5d5d9eef.svg
→ You don't need to rename this file
# Update Custom UI Field
Go to
Components
→Micro frontends & Widgets
Under the
My Widgets
category → next toMy Widget
→ underAction
→ selectEdit
Update
Custom UI
field:
<#assign wp=JspTaglibs[ "/aps-core"]>
<link rel="stylesheet" type="text/css" href="<@wp.resourceURL />my-widget/static/css/main.css">
<script async src="<@wp.resourceURL />my-widget/static/js/runtime.js"></script>
<script async src="<@wp.resourceURL />my-widget/static/js/vendor.js"></script>
<script async src="<@wp.resourceURL />my-widget/static/js/main.js"></script>
<my-widget />
- Click
Save
# View the Widget
View the React micro frontend in action on your page.
In the
Entando App Builder
go back toPages
→Management
Next to the page you created, under
Actions
→Design
. This takes you back to the page Designer.Click on
View Published Page
on the top right side
Congratulations!
You now have a React micro frontend running in Entando.
← Introduction Angular →