Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/analytics-core/src/storage/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class CookieStorage<T> implements Storage<T> {

async getRaw(key: string): Promise<string | undefined> {
const globalScope = getGlobalScope();
const cookie = globalScope?.document?.cookie.split('; ') ?? [];
const cookieStr = globalScope?.document?.cookie ?? '';
const cookie = cookieStr.split(/;\s*/);
const match = cookie.find((c) => c.indexOf(key + '=') === 0);
if (!match) {
return undefined;
Expand Down
10 changes: 10 additions & 0 deletions packages/analytics-core/test/storage/cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ describe('cookies', () => {
await cookies.remove('hello');
});

test('should parse cookies without spaces', async () => {
const cookies = new CookieStorage();
const value = { b: 2 };
const encodedValue = btoa(encodeURIComponent(JSON.stringify(value)));
jest.spyOn(GlobalScopeModule, 'getGlobalScope').mockReturnValueOnce({
document: { cookie: `hello=${encodedValue};hello2=${encodedValue}` },
} as unknown as typeof globalThis);
expect(await cookies.get('hello2')).toEqual(value);
});

test('should return undefined when global scope is not defined', async () => {
const cookies = new CookieStorage<number[]>();
await cookies.set('hello', [1]);
Expand Down
Loading