-
Notifications
You must be signed in to change notification settings - Fork 1
fix referrer encoding #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
0fb5c73
04100dc
0dbd313
9dc4f41
f4a454d
6753e2a
85c0a2e
8b6f64f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,24 @@ import { headers } from "next/headers"; | |
| import { redirect } from "next/navigation"; | ||
| import { getTranslations } from "next-intl/server"; | ||
|
|
||
| const normaliseReferrer = (referrer: string | undefined) => { | ||
| if (!referrer) return undefined; | ||
|
|
||
| let current = referrer; | ||
|
|
||
| for (let i = 0; i < 3; i += 1) { | ||
| try { | ||
| const decoded = decodeURIComponent(current); | ||
| if (decoded === current) break; | ||
| current = decoded; | ||
| } catch { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return current; | ||
| }; | ||
|
||
|
|
||
| export const signUpAction = async (formData: FormData, request?: Request) => { | ||
| const t = await getTranslations("Errors"); | ||
| const email = formData.get("email")?.toString(); | ||
|
|
@@ -28,7 +46,9 @@ export const signUpAction = async (formData: FormData, request?: Request) => { | |
| const origin = headersList.get("origin"); | ||
|
|
||
| // Get attribution data | ||
| const referrer = formData.get("initial_referrer")?.toString(); | ||
| const referrer = normaliseReferrer( | ||
| formData.get("initial_referrer")?.toString() | ||
| ); | ||
|
||
| const utmSource = formData.get("utm_source")?.toString(); | ||
| const utmMedium = formData.get("utm_medium")?.toString(); | ||
| const utmCampaign = formData.get("utm_campaign")?.toString(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,17 +36,13 @@ export async function proxy(request: NextRequest) { | |
| externalReferrer && | ||
| request.method === "GET" | ||
| ) { | ||
| response.cookies.set( | ||
| INITIAL_REFERRER_COOKIE, | ||
| encodeURIComponent(externalReferrer), | ||
| { | ||
| httpOnly: false, | ||
| maxAge: INITIAL_REFERRER_MAX_AGE, | ||
| path: "/", | ||
| sameSite: "lax", | ||
| secure: request.nextUrl.protocol === "https:", | ||
| } | ||
| ); | ||
| response.cookies.set(INITIAL_REFERRER_COOKIE, externalReferrer, { | ||
| httpOnly: false, | ||
| maxAge: INITIAL_REFERRER_MAX_AGE, | ||
| path: "/", | ||
| sameSite: "lax", | ||
| secure: request.nextUrl.protocol === "https:", | ||
| }); | ||
|
Comment on lines
+39
to
+45
|
||
| } | ||
|
|
||
| return response; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,12 +47,28 @@ const getCookie = (name: string): string | null => { | |||||||||||||||||||||||||||||||||||
| if (!value) return null; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||
| return decodeURIComponent(value); | ||||||||||||||||||||||||||||||||||||
| return normaliseReferrer(value); | ||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||
| return value; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const normaliseReferrer = (referrer: string): string => { | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| return normaliseReferrer(value); | |
| } catch { | |
| return value; | |
| } | |
| }; | |
| const normaliseReferrer = (referrer: string): string => { | |
| return normalizeReferrer(value); | |
| } catch { | |
| return value; | |
| } | |
| }; | |
| const normalizeReferrer = (referrer: string): string => { |
Copilot
AI
Apr 18, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normaliseReferrer is now implemented in both src/utils/attributionUtils.ts (client) and src/app/actions.ts (server) with the same logic. To avoid the two drifting over time, consider extracting this into a shared utility (e.g. src/utils/referrer.ts) and importing it from both places.
Copilot
AI
Apr 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
storedInitialReferrer comes from localStorage.getItem(...) (type string | null), but the new conditional uses truthiness. This changes behavior vs the previous ?? logic: an empty string will now be treated as missing and fall back to the cookie. Use an explicit null check (e.g., storedInitialReferrer !== null) so empty-string values don’t change control flow unintentionally.
| const initialReferrer = storedInitialReferrer | |
| ? normaliseReferrer(storedInitialReferrer) | |
| : cookieReferrer; | |
| if (!storedInitialReferrer && initialReferrer) { | |
| storeInitialReferrer(initialReferrer); | |
| } else if ( | |
| storedInitialReferrer && | |
| const initialReferrer = | |
| storedInitialReferrer !== null | |
| ? normaliseReferrer(storedInitialReferrer) | |
| : cookieReferrer; | |
| if (storedInitialReferrer === null && initialReferrer) { | |
| storeInitialReferrer(initialReferrer); | |
| } else if ( | |
| storedInitialReferrer !== null && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming: the repository uses
normalize*in other utils; consider renamingnormaliseReferrertonormalizeReferrerhere as well for consistency and discoverability.