Creating custom components for Sitecore Content Hub can significantly enhance functionality and provide tailored user experiences. This guide will walk you through setting up a React project from scratch to build reusable external components, and includes a sample component to illustrate the process. We’ll cover two methods: using a provided GitHub repository or building everything from scratch on your local machine.
Method 1: Use the GitHub Repository
If you want to get started quickly, you can clone a pre-configured repository, which includes a sample "User Info" component for reference.
- Go to the GitHub Repository.
- Clone or download the repository.
- Follow the instructions in the
README.md
file to get set up. The "User Info" component serves as an example of how to structure and build components. - After exploring the setup, you can replicate the approach to create new components.
Method 2: Build from Scratch
For a deeper understanding of how everything fits together, follow these steps to set up a React project from scratch using Vite as the build tool.
Step 1: Set Up Your Project Folder
- Create a New Folder: In a location of your choice, create a new folder, e.g.,
SCHUB-REACTREPO
. - Open Terminal: Open your terminal and navigate to the new folder.
Step 2: Initialize the Project with Vite
- Run Vite Setup Command: In your terminal, run:
Press
Enter
and confirm to proceed.
- Choose Project Details:
- Project Name: Name your project (e.g.,
react-sch-project
). - Framework: Choose
React
. - Variant: Select
TypeScript
. - Check Project Structure: Once the project setup completes, open your folder in an editor like VS Code. You should see a basic project structure generated by Vite.
Step 3: Install Dependencies and Start Local Server
- Navigate to Project Directory:
- Install Required Packages:
- Run Development Server:
- Verify Local Server: Open your browser and navigate to
http://localhost:5137
. You should see the Vite + React project running.
Step 4: Add Basic SSL Support
For secure communication, you’ll need to configure SSL:
- Install SSL Plugin:
- Configure SSL in Vite:
- Open
vite.config.ts
. - Import
basicSsl
: - In
defineConfig
, passbasicSsl
to theplugins
array:
- Update Dev Script:
- Open
package.json
and add--host --https --cors
to thedev
script: - Verify HTTPS: Restart the server and check
https://localhost:5137
in your browser.
Step 5: Resolve Node Types Error
While configuring Vite, you might encounter an error related to Node types. To fix this:
- Install Node Types:
Step 6: Build and Add Your Sample Component
To get you started, let’s add a sample "User Info" component and build it as a minified JavaScript file.
- Create a New Component: Inside
src
, create a folder (e.g.,components
) and add a new component file,UserInfo.tsx
. - Build Component:
- This command compiles the component, generating a minified JavaScript file in the
dist
folder.- Access the Minified JavaScript:
- Open your browser and navigate to:
- You should see the minified JavaScript code for the
UserInfo
component.
- Start Dev Server Again:
Summary
Following these steps, you've created a project that allows for rapid development and testing of external components for Sitecore Content Hub. This guide provides flexibility—whether you prefer to clone an existing setup or configure a new one.
Thank you for reading, keep exploring, and continue building powerful, customized experiences for Sitecore Content Hub. Happy coding!😊👋