Skip to content

Commit ac2064f

Browse files
heiskrCopilot
andauthored
Standardize cookie names to snake_case with central constants (#60140)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 255375f commit ac2064f

12 files changed

Lines changed: 63 additions & 42 deletions

File tree

src/color-schemes/components/useTheme.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useState, useEffect } from 'react'
22
import Cookies from '../../frame/components/lib/cookies'
3+
import { COLOR_MODE_COOKIE_NAME } from '@/frame/lib/constants'
34

45
enum CssColorMode {
56
auto = 'auto',
@@ -118,7 +119,7 @@ export function useTheme() {
118119
// under the hood, which Primer is planning to do in the next couple quarters.
119120
// Reference: https://github.com/primer/react/issues/2229
120121
setTimeout(() => {
121-
const cookieValue = Cookies.get('color_mode')
122+
const cookieValue = Cookies.get(COLOR_MODE_COOKIE_NAME)
122123
const css = getCssTheme(cookieValue)
123124
const component = getComponentTheme(cookieValue)
124125
setTheme({ css, component })

src/events/components/events.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import Cookies from '@/frame/components/lib/cookies'
2-
import { ANALYTICS_ENABLED } from '@/frame/lib/constants'
2+
import {
3+
ANALYTICS_ENABLED,
4+
ANNOTATE_MODE_COOKIE_NAME,
5+
DOCS_EVENTS_COOKIE_NAME,
6+
TOOL_PREFERRED_COOKIE_NAME,
7+
OS_PREFERRED_COOKIE_NAME,
8+
} from '@/frame/lib/constants'
39
import { parseUserAgent } from './user-agent'
410
import { Router } from 'next/router'
511
import { isLoggedIn } from '@/frame/components/hooks/useHasAccount'
@@ -8,8 +14,6 @@ import { EventType, EventPropsByType } from '../types'
814
import { isHeadless } from './is-headless'
915
import { sendHydroAnalyticsEvent, getOctoClientId } from './hydro-analytics'
1016

11-
const COOKIE_NAME = '_docs-events'
12-
1317
const startVisitTime = Date.now()
1418

1519
const BATCH_INTERVAL = 5000 // 5 seconds
@@ -66,10 +70,10 @@ export function uuidv4(): string {
6670

6771
export function getUserEventsId() {
6872
if (cookieValue) return cookieValue
69-
cookieValue = Cookies.get(COOKIE_NAME)
73+
cookieValue = Cookies.get(DOCS_EVENTS_COOKIE_NAME)
7074
if (cookieValue) return cookieValue
7175
cookieValue = uuidv4()
72-
Cookies.set(COOKIE_NAME, cookieValue)
76+
Cookies.set(DOCS_EVENTS_COOKIE_NAME, cookieValue)
7377
return cookieValue
7478
}
7579

@@ -136,10 +140,10 @@ export function sendEvent<T extends EventType>({
136140
user_language: navigator.language,
137141

138142
// Preference information
139-
application_preference: Cookies.get('toolPreferred'),
143+
application_preference: Cookies.get(TOOL_PREFERRED_COOKIE_NAME),
140144
color_mode_preference: getColorModePreference(),
141-
os_preference: Cookies.get('osPreferred'),
142-
code_display_preference: Cookies.get('annotate-mode'),
145+
os_preference: Cookies.get(OS_PREFERRED_COOKIE_NAME),
146+
code_display_preference: Cookies.get(ANNOTATE_MODE_COOKIE_NAME),
143147

144148
experiment_variation:
145149
getExperimentVariationForContext(

src/events/middleware.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { noCacheControl } from '@/frame/middleware/cache-control'
1313
import { getJsonValidator } from '@/tests/lib/validate-json-schema'
1414
import { formatErrors } from './lib/middleware-errors'
1515
import { publish as _publish } from './lib/hydro'
16+
import { DOTCOM_USER_COOKIE_NAME, STAFFONLY_COOKIE_NAME } from '@/frame/lib/constants'
1617
import { analyzeComment, getGuessedLanguage } from './lib/analyze-comment'
1718
import { EventType, EventProps, EventPropsByType } from './types'
1819

@@ -77,8 +78,10 @@ router.post(
7778
if (body.context) {
7879
// Add dotcom_user to the context if it's available
7980
// JSON.stringify removes `undefined` values but not `null`, and we don't want to send `null` to Hydro
80-
body.context.dotcom_user = req.cookies?.dotcom_user ? req.cookies.dotcom_user : undefined
81-
body.context.is_staff = Boolean(req.cookies?.staffonly)
81+
body.context.dotcom_user = req.cookies?.[DOTCOM_USER_COOKIE_NAME]
82+
? req.cookies[DOTCOM_USER_COOKIE_NAME]
83+
: undefined
84+
body.context.is_staff = Boolean(req.cookies?.[STAFFONLY_COOKIE_NAME])
8285
// Add IP address and user agent from request
8386
// Moda forwards the client's IP using the `fastly-client-ip` header
8487
body.context.ip = req.headers['fastly-client-ip'] as string | undefined

src/frame/components/hooks/useHasAccount.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useState, useEffect } from 'react'
22
import Cookies from '@/frame/components/lib/cookies'
3+
import { COLOR_MODE_COOKIE_NAME, PREFERRED_COLOR_MODE_COOKIE_NAME } from '@/frame/lib/constants'
34

45
// Measure if the user has a github.com account and signed in during this session.
56
// The github.com sends the color_mode cookie every request when you sign in,
@@ -21,7 +22,7 @@ export function useHasAccount() {
2122
}
2223

2324
export function isLoggedIn() {
24-
const cookieValue = Cookies.get('color_mode')
25-
const altCookieValue = Cookies.get('preferred_color_mode')
25+
const cookieValue = Cookies.get(COLOR_MODE_COOKIE_NAME)
26+
const altCookieValue = Cookies.get(PREFERRED_COLOR_MODE_COOKIE_NAME)
2627
return Boolean(cookieValue || altCookieValue)
2728
}

src/frame/components/lib/toggle-annotations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Cookies from '@/frame/components/lib/cookies'
22
import { sendEvent } from '@/events/components/events'
33
import { EventType } from '@/events/types'
4+
import { ANNOTATE_MODE_COOKIE_NAME } from '@/frame/lib/constants'
45

56
enum annotationMode {
67
Beside = 'beside',
@@ -23,7 +24,7 @@ export default function toggleAnnotation() {
2324
const annotationButtons = Array.from(document.querySelectorAll('.annotate-toggle button'))
2425
if (!annotationButtons.length) return
2526

26-
const cookie = validateMode(Cookies.get('annotate-mode')) // will default to beside
27+
const cookie = validateMode(Cookies.get(ANNOTATE_MODE_COOKIE_NAME)) // will default to beside
2728
displayAnnotationMode(annotationButtons, cookie)
2829

2930
// this loop adds event listeners for both the annotation buttons
@@ -33,7 +34,7 @@ export default function toggleAnnotation() {
3334

3435
// validate the annotation mode and set the cookie with the valid mode
3536
const validMode = validateMode(annotationBtn.getAttribute('value')!)
36-
Cookies.set('annotate-mode', validMode!)
37+
Cookies.set(ANNOTATE_MODE_COOKIE_NAME, validMode!)
3738
sendEvent({
3839
type: EventType.preference,
3940
preference_name: 'code_display',

src/frame/lib/constants.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1-
const isDev = process.env.NODE_ENV === 'development'
2-
3-
// 10 seconds, by default in production and tests.
4-
// 15 seconds, by default in development.
5-
// Why more in development? Because Next.js compilation is JIT (Just-In-Time)
6-
// so that starting `npm start` and viewing your first page on localhost:4000
7-
// can, these days, take more than 10 seconds when the computer isn't
8-
// very fast. That first compilation can take long even of fast hardware
9-
// if the local contributor is running several other resource-intensive
10-
// applications at the same time.
11-
const DEFAULT_MAX_REQUEST_TIMEOUT = isDev ? 15_000 : 10_000
12-
131
export const ROOT = process.env.ROOT || '.'
14-
export const USER_LANGUAGE_COOKIE_NAME = 'user_language'
15-
export const MACHINE_TRANSLATION_BANNER_COOKIE_NAME = 'machine_translation_banner_seen'
16-
export const USER_VERSION_COOKIE_NAME = 'user_version'
172
export const TRANSLATIONS_ROOT = process.env.TRANSLATIONS_ROOT || 'translations'
3+
export const TRANSLATIONS_FIXTURE_ROOT = process.env.TRANSLATIONS_FIXTURE_ROOT
4+
5+
const isDev = process.env.NODE_ENV === 'development'
6+
// Higher in development to account for JIT compilation on first page load.
7+
const DEFAULT_MAX_REQUEST_TIMEOUT = isDev ? 15_000 : 10_000
188
export const MAX_REQUEST_TIMEOUT = process.env.REQUEST_TIMEOUT
199
? parseInt(process.env.REQUEST_TIMEOUT, 10)
2010
: DEFAULT_MAX_REQUEST_TIMEOUT
21-
export const TRANSLATIONS_FIXTURE_ROOT = process.env.TRANSLATIONS_FIXTURE_ROOT
11+
12+
// Docs cookies — we own these and use snake_case naming.
13+
export const USER_LANGUAGE_COOKIE_NAME = 'user_language' // Also referenced in Fastly VCL
14+
export const USER_VERSION_COOKIE_NAME = 'user_version' // Also referenced in Fastly VCL
15+
export const API_VERSION_COOKIE_NAME = 'api_version_preferred'
16+
export const ANNOTATE_MODE_COOKIE_NAME = 'annotate_mode'
17+
export const CODE_SAMPLE_LANGUAGE_COOKIE_NAME = 'code_sample_language_preferred'
18+
export const TOOL_PREFERRED_COOKIE_NAME = 'tool_preferred'
19+
export const OS_PREFERRED_COOKIE_NAME = 'os_preferred'
20+
export const DOCS_EVENTS_COOKIE_NAME = 'docs_events'
21+
export const MACHINE_TRANSLATION_BANNER_COOKIE_NAME = 'machine_translation_banner_seen'
22+
23+
// Monolith cookies — set by github.com, read-only for us. Names are not
24+
// ours to change so they don't follow our snake_case convention.
25+
export const COLOR_MODE_COOKIE_NAME = 'color_mode'
26+
export const PREFERRED_COLOR_MODE_COOKIE_NAME = 'preferred_color_mode'
27+
export const DOTCOM_USER_COOKIE_NAME = 'dotcom_user'
28+
export const STAFFONLY_COOKIE_NAME = 'staffonly'
29+
30+
// Feature flags
31+
export const ANALYTICS_ENABLED = true
32+
export const HOVERCARDS_ENABLED = true
2233

2334
// Minimum required HTML for 404: W3C valid, no external, legal.
2435
export const minimumNotFoundHtml = `
@@ -36,6 +47,3 @@ export const minimumNotFoundHtml = `
3647
&bull; <a href=https://docs.github.com/site-policy/privacy-policies/github-privacy-statement>Privacy</a>
3748
</small>
3849
`.replace(/\n/g, '')
39-
40-
export const ANALYTICS_ENABLED = true
41-
export const HOVERCARDS_ENABLED = true

src/frame/middleware/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import article from '@/article-api/middleware/article'
1111
import webhooks from '@/webhooks/middleware/webhooks'
1212
import { ExtendedRequest } from '@/types'
1313
import { noCacheControl } from './cache-control'
14+
import { STAFFONLY_COOKIE_NAME } from '@/frame/lib/constants'
1415

1516
const router = express.Router()
1617

@@ -55,7 +56,7 @@ if (process.env.ELASTICSEARCH_URL) {
5556
router.get('/cookies', (req, res) => {
5657
noCacheControl(res)
5758
const cookies = {
58-
isStaff: Boolean(req.cookies?.staffonly?.startsWith('yes')) || false,
59+
isStaff: Boolean(req.cookies?.[STAFFONLY_COOKIE_NAME]?.startsWith('yes')) || false,
5960
}
6061
res.json(cookies)
6162
})

src/rest/components/ApiVersionPicker.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useMainContext } from '@/frame/components/context/MainContext'
66
import { DEFAULT_VERSION, useVersion } from '@/versions/components/useVersion'
77
import { Picker } from '@/tools/components/Picker'
88
import { useTranslation } from '@/languages/components/useTranslation'
9-
import { API_VERSION_COOKIE_NAME } from '@/rest/components/RestRedirect'
9+
import { API_VERSION_COOKIE_NAME } from '@/frame/lib/constants'
1010
import { apiVersionPath } from '@/rest/lib/config'
1111

1212
const API_VERSION_SUFFIX = ' (latest)'

src/rest/components/RestCodeSamples.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import hljsCurl from 'highlightjs-curl'
1313

1414
import { useTranslation } from '@/languages/components/useTranslation'
1515
import useClipboard from '@/rest/components/useClipboard'
16+
import { CODE_SAMPLE_LANGUAGE_COOKIE_NAME } from '@/frame/lib/constants'
1617
import {
1718
getShellExample,
1819
getGHExample,
@@ -114,14 +115,14 @@ export function RestCodeSamples({ operation, slug, heading }: Props) {
114115

115116
const handleLanguageSelection = (languageKey: CodeSampleKeys) => {
116117
setSelectedLanguage(languageKey)
117-
Cookies.set('codeSampleLanguagePreferred', languageKey)
118+
Cookies.set(CODE_SAMPLE_LANGUAGE_COOKIE_NAME, languageKey)
118119
}
119120

120121
// Change the language based on cookies
121122
useEffect(() => {
122123
// If the user previously selected a language preference and the language
123124
// is available in this component set it as the selected language
124-
const cookieValue = Cookies.get('codeSampleLanguagePreferred')
125+
const cookieValue = Cookies.get(CODE_SAMPLE_LANGUAGE_COOKIE_NAME)
125126
const preferredCodeLanguage = languageSelectOptions.find((item) => item === cookieValue)
126127
if (cookieValue && preferredCodeLanguage) {
127128
setSelectedLanguage(cookieValue as CodeSampleKeys)

src/rest/components/RestRedirect.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import Cookies from '@/frame/components/lib/cookies'
44

55
import { useVersion } from '@/versions/components/useVersion'
66
import { useMainContext } from '@/frame/components/context/MainContext'
7-
8-
export const API_VERSION_COOKIE_NAME = 'apiVersionPreferred'
7+
import { API_VERSION_COOKIE_NAME } from '@/frame/lib/constants'
98

109
// This component allows us to set the URL Param for the REST API Calendar Date version
1110
// We set a cookie as well to remember what calendar date version the user is on

0 commit comments

Comments
 (0)