Engage.initServer.handleCookie(req, res)
The asynchronous initServer.handleCookie()
function creates the browser ID cookie on the server side and includes the cookie in the response header. To access this function, you must first call the initServer()
function. After you call initServer.handleCookie()
, you receive the cookie on the client side in the response header.
In production, we recommend that you call the initServer.handleCookie()
function in a middleware and use try-catch blocks to handle errors.
Here's an example of how to use the initServer.handleCookie()
function in a Next app to set cookies from the server.
To set cookies from the server, you must set the forceServerCookieMode
attribute to true
in the settings object. After you call the initServer.handleCookie()
function, the cookie is set in the response header.
import { initServer } from "@sitecore/engage";
const engageSettings = {
// ...
includeUTMParameters: true
};
const engageServer = initServer(engageSettings);
export async function getServerSideProps({ req, res }) {
await engageServer.handleCookie(req, res);
return {
props: {},
};
};
export default function Home() {
// ...
};