Standardize headers
This commit is contained in:
@@ -18,6 +18,16 @@ export function getInitialSessionId() {
|
||||
return sessionId
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current session ID. Freshness depends on `useSessionId` being
|
||||
* mounted, which handles refreshing this value between foreground/background
|
||||
* transitions. Since that's mounted in `analytics/index.tsx`, this value can
|
||||
* generally be trusted to be up to date.
|
||||
*/
|
||||
export function getSessionId() {
|
||||
return device.get(['nativeSessionId'])
|
||||
}
|
||||
|
||||
export function useSessionId() {
|
||||
const [id, setId] = useState(() => sessionId)
|
||||
|
||||
|
||||
@@ -21,6 +21,16 @@ export function getInitialSessionId() {
|
||||
return sessionId
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current session ID. Freshness depends on `useSessionId` being
|
||||
* mounted, which handles refreshing this value between foreground/background
|
||||
* transitions. Since that's mounted in `analytics/index.tsx`, this value can
|
||||
* generally be trusted to be up to date.
|
||||
*/
|
||||
export function getSessionId() {
|
||||
return window.sessionStorage.getItem(SESSION_ID_KEY)
|
||||
}
|
||||
|
||||
export function useSessionId() {
|
||||
const [id, setId] = useState(() => sessionId)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import {useMemo} from 'react'
|
||||
|
||||
import {BSKY_SERVICE} from '#/lib/constants'
|
||||
import {type SessionAccount} from '#/state/session'
|
||||
import {getDeviceId, getSessionId} from '#/analytics/identifiers'
|
||||
import {
|
||||
type MergeableMetadata,
|
||||
type SessionMetadata,
|
||||
@@ -31,3 +32,20 @@ export function accountToSessionMetadata(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get anonymous identifiers and construct headers we use for requests that may
|
||||
* trigger experiment exposures in our backend. These values are used for A/B
|
||||
* test bucketing, in addition to the user DID, if the request is
|
||||
* authenticated. They ensure we can attribute exposures to a specific device
|
||||
* and session, even for unauthenticated requests.
|
||||
*
|
||||
* These headers must stay in sync with our appview.
|
||||
* @see https://github.com/bluesky-social/atproto/blob/39cf199df5847d3fd4a60d8cdeb604a0e07f9784/packages/bsky/src/feature-gates/utils.ts#L7-L8
|
||||
*/
|
||||
export function getAnalyticsHeaders() {
|
||||
return {
|
||||
'X-Bsky-Device-Id': getDeviceId(),
|
||||
'X-Bsky-Session-Id': getSessionId(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
getAppLanguageAsContentLanguage,
|
||||
getContentLanguages,
|
||||
} from '#/state/preferences/languages'
|
||||
import {getDeviceId} from '#/analytics/identifiers'
|
||||
import {getAnalyticsHeaders} from '#/analytics/utils'
|
||||
import {type FeedAPI, type FeedAPIResponse} from './types'
|
||||
import {createBskyTopicsHeader, isBlueskyOwnedFeed} from './utils'
|
||||
|
||||
@@ -66,7 +66,7 @@ export class CustomFeedAPI implements FeedAPI {
|
||||
headers: isBlueskyOwned
|
||||
? {
|
||||
...createBskyTopicsHeader(this.userInterests),
|
||||
'X-Bsky-Stable-Id': getDeviceId(),
|
||||
...getAnalyticsHeaders(),
|
||||
'Accept-Language': contentLangs,
|
||||
}
|
||||
: {
|
||||
|
||||
Reference in New Issue
Block a user