chore: exclude local resource artifacts from main sync
This commit is contained in:
56
src/app/main/clientIdentity.ts
Executable file → Normal file
56
src/app/main/clientIdentity.ts
Executable file → Normal file
@@ -1,6 +1,28 @@
|
||||
import type { AppPageDescriptor } from '../../store/appStore/types';
|
||||
import { isPreviewRuntime } from './previewRuntime';
|
||||
|
||||
export const CLIENT_ID_STORAGE_KEY = 'work-app.visitor.client-id';
|
||||
const PREVIEW_CLIENT_ID_STORAGE_KEY = 'work-app.preview-runtime.client-id';
|
||||
|
||||
function getClientStorage() {
|
||||
if (typeof window === 'undefined') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isPreviewRuntime()) {
|
||||
return {
|
||||
key: PREVIEW_CLIENT_ID_STORAGE_KEY,
|
||||
primaryStorage: window.localStorage,
|
||||
legacyStorage: window.sessionStorage,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
key: CLIENT_ID_STORAGE_KEY,
|
||||
primaryStorage: window.localStorage,
|
||||
legacyStorage: null,
|
||||
};
|
||||
}
|
||||
|
||||
function generateFallbackClientId() {
|
||||
return `client-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
||||
@@ -15,19 +37,38 @@ function generateClientId() {
|
||||
}
|
||||
|
||||
export function getClientId() {
|
||||
if (typeof window === 'undefined') {
|
||||
const storageConfig = getClientStorage();
|
||||
|
||||
if (!storageConfig) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return window.localStorage.getItem(CLIENT_ID_STORAGE_KEY)?.trim() ?? '';
|
||||
const savedClientId = storageConfig.primaryStorage.getItem(storageConfig.key)?.trim() ?? '';
|
||||
|
||||
if (savedClientId) {
|
||||
return savedClientId;
|
||||
}
|
||||
|
||||
const legacyClientId = storageConfig.legacyStorage?.getItem(storageConfig.key)?.trim() ?? '';
|
||||
|
||||
if (legacyClientId) {
|
||||
storageConfig.primaryStorage.setItem(storageConfig.key, legacyClientId);
|
||||
storageConfig.legacyStorage?.removeItem(storageConfig.key);
|
||||
return legacyClientId;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
export function clearClientId() {
|
||||
if (typeof window === 'undefined') {
|
||||
const storageConfig = getClientStorage();
|
||||
|
||||
if (!storageConfig) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.localStorage.removeItem(CLIENT_ID_STORAGE_KEY);
|
||||
storageConfig.primaryStorage.removeItem(storageConfig.key);
|
||||
storageConfig.legacyStorage?.removeItem(storageConfig.key);
|
||||
}
|
||||
|
||||
export function getOrCreateClientId() {
|
||||
@@ -37,12 +78,15 @@ export function getOrCreateClientId() {
|
||||
return existingClientId;
|
||||
}
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
const storageConfig = getClientStorage();
|
||||
|
||||
if (!storageConfig) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const nextClientId = generateClientId();
|
||||
window.localStorage.setItem(CLIENT_ID_STORAGE_KEY, nextClientId);
|
||||
storageConfig.primaryStorage.setItem(storageConfig.key, nextClientId);
|
||||
storageConfig.legacyStorage?.removeItem(storageConfig.key);
|
||||
return nextClientId;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user