Support new backend contracts: single-flight refresh, ad impression tokens, error handling
- apiFetch: proactive refresh, cross-tab single-flight via Web Locks, no logout on transient errors - AuthContext: save tokens returned on own login change, revoke refresh on logout, sync tabs - Ads: X-Session-Id, impressionToken in impression/click, IAB viewability, safe http(s) targetUrl - STOMP: refresh before CONNECT, stop reconnect loop on dead session, /user/queue/errors behind flag - Auth form: password 8 chars / 72 bytes; 429/413 messages via describeApiError - My posts with auth + moderation badges; post form step validation and field limits - Map: escape user text in Yandex hint/template Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
const AD_SESSION_KEY = "ad_session_id";
|
||||
// Тот же формат, что принимает бэкенд (util/AdSessionId): иначе он заменит
|
||||
// недопустимые символы на "_" и обрежет id, и показы одного устройства
|
||||
// разъедутся по разным «субъектам».
|
||||
const VALID_SESSION_ID = /^[A-Za-z0-9._:-]{1,64}$/;
|
||||
|
||||
function generateAdSessionId() {
|
||||
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
||||
@@ -11,6 +15,18 @@ function generateAdSessionId() {
|
||||
});
|
||||
}
|
||||
|
||||
// Запасной id на время жизни страницы, если localStorage недоступен:
|
||||
// без него каждый запрос получал бы новый id, и лимиты показов не работали бы.
|
||||
let memorySessionId = null;
|
||||
|
||||
export function isValidAdSessionId(value) {
|
||||
return typeof value === "string" && VALID_SESSION_ID.test(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Стабильный идентификатор устройства/браузера для рекламы (X-Session-Id и
|
||||
* поле sessionId в /impression и /click). Хранится в localStorage.
|
||||
*/
|
||||
export function getAdSessionId() {
|
||||
// localStorage бросает в приватном режиме Safari и при отключённом
|
||||
// хранилище. Здесь это ломало не только трекинг, но и сам клик по рекламе
|
||||
@@ -22,13 +38,15 @@ export function getAdSessionId() {
|
||||
persisted = null;
|
||||
}
|
||||
|
||||
if (persisted) return persisted;
|
||||
if (isValidAdSessionId(persisted)) return persisted;
|
||||
if (memorySessionId) return memorySessionId;
|
||||
|
||||
const sessionId = generateAdSessionId();
|
||||
memorySessionId = sessionId;
|
||||
try {
|
||||
localStorage.setItem(AD_SESSION_KEY, sessionId);
|
||||
} catch {
|
||||
// хранилище недоступно — отдаём id без persist
|
||||
}
|
||||
return sessionId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user