How to connect Nextjs app with XM Cloud using Graphql

We have a Nextjs app with sxastarter template.

Please find the below GitHub repo for the solution.

https://github.com/sitecorelabs/xmcloud-foundation-head-staging


Clone the repo into your local disk. 



Note: This repo is a docker-based solution but we don't need to install any docker-related packages.

Open the folder in visual studio code for admin mode. 

Navigate to the Nextjs app source using the below command:
    
PS C:\xm-cloud> cd src/sxastarter



Open the .env.local file and update the following fields.

.env.local is a copy of the .env file and overrides the updated settings and it's ignored by .gitignore for local.


Need to Update :

SITECORE_API_KEY= D732E23E-A7A6-4E4E-87B8-A68B88D51612

Get the key value from your Sitecore path. which is required to authenticate and identify the connections.

 /sitecore/system/Settings/Services/API Keys - here find the item Id and remove the curly braces



JSS_APP_NAME="NextApp"

If "JSS_APP_NAME" is not there in .env.local file then add the key with mentioned and update the Application name of your Sitecore. Like sitename/appname



GRAPH_QL_ENDPOINT={sitecorecmdomain}/sitecore/api/graph/edge

FETCH_WITH=GraphQL (default its Rest, change it to GraphQL)



Run the following command npm run next:dev

When you are running the command it will take from the package.json script section to all the npm-related installations and build the app and run.


Default it will run on http://localhost:3000/

When you open the URL it resolves the content from the home page. 

But you might have gotten an error like the below screenshot


It's because by default Sitecore CDP integration is enabled so expect appropriate fields to configure.

For now, we can disable the CDP integration with the below steps.

Navigate to file: CdpPageView.tsx, here you could find the disable option and set it to true

const disabled = () => {
    return true;
  };




Comments

Popular posts from this blog

How to Enable Voice Search in Sitecore Content Editor

How to Create a Component in Next.js for XM Cloud

How to Create a Custom Component in XM Cloud.