Skip to content

Commit

Permalink
🐛 [RUMF-1021] clear cookie cache before expanding cookie (#1080)
Browse files Browse the repository at this point in the history
ensure that we read the up to date value before overwriting it
  • Loading branch information
bcaudan authored Sep 28, 2021
1 parent 14b8568 commit 57ceab0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/browser/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface CookieOptions {
export interface CookieCache {
get: () => string | undefined
set: (value: string, expireDelay: number) => void
clearCache: () => void
}

export function cacheCookieAccess(name: string, options: CookieOptions): CookieCache {
Expand Down Expand Up @@ -41,6 +42,10 @@ export function cacheCookieAccess(name: string, options: CookieOptions): CookieC
cache = value
cacheAccess()
},
clearCache: () => {
clearTimeout(timeout)
hasCache = false
},
}
}

Expand Down
34 changes: 32 additions & 2 deletions packages/core/src/domain/sessionManagement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
getCookie,
setCookie,
} from '../browser/cookie'
import { Clock, isIE, mockClock, restorePageVisibility, setPageVisibility } from '../../test/specHelper'
import { ONE_HOUR } from '../tools/utils'
import { Clock, isIE, mockClock, restorePageVisibility, setPageVisibility, createNewEvent } from '../../test/specHelper'
import { ONE_HOUR, DOM_EVENT } from '../tools/utils'
import {
Session,
SESSION_COOKIE_NAME,
Expand Down Expand Up @@ -278,6 +278,36 @@ describe('startSessionManagement', () => {
expect(idA).toBe(idB)
})

it('should not erase other session type', () => {
startSessionManagement(COOKIE_OPTIONS, FIRST_PRODUCT_KEY, () => ({
isTracked: true,
trackingType: FakeTrackingType.TRACKED,
}))

// schedule an expandOrRenewSession
document.dispatchEvent(new CustomEvent('click'))

clock.tick(COOKIE_ACCESS_DELAY / 2)

// expand first session cookie cache
document.dispatchEvent(createNewEvent(DOM_EVENT.VISIBILITY_CHANGE))

startSessionManagement(COOKIE_OPTIONS, SECOND_PRODUCT_KEY, () => ({
isTracked: true,
trackingType: FakeTrackingType.TRACKED,
}))

// cookie correctly set
expect(getCookie(SESSION_COOKIE_NAME)).toContain('first')
expect(getCookie(SESSION_COOKIE_NAME)).toContain('second')

clock.tick(COOKIE_ACCESS_DELAY / 2)

// scheduled expandOrRenewSession should not use cached value
expect(getCookie(SESSION_COOKIE_NAME)).toContain('first')
expect(getCookie(SESSION_COOKIE_NAME)).toContain('second')
})

it('should have independent tracking types', () => {
const firstSession = startSessionManagement(COOKIE_OPTIONS, FIRST_PRODUCT_KEY, () => ({
isTracked: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/domain/sessionManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function startSessionManagement<TrackingType extends string>(

const { throttled: expandOrRenewSession } = utils.throttle(
monitor(() => {
sessionCookie.clearCache()
const cookieSession = retrieveActiveSession(sessionCookie)
const retrievedSession = { ...cookieSession }
const { trackingType, isTracked } = computeSessionState(cookieSession[productKey])
Expand Down Expand Up @@ -70,6 +71,7 @@ export function startSessionManagement<TrackingType extends string>(
)

const expandSession = () => {
sessionCookie.clearCache()
const session = retrieveActiveSession(sessionCookie)
persistSession(session, sessionCookie)
}
Expand Down

0 comments on commit 57ceab0

Please sign in to comment.