fix utm unicode
This commit is contained in:
parent
b4c4067b5e
commit
016241aec7
@ -62,17 +62,30 @@ export const getCurrentQueryParams = (): Record<string, string> => {
|
|||||||
return utmParams;
|
return utmParams;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert bytes to base64 string (handles UTF-8 properly)
|
||||||
|
* MDN recommended approach: https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa#unicode_strings
|
||||||
|
*/
|
||||||
|
const bytesToBase64 = (bytes: Uint8Array): string => {
|
||||||
|
const binString = Array.from(bytes, (byte) =>
|
||||||
|
String.fromCodePoint(byte)
|
||||||
|
).join("");
|
||||||
|
return btoa(binString);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode params as base64 JSON for state parameter
|
* Encode params as base64 JSON for state parameter
|
||||||
* Uses URL-safe base64 encoding
|
* Uses URL-safe base64 encoding with UTF-8 support
|
||||||
|
* Handles Unicode characters (e.g., utm_campaign=夏セール)
|
||||||
*/
|
*/
|
||||||
export const encodeStateParam = (params: Record<string, string>): string => {
|
export const encodeStateParam = (params: Record<string, string>): string => {
|
||||||
if (Object.keys(params).length === 0) return "";
|
if (Object.keys(params).length === 0) return "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const json = JSON.stringify(params);
|
const json = JSON.stringify(params);
|
||||||
// Use btoa for base64, replace unsafe chars for URL
|
// Encode string as UTF-8 bytes, then convert to base64
|
||||||
const base64 = btoa(json)
|
const bytes = new TextEncoder().encode(json);
|
||||||
|
const base64 = bytesToBase64(bytes)
|
||||||
.replace(/\+/g, "-")
|
.replace(/\+/g, "-")
|
||||||
.replace(/\//g, "_")
|
.replace(/\//g, "_")
|
||||||
.replace(/=+$/, "");
|
.replace(/=+$/, "");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user