Long cookies #7742
Replies: 3 comments 15 replies
-
That specific length - 4096 bytes - is not an accident. It's actually a limitation of browsers themselves, not the Remix API. http://browsercookielimits.iain.guru You've got a couple of options here:
|
Beta Was this translation helpful? Give feedback.
-
@KnisterPeter, what did you end up doing? I'm looking at the exact same situation. |
Beta Was this translation helpful? Give feedback.
-
Here is how I handled this is issue by creating a getSessionStorage helper function. import { config } from './config/env-config';
import type { SessionData, CookieSerializeOptions, Session, CookieParseOptions } from '@remix-run/server-runtime';
interface IGetSessionStorageResponse {
commitSession: (data: Session<SessionData, SessionData>, options?: CookieSerializeOptionsExtended) => Promise<string>;
destroySession: (session: Session<SessionData, SessionData>, options?: CookieSerializeOptionsExtended) => Promise<string>;
getSession: (cookieHeader?: string | null, options?: CookieParseOptions & { isExtended?: boolean }) => Promise<Session<SessionData, SessionData>>;
}
type CookieSerializeOptionsExtended = CookieSerializeOptions & { isExtended?: boolean };
export const getSessionStorage = async (): Promise<IGetSessionStorageResponse> => {
const { createCookieSessionStorage } = await import('@remix-run/node/dist/implementations.js');
let sessionStorage = createCookieSessionStorage({
cookie: {
name: '_session', // use any name you want here
sameSite: 'lax', // this helps with CSRF
... // add your cookie options here
},
});
let sessionExtendedStorage = createCookieSessionStorage({
cookie: {
name: '_session-extended', // use any name you want here
sameSite: 'lax', // this helps with CSRF
... // add your cookie options here
},
});
const commitSession = (data: Session<SessionData, SessionData>, options: CookieSerializeOptionsExtended) => {
if (options?.isExtended) {
return sessionExtendedStorage.commitSession(data, options);
}
return sessionStorage.commitSession(data, options);
}
const getSession = (cookieHeader?: string | null, options?: CookieParseOptions & { isExtended?: boolean }) => {
if (options?.isExtended) {
return sessionExtendedStorage.getSession(cookieHeader, options);
}
return sessionStorage.getSession(cookieHeader, options);
}
const destroySession = (session: Session<SessionData, SessionData>, options?: CookieSerializeOptionsExtended) => {
if (options?.isExtended) {
return sessionExtendedStorage.destroySession(session, options);
}
return sessionStorage.destroySession(session, options);
}
return {
commitSession,
getSession,
destroySession
};
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
the current
SessionStorage
api does not allow cookies which are too long. The default implementation throws if a cookie is more than 4096 bytes. This could be too short if there are longer data to be stored which would required multiple cookies to encode/decode all data.I understand that this is not part of the default cookie implementation, but the current API of the session storage does only return a single string (cookie header) from the
commitSession
call. It does not even receive a response object to write additional headers (multiple cookies) or deal with that in any other way.Someone elso also has this issue already? How to deal with that?
(e.g. we would like to use
remix-auth
with OAuth2 strategy. And the resulting token, refresh-token, id-token are too long for a cookie)Beta Was this translation helpful? Give feedback.
All reactions