Standardize headers

This commit is contained in:
Eric Bailey
2026-02-17 17:10:18 -06:00
parent 161acac902
commit c490c181c7
4 changed files with 40 additions and 2 deletions
+10
View File
@@ -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)
+10
View File
@@ -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)
+18
View File
@@ -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(),
}
}
+2 -2
View File
@@ -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,
}
: {