Setting Up Sitecore Headless with Next.js in connected Mode
In Disconnected Mode, the Next.js app uses local content stored in static files meaning that the data flow is not dependent on the Sitecore CMS.
In Connected
Mode, the Next.js frontend communicates directly with a live Sitecore instance.
This setup enables
- -
Real
time content fetching from Sitecore CMS using the Layout Service and
- -
Support
for using Experience Editor for visual page editing
Steps to set Up Sitecore Headless with Next.js in Connected Mode
1. Create an SSL Certificate
First, generate
a new SSL certificate to allow HTTPS connections locally, below PowerShell
script helps to generates a new SSL certificate and it is added to trusted
certificates. If the certificate doesn’t automatically move to Trusted Root
Certification Authorities, manually move it via Manage User Certificates.
New-SelfSignedCertificate
-CertStoreLocation cert:\LocalMachine\My -DnsName
"mynextjsapp.dev.local"
2. Add new Binding in IIS
Next, bind the
SSL certification to Sitecore CM Instance. To assign the newly created
certification to the Sitecore CM instance
- ·
Open
IIS Manager
- ·
Select
the Sitecore CM Site
- ·
Go
to binding section
- ·
Add
a new binding with https type
- ·
Provide
hostname as mynextjsapp.dev.local
- · Select the newly created certificate
from SSL certification drop down list
3. Update the Hosts File
To
map the custom domain to local machine, Open the host file in admin mode from
the location
C:\Windows\System32\drivers\etc\hosts
Add
the new hostname entry same as we added in binding in previous step mynextjsapp.dev.local
as depicted below
4. Create a Sitecore API key
- -
Open
the Sitecore CMS, access Content Editor
- -
Navigate
to the location Sitecore/system/Settings/Services/API Keys
- -
Create
a new API key e.g. NextJSAPI,
- -
Set
the CORS Origins and Allowed Controller Fields to *
- -
Save
and publish the item
5. JSS Setup
- ·
Open
the NextJS app folder in VS code
- ·
Open
the new terminal and run below command
jss
setup
After
successful setup, scjssconfig.json file will be updated
(e,g.
location C:\Learning\NextJS\nextjs\scjssconfig.json)
Now deploy app
configuration to Sitecore with below command
Jss deploy
config
This copies app's configuration files to Sitecore location under /App_Config/Include/zzz/
6. Deploy Items and Content
Before
deploying items, get the certificate thumbprint using below command
jss deploy
items --includeContent --includeDictionary --acceptCertificate test
Copy the thumbprint
from the error message, then run:
jss deploy
items --includeContent --includeDictionary --acceptCertificate THUMBPRINT_FROM_THE_ERROR
e.g.
jss deploy items --includeContent --includeDictionary --acceptCertificate F9:63:19:34:F0:27:2D:B1:6C:9D:86:FC:A7:6F:AC:SE:F7:B7:71:CF
This imports Components,
Templates, Renderings and Pages to Sitecore instance under
/sitecore/content/sitecore-jss-app
7. Run the NextJs Application in connected mode
In PowerShell
(admin mode), navigate to NextJs app root and run the below commands.
$env:NODE_TLS_REJECT_UNAUTHORIZED=0
jss start:connected
Visit http://localhost:3000 in the browser, application
will start running in connected mode.
Open the page in the Sitecore and Sitecore experience editor.
Enjoy the
connected experience.
Happy coding!
Comments
Post a Comment