writeCroctCookies

Learn how to persist the visitor cookies on the response.

This function writes the Croct visitor cookies, such as the client ID and user token, onto the response as Set-Cookie headers.

Call it in your server entry after committing the Hydrogen session, so its Set-Cookie header does not overwrite Croct’s cookies. It works the same in both the React Router 7 and Remix setups.

Signature

This function has the following signature:

function writeCroctCookies(response: Response, context: CroctContext): void;

Example

Write the cookies after committing the session:

server.ts
import {writeCroctCookies} from '@croct/plug-hydrogen/server';
const response = await handleRequest(request);
if (context.session.isPending) {  response.headers.set('Set-Cookie', await context.session.commit());}
writeCroctCookies(response, context);
return response;

Parameters

The following list describes the supported parameters:

response
Response

The outgoing response to attach the cookies to.

context
CroctContext

The Hydrogen load context for the current request.

It carries the visitor context resolved by the route middleware on React Router 7, or by the context helper on Remix.