From 016241aec7111cc8d146b1b08f2c8f39bbab45b4 Mon Sep 17 00:00:00 2001 From: "dev.daminik00" Date: Wed, 24 Dec 2025 22:02:12 +0300 Subject: [PATCH] fix utm unicode --- src/services/url/index.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/services/url/index.ts b/src/services/url/index.ts index 37cb2b0..88c5bd6 100644 --- a/src/services/url/index.ts +++ b/src/services/url/index.ts @@ -62,17 +62,30 @@ export const getCurrentQueryParams = (): Record => { 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 - * 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 => { if (Object.keys(params).length === 0) return ""; try { const json = JSON.stringify(params); - // Use btoa for base64, replace unsafe chars for URL - const base64 = btoa(json) + // Encode string as UTF-8 bytes, then convert to base64 + const bytes = new TextEncoder().encode(json); + const base64 = bytesToBase64(bytes) .replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/, "");