@v-tilt/browser 1.4.3 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/storage.d.ts CHANGED
@@ -1,23 +1,41 @@
1
1
  /**
2
- * Unified Storage Manager - Following PostHog's PostHogPersistence pattern
2
+ * Unified Storage Manager
3
3
  *
4
- * Single class handles all storage operations for both sessions and users.
5
- * Reduces code duplication and ensures consistent cookie handling.
4
+ * Handles all storage operations for sessions and users with consistent
5
+ * cookie handling across different persistence methods.
6
6
  *
7
7
  * Storage methods:
8
- * - cookie: Browser cookies (cross-subdomain support)
9
- * - localStorage: Persistent local storage
10
- * - sessionStorage: Tab-specific storage (cleared on tab close)
11
- * - localStorage+cookie: Both for redundancy
12
- * - memory: In-memory only (no persistence)
8
+ * - `cookie`: Browser cookies with cross-subdomain support
9
+ * - `localStorage`: Persistent local storage
10
+ * - `sessionStorage`: Tab-specific storage (cleared on tab close)
11
+ * - `localStorage+cookie`: Hybrid mode (default) - full data in localStorage,
12
+ * critical identity properties also in cookies for SSR support
13
+ * - `memory`: In-memory only (no persistence)
14
+ *
15
+ * The `localStorage+cookie` mode is designed for traditional server-side
16
+ * rendered websites where each page navigation reloads JavaScript:
17
+ * - Critical properties (anonymous_id, device_id, etc.) are stored in cookies
18
+ * - Cookies ensure identity persists across full page reloads
19
+ * - localStorage provides fast access for SPA-style navigation
20
+ * - Falls back to cookie-only if localStorage is unavailable
13
21
  */
14
22
  import { PersistenceMethod } from "./types";
23
+ /**
24
+ * Critical properties persisted to cookies in `localStorage+cookie` mode.
25
+ * These ensure identity survives full page reloads in traditional SSR websites.
26
+ */
27
+ export declare const COOKIE_PERSISTED_PROPERTIES: readonly ["__vt_anonymous_id", "__vt_device_id", "__vt_distinct_id", "__vt_user_state"];
28
+ /** Session cookie TTL: 30 minutes */
15
29
  export declare const SESSION_COOKIE_MAX_AGE = 1800;
30
+ /** User cookie TTL: 1 year */
16
31
  export declare const USER_COOKIE_MAX_AGE = 31536000;
17
32
  export interface StorageOptions {
18
33
  method: PersistenceMethod;
34
+ /** Enable cross-subdomain cookies (auto-detected if not specified) */
19
35
  cross_subdomain?: boolean;
36
+ /** Use secure cookies (auto-detected from protocol if not specified) */
20
37
  secure?: boolean;
38
+ /** Cookie SameSite attribute */
21
39
  sameSite?: "Strict" | "Lax" | "None";
22
40
  }
23
41
  export interface StorageItem<T = string> {
@@ -25,13 +43,15 @@ export interface StorageItem<T = string> {
25
43
  expiry?: number;
26
44
  }
27
45
  /**
28
- * Auto-detect if cross-subdomain cookies should be enabled
29
- * Returns false for platforms like herokuapp.com, vercel.app, netlify.app
46
+ * Check if cross-subdomain cookies should be enabled.
47
+ * Returns false for shared hosting platforms to prevent cookie conflicts.
30
48
  */
31
49
  export declare function shouldUseCrossSubdomainCookie(): boolean;
32
50
  /**
33
51
  * Unified Storage Manager
52
+ *
34
53
  * Provides consistent storage operations across all persistence methods
54
+ * with automatic fallbacks and SSR support.
35
55
  */
36
56
  export declare class StorageManager {
37
57
  private method;
@@ -39,62 +59,59 @@ export declare class StorageManager {
39
59
  private secure;
40
60
  private sameSite;
41
61
  private memoryStorage;
62
+ private _localStorageSupported;
42
63
  constructor(options: StorageOptions);
64
+ /** Check if localStorage is available (result is cached) */
65
+ private isLocalStorageSupported;
66
+ /** Check if a key should be persisted to cookies */
67
+ private isCriticalProperty;
43
68
  /**
44
- * Get a value from storage
69
+ * Get a value from storage.
70
+ *
71
+ * For `localStorage+cookie` mode:
72
+ * - Critical properties: read cookie first (for SSR), fall back to localStorage
73
+ * - Non-critical properties: read from localStorage only
45
74
  */
46
75
  get(key: string): string | null;
47
76
  /**
48
- * Set a value in storage
49
- * @param maxAge - Cookie max age in seconds (ignored for localStorage/sessionStorage)
77
+ * Set a value in storage.
78
+ * @param maxAge Cookie max age in seconds (only for cookie-based storage)
79
+ *
80
+ * For `localStorage+cookie` mode:
81
+ * - All values stored in localStorage (if available)
82
+ * - Critical properties ALWAYS stored in cookies for SSR support
50
83
  */
51
84
  set(key: string, value: string, maxAge?: number): void;
52
- /**
53
- * Remove a value from storage
54
- */
85
+ /** Remove a value from storage */
55
86
  remove(key: string): void;
56
- /**
57
- * Get JSON value with expiry check
58
- * Returns null if expired or not found
59
- */
87
+ private getLocalStoragePlusCookie;
88
+ private setLocalStoragePlusCookie;
89
+ private removeLocalStoragePlusCookie;
90
+ private getLocalStorage;
91
+ private setLocalStorage;
92
+ private removeLocalStorage;
93
+ private readFromSessionStorage;
94
+ private writeToSessionStorage;
95
+ private removeFromSessionStorage;
96
+ private getCookie;
97
+ private setCookie;
98
+ private removeCookie;
99
+ /** Get JSON value with expiry check */
60
100
  getWithExpiry<T>(key: string): T | null;
61
- /**
62
- * Set JSON value with optional expiry
63
- * @param ttlMs - Time to live in milliseconds
64
- */
101
+ /** Set JSON value with optional TTL */
65
102
  setWithExpiry<T>(key: string, value: T, ttlMs?: number): void;
66
- /**
67
- * Get JSON value (no expiry check)
68
- */
103
+ /** Get parsed JSON value */
69
104
  getJSON<T>(key: string): T | null;
70
- /**
71
- * Set JSON value
72
- */
105
+ /** Set JSON value */
73
106
  setJSON<T>(key: string, value: T, maxAge?: number): void;
74
- private getCookie;
75
- private setCookie;
76
- private removeCookie;
77
- private usesLocalStorage;
78
- private usesSessionStorage;
79
- /**
80
- * Check if sessionStorage is available
81
- */
107
+ /** Check if sessionStorage is available */
82
108
  canUseSessionStorage(): boolean;
83
- /**
84
- * Get direct access to sessionStorage (for window_id which always uses sessionStorage)
85
- */
109
+ /** Get sessionStorage instance (for window_id which always uses sessionStorage) */
86
110
  getSessionStorage(): Storage | null;
87
- /**
88
- * Update storage method at runtime
89
- */
111
+ /** Update storage method at runtime */
90
112
  setMethod(method: PersistenceMethod): void;
91
- /**
92
- * Get current storage method
93
- */
113
+ /** Get current storage method */
94
114
  getMethod(): PersistenceMethod;
95
115
  }
96
- /**
97
- * Create a shared storage instance
98
- * Use this for creating storage managers with consistent settings
99
- */
116
+ /** Factory function to create a StorageManager with default settings */
100
117
  export declare function createStorageManager(method: PersistenceMethod, cross_subdomain?: boolean): StorageManager;