All files / hooks/use-cookie/src useCookie.ts

93.54% Statements 58/62
90.47% Branches 19/21
87.5% Functions 7/8
93.33% Lines 56/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283                                                                                                                                    40x                                                                                                                                                                     45x     45x 45x 45x 45x   45x 45x 45x 45x     45x 45x       45x         45x     45x   31x           45x 148x       148x 148x   148x 113x       35x 11x   24x     32x 32x   3x 3x 3x 3x         45x       45x             45x   11x 11x     10x 10x 10x 10x     10x             10x   11x 11x             11x   11x   1x             45x 5x 5x   4x   4x 4x   4x   1x       45x    
import { useCallback, useRef, useSyncExternalStore } from "react";
import { subscribe, notifyListeners } from "./store";
import {
  buildCookieString,
  buildRemovalCookieString,
  defaultDeserializer,
  defaultSerializer,
  isDocumentAvailable,
  readRawCookie,
  type CookieAttributes,
} from "./utils";
 
export type { CookieAttributes, SameSite } from "./utils";
 
/**
 * Type for the initial value that can be a value or a function returning a value
 * (lazy initialization).
 */
export type InitialValue<T> = T | (() => T);
 
/**
 * Options for the {@link useCookie} hook. Combines serialization/lifecycle
 * options (aligned with `@usefy/use-local-storage`) with the cookie-specific
 * write attributes from {@link CookieAttributes}.
 */
export interface UseCookieOptions<T> extends CookieAttributes {
  /**
   * Initial value used when the cookie is absent (or on the server). Supports a
   * lazy initializer function. This is also the value the state resets to when
   * {@link UseCookieReturn} `remove()` is called.
   */
  initialValue?: InitialValue<T>;
  /**
   * Custom serializer for converting the value to the stored cookie string.
   * @default JSON.stringify
   */
  serializer?: (value: T) => string;
  /**
   * Custom deserializer for parsing the stored cookie string back to a value.
   * The default tries `JSON.parse` and **falls back to the raw string** for
   * non-JSON cookies (so a plain `foo=bar` cookie never throws).
   * @default (value) => { try { return JSON.parse(value) } catch { return value } }
   */
  deserializer?: (value: string) => T;
  /** Callback invoked when a serialization/deserialization error occurs. */
  onError?: (error: Error) => void;
}
 
/**
 * Return type for {@link useCookie} — a tuple similar to `useState`.
 */
export type UseCookieReturn<T> = readonly [
  /** Current cookie value (`undefined` when the cookie is absent and no `initialValue` was given). */
  T | undefined,
  /** Update the cookie value. Accepts a value or a functional updater `(prev) => next`. */
  React.Dispatch<React.SetStateAction<T | undefined>>,
  /** Delete the cookie and reset state to `initialValue` (or `undefined`). */
  () => void
];
 
/**
 * Resolve an initial value, supporting a lazy initializer function.
 */
function resolveInitialValue<T>(
  initialValue: InitialValue<T> | undefined
): T | undefined {
  return typeof initialValue === "function"
    ? (initialValue as () => T)()
    : initialValue;
}
 
/**
 * A hook for reading and writing a browser **cookie** as React state — the
 * cookie sibling of `@usefy/use-local-storage` and `@usefy/use-session-storage`.
 * Works like `useState` but the value is persisted in `document.cookie`.
 *
 * Features:
 * - **`useState`-like tuple**: `[value, setValue, remove]`.
 * - **Same-document sync**: multiple `useCookie(key)` instances stay in sync.
 * - **SSR-safe**: guards all `document` access and returns `initialValue` on the
 *   server (uses `useSyncExternalStore`, identical to its storage siblings, to
 *   avoid hydration mismatches).
 * - **Cookie attributes**: `expires`, `maxAge`, `path`, `domain`, `secure`,
 *   `sameSite`.
 * - **Safe (de)serialization**: JSON by default, with a graceful fallback to the
 *   raw string for plain (non-JSON) cookies.
 *
 * > **Cross-tab note:** cookies have no `storage` event, so writes in *other*
 * > tabs are not observed without polling. Same-document instances always sync.
 *
 * @template T - The type of the stored value
 * @param key - The cookie name
 * @param options - Serialization, initial value, and cookie write attributes
 * @returns A tuple of `[value, setValue, remove]`
 *
 * @example
 * ```tsx
 * // Basic usage — a string cookie
 * function ThemeToggle() {
 *   const [theme, setTheme, removeTheme] = useCookie("theme", {
 *     initialValue: "light",
 *   });
 *
 *   return (
 *     <div>
 *       <p>Current theme: {theme}</p>
 *       <button onClick={() => setTheme("dark")}>Dark</button>
 *       <button onClick={removeTheme}>Reset</button>
 *     </div>
 *   );
 * }
 * ```
 *
 * @example
 * ```tsx
 * // With cookie attributes — persist for a week over HTTPS only
 * const [token, setToken] = useCookie<string>("token", {
 *   expires: 7, // days (or a Date)
 *   path: "/",
 *   secure: true,
 *   sameSite: "strict",
 * });
 * ```
 *
 * @example
 * ```tsx
 * // Object value with a functional update (JSON serialized)
 * interface Prefs { lang: string; compact: boolean }
 * const [prefs, setPrefs] = useCookie<Prefs>("prefs", {
 *   initialValue: { lang: "en", compact: false },
 * });
 * setPrefs((prev) => ({ ...prev!, compact: true }));
 * ```
 */
