Skip to content

Commit 0fb5c73

Browse files
committed
fix referrer encoding
1 parent 74e913e commit 0fb5c73

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

src/app/actions.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ import { headers } from "next/headers";
1313
import { redirect } from "next/navigation";
1414
import { getTranslations } from "next-intl/server";
1515

16+
const normaliseReferrer = (referrer: string | undefined) => {
17+
if (!referrer) return undefined;
18+
19+
let current = referrer;
20+
21+
for (let i = 0; i < 3; i += 1) {
22+
try {
23+
const decoded = decodeURIComponent(current);
24+
if (decoded === current) break;
25+
current = decoded;
26+
} catch {
27+
break;
28+
}
29+
}
30+
31+
return current;
32+
};
33+
1634
export const signUpAction = async (formData: FormData, request?: Request) => {
1735
const t = await getTranslations("Errors");
1836
const email = formData.get("email")?.toString();
@@ -28,7 +46,9 @@ export const signUpAction = async (formData: FormData, request?: Request) => {
2846
const origin = headersList.get("origin");
2947

3048
// Get attribution data
31-
const referrer = formData.get("initial_referrer")?.toString();
49+
const referrer = normaliseReferrer(
50+
formData.get("initial_referrer")?.toString()
51+
);
3252
const utmSource = formData.get("utm_source")?.toString();
3353
const utmMedium = formData.get("utm_medium")?.toString();
3454
const utmCampaign = formData.get("utm_campaign")?.toString();

src/proxy.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,13 @@ export async function proxy(request: NextRequest) {
3636
externalReferrer &&
3737
request.method === "GET"
3838
) {
39-
response.cookies.set(
40-
INITIAL_REFERRER_COOKIE,
41-
encodeURIComponent(externalReferrer),
42-
{
43-
httpOnly: false,
44-
maxAge: INITIAL_REFERRER_MAX_AGE,
45-
path: "/",
46-
sameSite: "lax",
47-
secure: request.nextUrl.protocol === "https:",
48-
}
49-
);
39+
response.cookies.set(INITIAL_REFERRER_COOKIE, externalReferrer, {
40+
httpOnly: false,
41+
maxAge: INITIAL_REFERRER_MAX_AGE,
42+
path: "/",
43+
sameSite: "lax",
44+
secure: request.nextUrl.protocol === "https:",
45+
});
5046
}
5147

5248
return response;

src/utils/attributionUtils.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,28 @@ const getCookie = (name: string): string | null => {
4747
if (!value) return null;
4848

4949
try {
50-
return decodeURIComponent(value);
50+
return normaliseReferrer(value);
5151
} catch {
5252
return value;
5353
}
5454
};
5555

56+
const normaliseReferrer = (referrer: string): string => {
57+
let current = referrer;
58+
59+
for (let i = 0; i < 3; i += 1) {
60+
try {
61+
const decoded = decodeURIComponent(current);
62+
if (decoded === current) break;
63+
current = decoded;
64+
} catch {
65+
break;
66+
}
67+
}
68+
69+
return current;
70+
};
71+
5672
const getExternalDocumentReferrer = (): string | null => {
5773
const referrer = document.referrer;
5874

@@ -72,7 +88,7 @@ const getExternalDocumentReferrer = (): string | null => {
7288
};
7389

7490
const storeInitialReferrer = (referrer: string) => {
75-
localStorage.setItem(INITIAL_REFERRER_KEY, referrer);
91+
localStorage.setItem(INITIAL_REFERRER_KEY, normaliseReferrer(referrer));
7692
};
7793

7894
export function captureAttributionParams() {
@@ -125,10 +141,17 @@ export function getStoredAttributionParams(): StoredAttributionParams {
125141
const stored = localStorage.getItem(UTM_STORAGE_KEY);
126142
const storedInitialReferrer = localStorage.getItem(INITIAL_REFERRER_KEY);
127143
const cookieReferrer = getCookie(INITIAL_REFERRER_COOKIE);
128-
const initialReferrer = storedInitialReferrer ?? cookieReferrer;
144+
const initialReferrer = storedInitialReferrer
145+
? normaliseReferrer(storedInitialReferrer)
146+
: cookieReferrer;
129147

130148
if (!storedInitialReferrer && initialReferrer) {
131149
storeInitialReferrer(initialReferrer);
150+
} else if (
151+
storedInitialReferrer &&
152+
initialReferrer !== storedInitialReferrer
153+
) {
154+
storeInitialReferrer(initialReferrer);
132155
}
133156

134157
return {

0 commit comments

Comments
 (0)