Skip to content

Commit 6d1d701

Browse files
committed
revert: cookie usage
1 parent e7d64cb commit 6d1d701

File tree

5 files changed

+10
-157
lines changed

5 files changed

+10
-157
lines changed

src/components/AB/TestDebugPanel.tsx

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import { useRouter } from "next/navigation"
55

66
import { cn } from "@/lib/utils/cn"
77

8-
import { AB_TEST_COOKIE_PREFIX } from "@/lib/constants"
9-
108
import { Button } from "../ui/buttons/Button"
119

12-
import { clearABTestCookie, forceABTestVariant } from "@/lib/ab-testing/actions"
10+
import { forceABTestVariant } from "@/lib/ab-testing/actions"
1311
import { ABTestAssignment } from "@/lib/ab-testing/types"
1412

1513
type ABTestDebugPanelProps = {
@@ -33,28 +31,11 @@ export function ABTestDebugPanel({
3331
setLocalAssignment(currentAssignment)
3432
}, [currentAssignment])
3533

36-
const cookieName = AB_TEST_COOKIE_PREFIX + testKey
37-
// Check if cookie exists client-side and set fallback if needed
38-
useEffect(() => {
39-
if (currentAssignment) {
40-
const cookieExists = document.cookie.includes(cookieName)
41-
42-
if (!cookieExists) {
43-
console.log(`[AB Test Debug] Setting fallback cookie for ${testKey}`)
44-
document.cookie = `${cookieName}=${JSON.stringify(currentAssignment)}; max-age=${60 * 60 * 24 * 30}; path=/; samesite=lax`
45-
}
46-
}
47-
}, [cookieName, currentAssignment, testKey])
48-
4934
const forceVariant = async (variantName: string) => {
5035
try {
5136
const newAssignment = await forceABTestVariant(testKey, variantName)
5237
setLocalAssignment(newAssignment)
5338

54-
// Fallback: Set cookie client-side if server action doesn't persist
55-
document.cookie = `${cookieName}=${JSON.stringify(newAssignment)}; max-age=${60 * 60 * 24 * 30}; path=/; samesite=lax`
56-
57-
// Use transition for smoother updates without full refresh
5839
startTransition(() => {
5940
router.refresh()
6041
})
@@ -63,41 +44,13 @@ export function ABTestDebugPanel({
6344
}
6445
}
6546

66-
const clearCookie = async () => {
67-
try {
68-
await clearABTestCookie(testKey)
69-
70-
// Fallback: Clear cookie client-side as well
71-
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
72-
73-
setLocalAssignment(null)
74-
// Use transition for smoother updates without full refresh
75-
startTransition(() => {
76-
router.refresh()
77-
})
78-
} catch (error) {
79-
console.error("Failed to clear cookie:", error)
80-
}
81-
}
82-
83-
const resetAllTests = () => {
84-
// Clear all AB test cookies and reload page
85-
document.cookie.split(";").forEach((cookie) => {
86-
const [name] = cookie.split("=")
87-
if (name.trim().startsWith(AB_TEST_COOKIE_PREFIX)) {
88-
document.cookie = `${name.trim()}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
89-
}
90-
})
91-
window.location.reload()
92-
}
93-
9447
return (
95-
<div className="fixed bottom-5 right-5 z-sticky rounded-lg border-2 bg-background-low p-2.5 font-mono text-xs">
48+
<div className="fixed bottom-5 right-5 z-modal rounded-lg border-2 bg-background-low p-2.5 font-mono text-xs">
9649
<Button
9750
onClick={() => setIsOpen(!isOpen)}
9851
className="w-full cursor-pointer rounded border-none bg-accent-a px-2.5 py-1 font-semibold text-white hover:bg-accent-a-hover"
9952
>
100-
🧪 AB Test Debug
53+
🧪 AB Test Switcher
10154
</Button>
10255

10356
{isOpen && (
@@ -111,16 +64,9 @@ export function ABTestDebugPanel({
11164
<span className="ml-2 text-xs text-gray-500">Loading...</span>
11265
)}
11366
</div>
114-
<div className="mt-1 text-2xs">
115-
<strong>Cookie:</strong>{" "}
116-
{typeof window !== "undefined" &&
117-
document.cookie.includes(AB_TEST_COOKIE_PREFIX + testKey)
118-
? "✓ Set"
119-
: "✗ Missing"}
120-
</div>
12167
<div className="mt-2.5">
12268
<div>
123-
<strong>Force Variant:</strong>
69+
<strong>Force variant:</strong>
12470
</div>
12571
{availableVariants.map((variant) => (
12672
<Button
@@ -141,30 +87,6 @@ export function ABTestDebugPanel({
14187
{variant}
14288
</Button>
14389
))}
144-
<div className="mt-2.5 flex gap-1">
145-
<Button
146-
onClick={clearCookie}
147-
disabled={isPending}
148-
className={cn(
149-
"flex-1 rounded border-none bg-warning px-2 py-1 text-xs text-black hover:bg-warning-dark",
150-
isPending ? "cursor-not-allowed opacity-60" : "cursor-pointer"
151-
)}
152-
title="Clear this test's cookie and get new random assignment"
153-
>
154-
🔄 Reset Test
155-
</Button>
156-
<Button
157-
onClick={resetAllTests}
158-
disabled={isPending}
159-
className={cn(
160-
"flex-1 rounded border-none bg-error px-2 py-1 text-xs text-white hover:bg-error-dark",
161-
isPending ? "cursor-not-allowed opacity-60" : "cursor-pointer"
162-
)}
163-
title="Clear all AB test cookies and reload page"
164-
>
165-
🗑️ Reset All
166-
</Button>
167-
</div>
16890
</div>
16991
</div>
17092
)}

src/components/AB/TestWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const ABTestWrapper = async ({
2121
fallback,
2222
}: ABTestWrapperProps) => {
2323
try {
24-
// Get deterministic assignment (cookieless, based on user fingerprint)
24+
// Get deterministic assignment (cookie-less, based on fingerprint)
2525
const assignment = await getABTestAssignment(testKey)
2626

2727
if (!assignment) {

src/lib/ab-testing/actions.ts

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
11
"use server"
22

3-
import { cookies } from "next/headers"
4-
5-
import { AB_TEST_COOKIE_PREFIX } from "../constants"
6-
import { IS_PROD } from "../utils/env"
7-
83
import { getABTestConfigs } from "./config"
94
import { ABTestAssignment } from "./types"
105

11-
// Server Action to set AB test assignment cookie
12-
export async function setABTestCookie(
13-
testKey: string,
14-
assignment: ABTestAssignment
15-
) {
16-
const cookieStore = await cookies()
17-
const cookieName = AB_TEST_COOKIE_PREFIX + testKey
18-
const maxAge = 60 * 60 * 24 * 30 // 30 days
19-
20-
cookieStore.set(cookieName, JSON.stringify(assignment), {
21-
maxAge,
22-
httpOnly: false, // Needs to be accessible by client for potential debugging
23-
secure: IS_PROD,
24-
sameSite: "lax",
25-
path: "/",
26-
})
27-
}
28-
29-
// Server action to manually set a specific variant for testing
6+
// Server action to create a specific variant assignment for debug panel testing
7+
// Note: This doesn't persist anywhere - it's just for debug panel state
308
export async function forceABTestVariant(testKey: string, variantName: string) {
31-
const configs = getABTestConfigs()
9+
const configs = await getABTestConfigs()
3210
const testConfig = configs[testKey]
3311

3412
if (!testConfig) {
@@ -42,36 +20,10 @@ export async function forceABTestVariant(testKey: string, variantName: string) {
4220

4321
const assignment: ABTestAssignment = {
4422
experimentId: testConfig.id,
45-
experimentName: testConfig.name,
23+
experimentName: testConfig.name || testKey,
4624
variant: variantName,
4725
assignedAt: Date.now(),
4826
}
4927

50-
const cookieStore = await cookies()
51-
const cookieName = `${AB_TEST_COOKIE_PREFIX}${testKey}`
52-
const maxAge = 60 * 60 * 24 * 30 // 30 days
53-
54-
cookieStore.set(cookieName, JSON.stringify(assignment), {
55-
maxAge,
56-
httpOnly: false,
57-
secure: IS_PROD,
58-
sameSite: "lax",
59-
path: "/",
60-
})
61-
62-
if (process.env.NODE_ENV === "development") {
63-
console.log(`[AB Test] Forced variant ${variantName} for test ${testKey}`)
64-
}
6528
return assignment
6629
}
67-
68-
// Server action to clear AB test cookie
69-
export async function clearABTestCookie(testKey: string) {
70-
const cookieStore = await cookies()
71-
const cookieName = `${AB_TEST_COOKIE_PREFIX}${testKey}`
72-
73-
cookieStore.delete(cookieName)
74-
if (process.env.NODE_ENV === "development") {
75-
console.log(`[AB Test] Cleared cookie for test ${testKey}`)
76-
}
77-
}

src/lib/ab-testing/server.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import { cookies } from "next/headers"
2-
3-
import { AB_TEST_COOKIE_PREFIX } from "../constants"
4-
51
import { getABTestConfigs, getABTestConfigsSync } from "./config"
62
import { ABTestAssignment, ABTestConfig } from "./types"
73

@@ -15,23 +11,7 @@ export async function getABTestAssignment(
1511
return null
1612
}
1713

18-
const cookieStore = await cookies()
19-
const cookieName = AB_TEST_COOKIE_PREFIX + testKey
20-
const existingAssignment = cookieStore.get(cookieName)
21-
22-
if (existingAssignment?.value) {
23-
try {
24-
const parsed: ABTestAssignment = JSON.parse(existingAssignment.value)
25-
// Validate that the variant still exists in current config
26-
if (testConfig.variants.some((v) => v.name === parsed.variant)) {
27-
return parsed
28-
}
29-
} catch (error) {
30-
// Invalid cookie format, continue to reassign
31-
}
32-
}
33-
34-
// If no valid existing assignment, create deterministic assignment
14+
// Create deterministic assignment
3515
// Use IP + User-Agent as fingerprint for consistent assignment (cookie-less)
3616
const headers = await import("next/headers").then((m) => m.headers())
3717
const userAgent = headers.get("user-agent") || ""

src/lib/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const TRANSLATED_IMAGES_DIR = "/content/translations"
1212
export const PLACEHOLDER_IMAGE_DIR = "src/data/placeholders"
1313
export const INTERNAL_TUTORIALS_JSON = "src/data/internalTutorials.json"
1414
export const INTL_JSON_DIR = "src/intl"
15-
export const AB_TEST_COOKIE_PREFIX = "_pk_abtest_"
1615

1716
export const NULL_VALUE = "—"
1817

0 commit comments

Comments
 (0)