export function useCookie<T = string>(
  key: string,
  options: UseCookieOptions<T> = {}
): UseCookieReturn<T> {
  const {
    initialValue,
    serializer = defaultSerializer,
    deserializer = defaultDeserializer,
    onError,
    expires,
    maxAge,
    path,
    domain,
    secure,
    sameSite,
  } = options;
 
  // Store options in refs for stable references and access to the latest values.
  const serializerRef = useRef(serializer);
  const deserializerRef = useRef(deserializer);
  const onErrorRef = useRef(onError);
  const initialValueRef = useRef(initialValue);
 
  serializerRef.current = serializer;
  deserializerRef.current = deserializer;
  onErrorRef.current = onError;
  initialValueRef.current = initialValue;
 
  // Latest cookie write attributes, so setValue/remove stay stable across renders.
  const attributesRef = useRef<CookieAttributes>({});
  attributesRef.current = { expires, maxAge, path, domain, secure, sameSite };
 
  // Cache for getSnapshot so it returns a stable reference when the raw cookie
  // value is unchanged — required by useSyncExternalStore to avoid loops.
  const cacheRef = useRef<{
    rawValue: string | undefined;
    parsedValue: T | undefined;
  } | null>(null);
 
  const isClient = isDocumentAvailable();
 
  // Subscribe to same-document changes via the internal store.
  const subscribeToStore = useCallback(
    (onStoreChange: () => void) => {
      return subscribe(key, onStoreChange);
    },
    [key]
  );
 
  // getSnapshot: read the current value from document.cookie with caching.
  const getSnapshot = useCallback((): T | undefined => {
    Iif (!isClient) {
      return resolveInitialValue(initialValueRef.current);
    }
 
    try {
      const rawValue = readRawCookie(key);
 
      if (cacheRef.current && cacheRef.current.rawValue === rawValue) {
        return cacheRef.current.parsedValue;
      }
 
      let parsedValue: T | undefined;
      if (rawValue !== undefined) {
        parsedValue = deserializerRef.current(rawValue);
      } else {
        parsedValue = resolveInitialValue(initialValueRef.current);
      }
 
      cacheRef.current = { rawValue, parsedValue };
      return parsedValue;
    } catch (error) {
      onErrorRef.current?.(error as Error);
      const fallbackValue = resolveInitialValue(initialValueRef.current);
      cacheRef.current = { rawValue: undefined, parsedValue: fallbackValue };
      return fallbackValue;
    }
  }, [key, isClient]);
 
  // getServerSnapshot: return the initial value for SSR.
  const getServerSnapshot = useCallback((): T | undefined => {
    return resolveInitialValue(initialValueRef.current);
  }, []);
 
  const storedValue = useSyncExternalStore(
    subscribeToStore,
    getSnapshot,
    getServerSnapshot
  );
 
  // setValue — stable reference that writes the cookie and notifies listeners.
  const setValue = useCallback<React.Dispatch<React.SetStateAction<T | undefined>>>(
    (value) => {
      try {
        if (!isDocumentAvailable()) return;
 
        // Resolve the current value for functional updates.
        const currentValue = (() => {
          try {
            const raw = readRawCookie(key);
            Iif (raw !== undefined) {
              return deserializerRef.current(raw);
            }
            return resolveInitialValue(initialValueRef.current);
          } catch {
            return resolveInitialValue(initialValueRef.current);
          }
        })();
 
        const valueToStore =
          value instanceof Function ? value(currentValue) : value;
 
        const serialized = serializerRef.current(valueToStore as T);
        document.cookie = buildCookieString(
          key,
          serialized,
          attributesRef.current
        );
 
        // Invalidate cache so the next getSnapshot reads the fresh value.
        cacheRef.current = { rawValue: serialized, parsedValue: valueToStore };
 
        notifyListeners(key);
      } catch (error) {
        onErrorRef.current?.(error as Error);
      }
    },
    [key]
  );
 
  // remove — stable reference that deletes the cookie and resets to initial.
  const removeValue = useCallback(() => {
    try {
      if (!isDocumentAvailable()) return;
 
      document.cookie = buildRemovalCookieString(key, attributesRef.current);
 
      const initialVal = resolveInitialValue(initialValueRef.current);
      cacheRef.current = { rawValue: undefined, parsedValue: initialVal };
 
      notifyListeners(key);
    } catch (error) {
      onErrorRef.current?.(error as Error);
    }
  }, [key]);
 
  return [storedValue, setValue, removeValue] as const;
}