Rolling cookies allows you to prolong the expiration of a cookie by updating the expiration date of every cookie.
The rollingCookie function is prepared to be used in entry.server exported function to update the expiration date of a cookie if no loader set it.
rollingCookie
entry.server
For document request you can use it on the handleRequest function:
handleRequest
import { rollingCookie } from "remix-utils/rolling-cookie";import { sessionCookie } from "~/session.server";export default function handleRequest( request: Request, responseStatusCode: number, responseHeaders: Headers, remixContext: EntryContext) { await rollingCookie(sessionCookie, request, responseHeaders); return isbot(request.headers.get("user-agent")) ? handleBotRequest( request, responseStatusCode, responseHeaders, remixContext ) : handleBrowserRequest( request, responseStatusCode, responseHeaders, remixContext );} Copy
import { rollingCookie } from "remix-utils/rolling-cookie";import { sessionCookie } from "~/session.server";export default function handleRequest( request: Request, responseStatusCode: number, responseHeaders: Headers, remixContext: EntryContext) { await rollingCookie(sessionCookie, request, responseHeaders); return isbot(request.headers.get("user-agent")) ? handleBotRequest( request, responseStatusCode, responseHeaders, remixContext ) : handleBrowserRequest( request, responseStatusCode, responseHeaders, remixContext );}
And for data request you can do it on the handleDataRequest function:
handleDataRequest
import { rollingCookie } from "remix-utils/rolling-cookie";export let handleDataRequest: HandleDataRequestFunction = async ( response: Response, { request }) => { let cookieValue = await sessionCookie.parse( responseHeaders.get("set-cookie") ); if (!cookieValue) { cookieValue = await sessionCookie.parse(request.headers.get("cookie")); responseHeaders.append( "Set-Cookie", await sessionCookie.serialize(cookieValue) ); } return response;}; Copy
import { rollingCookie } from "remix-utils/rolling-cookie";export let handleDataRequest: HandleDataRequestFunction = async ( response: Response, { request }) => { let cookieValue = await sessionCookie.parse( responseHeaders.get("set-cookie") ); if (!cookieValue) { cookieValue = await sessionCookie.parse(request.headers.get("cookie")); responseHeaders.append( "Set-Cookie", await sessionCookie.serialize(cookieValue) ); } return response;};
The cookie to keep alive
The request object
The response headers object
A promise that resolves when the cookie has been set in the response
https://sergiodxa.com/articles/add-rolling-sessions-to-remix
Rolling cookies allows you to prolong the expiration of a cookie by updating the expiration date of every cookie.
The
rollingCookie
function is prepared to be used inentry.server
exported function to update the expiration date of a cookie if no loader set it.For document request you can use it on the
handleRequest
function:And for data request you can do it on the
handleDataRequest
function: