Integrate Next Js project Local Setup to XM cloud
Introduction:The objective of this document is to Integrate the Next js project created in the git hub in the previous blog to the XM cloud from your local system.
Prerequisites
- Node version 18 or higher.
- NET Core 6.0 SDK (check your installed version with the dotnet --version command).
- Install git on your local system
- Visual studio code
- Copy the repository path from git hub created in the previous blog which will be used later while cloning in local system.
Lets Get started with Next Js Project set up in the local environment.
- Clone the repository in your local system. Open the Git CMD and go the project folder where you want to clone and execute the below command.
Git clone <Repository Path>
- Go the Project location and open it in visual studio code. In my Case the project is cloned in the mentioned below path.
- In this Next Js project code base Sitecore CLI installation is already installed by default, just need to restore it using below command. Please refer to Sitecore documentation to do Sitecore cli setup in any project folder
Dotnet tool restore
Connect the Next Js project in your local system to the XM Cloud Instance
you need to use a GraphQL endpoint to query the content coming from XM Cloud. To connect the endpoint, we have two approach.
Approach 1: Connect using the delivery endpoint of Edge or to the preview endpoint of your CM instance
- Navigate to the .env file within your solution under src\sxastarter, and copy it. Name it .env.local.
- Populate the mentioned below keys values from your XM Cloud cm instance .Below are the keys need to be populated fin .env.local
- SITECORE_EDGE_CONTEXT_ID
- SITECORE_SITE_NAME
you will get these values from the XM cloud CM Instance ad mentioned in the screen shot below
Note -There are two context id in the above image .Context Id(Preview ) works without Publishing the content like pointing to master DB in XP. If you use Context ID(Live) then you need to Publish the content like web database in XP.This is just an analogy to understand both the different mode.
if you use live id without Publishing you will get the above error.
Approach 2: Connect the XM cloud using generating the Edge access token.
- Go to the folder where you cloned the project ,in my case it's "D:\XMCloud\XMCloudDemo" and execute the below mentioned command.
dotnet sitecore cloud login
Execution of above command will popup the below link in the browser
2.Capture the Environment id through the XM cloud portal which will help in the token generation as mentioned below
.\New-EdgeToken.ps1 -EnvironmentId {environment-id}
Execution of above command will lead the below popup .If you watch closely there is a double forward // in the URL ,remove one of it.
Execution of above command will lead the below popup .If you watch closely there is a double forward // in the URL ,remove one of it.
4. Navigate to the .env file within your solution under src\sxastarter, and copy it.
Name it .env.local.
- SITECORE_API_KEY: The token we generated in the previous step.
- GRAPH_QL_ENDPOINT: https://edge.sitecorecloud.io/api/graphql/v1
- JSS_APP_NAME: The site name we set in XM Cloud .For my demo pupose the site name is "Xmclouddemo"
5 . Go to the below folder and execute the command the below command
D:\XMCloud\XMCloudDemo\src\sxastarter
- npm install
- npm run start:connected
6. Browse http://localhost:3000/
This will Load your home page.

Comments
Post a Comment