Skip to content

Commit e0c7792

Browse files
authored
[LIBWEB-1353] Fix cookie write error (#891)
1 parent cd45117 commit e0c7792

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

.changeset/pink-dancers-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
[LIBWEB-1353] Fix cookie write error

packages/browser/src/core/user/__tests__/index.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ function clear(): void {
1919
localStorage.clear()
2020
}
2121

22+
/**
23+
* Filters out the calls made for probing cookie availability
24+
*/
25+
const ignoreProbeCookieWrites = (
26+
fn: jest.SpyInstance<
27+
string | undefined,
28+
[
29+
name: string,
30+
value: string | object,
31+
options?: jar.CookieAttributes | undefined
32+
]
33+
>
34+
) => fn.mock.calls.filter((c) => c[0] !== 'ajs_cookies_check')
35+
2236
let store: LocalStorage
2337
beforeEach(function () {
2438
store = new LocalStorage()
@@ -58,7 +72,7 @@ describe('user', () => {
5872
assert(user.anonymousId()?.length === 36)
5973
expect(jar.get('ajs_anonymous_id')).toBeUndefined()
6074
expect(localStorage.getItem('ajs_anonymous_id')).toBeNull()
61-
expect(setCookieSpy.mock.calls.length).toBe(0)
75+
expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0)
6276
})
6377

6478
it('should not overwrite anonymous id', () => {
@@ -218,7 +232,7 @@ describe('user', () => {
218232
user.id('foo')
219233

220234
assert(user.anonymousId() === prev)
221-
expect(setCookieSpy.mock.calls.length).toBe(0)
235+
expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0)
222236
})
223237

224238
it('should reset anonymousId if the user id changed', () => {
@@ -227,7 +241,7 @@ describe('user', () => {
227241
user.id('baz')
228242
assert(user.anonymousId() !== prev)
229243
assert(user.anonymousId()?.length === 36)
230-
expect(setCookieSpy.mock.calls.length).toBe(0)
244+
expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0)
231245
})
232246

233247
it('should not reset anonymousId if the user id changed to null', () => {
@@ -236,7 +250,7 @@ describe('user', () => {
236250
user.id(null)
237251
assert(user.anonymousId() === prev)
238252
assert(user.anonymousId()?.length === 36)
239-
expect(setCookieSpy.mock.calls.length).toBe(0)
253+
expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0)
240254
})
241255
})
242256

@@ -795,7 +809,7 @@ describe('group', () => {
795809
expect(group.id()).toBe('gid')
796810
expect(jar.get('ajs_group_id')).toBeFalsy()
797811
expect(store.get('ajs_group_id')).toBeFalsy()
798-
expect(setCookieSpy.mock.calls.length).toBe(0)
812+
expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0)
799813
})
800814

801815
it('behaves the same as user', () => {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Cookie } from '..'
2+
3+
describe('Cookie storage', () => {
4+
it('should report cookie storage available when cookies are accessible', () => {
5+
expect(Cookie.available()).toBe(true)
6+
})
7+
8+
it('should report cookie storage unavailable when cookies are not accessible', () => {
9+
;(document as any).__defineGetter__('cookie', function () {
10+
return ''
11+
})
12+
13+
expect(Cookie.available()).toBe(false)
14+
})
15+
})

packages/browser/src/core/user/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ const ONE_YEAR = 365
6262

6363
export class Cookie extends Store {
6464
static available(): boolean {
65-
let cookieEnabled = window.navigator.cookieEnabled
66-
67-
if (!cookieEnabled) {
68-
jar.set('ajs:cookies', 'test')
69-
cookieEnabled = document.cookie.includes('ajs:cookies')
70-
jar.remove('ajs:cookies')
65+
try {
66+
const PROBE_COOKIE = 'ajs_cookies_check'
67+
jar.set(PROBE_COOKIE, 'test')
68+
const cookieEnabled = document.cookie.includes(PROBE_COOKIE)
69+
jar.remove(PROBE_COOKIE)
70+
return cookieEnabled
71+
} catch (error) {
72+
return false
7173
}
72-
73-
return cookieEnabled
7474
}
7575

7676
static get defaults(): CookieOptions {

packages/browser/src/plugins/segmentio/__tests__/normalize.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import { SegmentEvent } from '../../../core/events'
77
import { JSDOM } from 'jsdom'
88
import { version } from '../../../generated/version'
99

10+
/**
11+
* Filters out the calls made for probing cookie availability
12+
*/
13+
const ignoreProbeCookieWrites = (
14+
fn: jest.SpyInstance<
15+
string | undefined,
16+
[
17+
name: string,
18+
value: string | object,
19+
options?: cookie.CookieAttributes | undefined
20+
]
21+
>
22+
) => fn.mock.calls.filter((c) => c[0] !== 'ajs_cookies_check')
23+
1024
describe('before loading', () => {
1125
let jsdom: JSDOM
1226

@@ -317,7 +331,7 @@ describe('before loading', () => {
317331
expect(object.context.referrer.id).toEqual('medium')
318332
assert(object.context.referrer.type === 'millennial-media')
319333
expect(cookie.get('s:context.referrer')).toBeUndefined()
320-
expect(setCookieSpy).not.toHaveBeenCalled()
334+
expect(ignoreProbeCookieWrites(setCookieSpy).length).toBe(0)
321335
})
322336

323337
it('should add .referrer.id and .referrer.type from cookie', () => {

0 commit comments

Comments
 (0)