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.
               D:\XMCloud\XMCloudDemo

  •  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


  1.   Navigate to the .env file within your solution under src\sxastarter, and copy it.   Name it .env.local. 
  2. 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. 

  1.   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



        Click confirm
    2.Capture the Environment id through the XM cloud portal which will         help in the token generation as mentioned below



3. using the environment id execute the below mentioned command to generate the edge token. Replace the environment id with the value mentioned above.

.\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.

Remove one of the forward  slash from the URL and it will popup the correct URL 


if you see the image at the bottom you will get this key "X-GQL-Token".This Edge token which will be used in the project to connect with XM cloud project.

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

Popular posts from this blog

Solrcloud With Zookeeper -Single server setup

Render Sitecore Experience Forms Using Sitecore XP 10.4 with a Headless Approach (Next.js + JSS SDK)

Next.js with XM Cloud EDGE and GraphQL