Server-side render JSS apps headlessly using a Sitecore Experience Edge endpoint
If you want to server-side render (SSR) a React, Angular, or Vue.js application built with JSS using a Sitecore Experience Edge (GraphQL) endpoint, Sitecore provides a sample application to simplify the setup process.
The sample application node-headless-ssr-experience-edge
for SSR using an Edge endpoint comes preconfigured with a Node.js and Express server and services for querying the endpoint.
Node.js and Express are third-party technologies you must configure according to your specific use-case scenarios for Headless mode.
The application renders your JSS application, querying layout, and dictionary data using the JSS GraphQL services without proxying the requests.
The sample for SSR using a Sitecore Experience Edge endpoint is experimental and not officially supported by Sitecore.
To use this technique, you must have:
-
A Sitecore instance with Headless Services installed.
-
A React, Vue.js, or Angular application built with JSS that:
-
Supports server-side rendering in integrated mode.
-
Supports the Experience Edge GraphQL schema.
-
Provides the
SITECORE_API_KEY
. TheGraphQLRequestClient
uses the API key to set thesc_apikey
header for authentication on both the Sitecore XM Edge schema and Sitecore Experience Edge.
-
To headlessly SSR a JSS React, Angular, or Vue.js app using the sample node-headless-ssr-experience-edge
:
-
In a terminal, run the following JSS command to create an application based on the
node-headless-ssr-experience-edge
sample, providing a name for the application, such asjss-edge
:RequestResponsenpx create-sitecore-jss node-headless-ssr-experience-edge --appName jss-edge
-
In another terminal, in the root directory of the JSS app you want to SSR, build your JSS app for production by running the following script:
RequestResponsejss build
-
Copy the resulting directory, for example,
dist
orbuild
, to the root directory of the SSR appjss-edge
, for example, in adist/<JSS app name>/
directory.Alternatively, in the JSS app, in the
scjssconfig.json
file, change theinstancePath
to the SSR sample root path. In the terminal, run thejss deploy files
command to deploy the build result to the SSR app.ImportantThe
scjssconfig.json
file might not exist if you have never connected your JSS app to Sitecore. -
In the SSR
jss-edge
app, inspect theconfig.js
file and modify it (or use environment variables) to specify values for the following properties:-
appName
- the value for your JSS application, as set in thepackage.json
file in theconfig.appName
configuration. -
bundlePath
- the path where you copied the directory resulting from the build processserver.bundle.js
. For example:'./dist/<JSS app name>/server.bundle'
. -
endpoint
- your Sitecore Experience Edge endpoint. -
apiKey
- set to the API key provisioned on Sitecore Experience Edge. -
defaultLanguage
- set to the default language configured in your JSS app used to determine language context if no language is specified in the request URL. -
port
- for starting the SSR sample.
-
-
In a terminal, at the root of the SSR
jss-edge
app, run the following script to test the SSR application:RequestResponsenpm run start
Environment variables
You can set environment variables to configure the SSR application instead of modifying the config.js
file for environments where this is more desirable, like containers.
You can set the environment variables in your hosting or container environment or the SSR application by using a library such as dotenv
in the config.js
file to read values from a .env
file.
To configure the environment of the JSS app for SSR with a Sitecore Experience Edge endpoint, use the following variables:
Parameter |
Description |
---|---|
|
The JSS app's name. Used when requesting layout data and dictionary using GraphQL queries, and the default value of |
| |
|
Path to the JSS app's |
|
Sitecore Experience Edge endpoint. |
|
The default language configured in the JSS app you want to SSR. Used to determine the language context if the language is not specified in the request URL. |
|
Optional. Port used when starting the sample. |