Remix Utils - v9.3.1
    Preparing search index...

    Module Middleware/Rolling Cookie

    Note

    Install using bunx shadcn@latest add @remix-utils/middleware-rolling-cookie.

    Note

    This depends on zod, and React Router.

    The rolling cookie middleware allows you to prolong the expiration of a cookie by updating the expiration date on every request.

    First, create a rolling cookie middleware instance:

    import { createRollingCookieMiddleware } from "remix-utils/middleware/rolling-cookie";

    // This must be a Cookie or TypedCookie instance
    import { cookie } from "~/cookies";

    export const [rollingCookieMiddleware] = createRollingCookieMiddleware({
    cookie,
    });

    Then, add the rollingCookieMiddleware to the middleware array in your app/root.tsx file.

    import { rollingCookieMiddleware } from "~/middleware/rolling-cookie.server";

    export const middleware: Route.MiddlewareFunction[] = [rollingCookieMiddleware];

    Now, every request will have the cookie updated with a new expiration date.

    Note

    If you set the same cookie in your own loaders or actions, the middleware will detect this and do nothing, so you can use the middleware and set the cookie in your own code without worrying about it.

    • MIT License

    Sergio Xalambrí

    Namespaces

    createRollingCookieMiddleware

    Functions

    createRollingCookieMiddleware