Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Session

Unauthenticated user sessions that maintain consistency pre and post-login.

Session contexts use these attributes:

AttributeTypeSourceExamplePrivateNotes
keyStringLocal/Session Storage7D97F0D3-B18C-4305-B110-0317BDB745DCFALSERandom UUID stored in session-bound storage
anonymousBooleanStatic: truetrueFALSEAlways true for session contexts
startedAtNumberCurrent time at session creation1640995200000FALSEUnix timestamp in milliseconds

Implementation example

Implement a session context like this:

const sessionContext = {
  kind: "session",
  key: getOrCreateSessionId(),
  anonymous: true,
  startedAt: Date.now()
};

function getOrCreateSessionId() {
  let sessionId = sessionStorage.getItem('ld-session-id');
  if (!sessionId) {
    sessionId = crypto.randomUUID();
    sessionStorage.setItem('ld-session-id', sessionId);
  }
  return sessionId;
}

Use cases

Progressive release for unauthenticated users

Use this when you roll out features to users before they log in. This ensures anonymous visitors see new features without authentication.

Maintain consistency across authentication

Use this when features should remain consistent before and after login. For example, a user sees a new checkout flow while browsing anonymously. They continue to see it after logging in during the same session.

Kill switch for resource-intensive features

Use this when you disable expensive features for unauthenticated users. For example, disable AI-powered recommendations or real-time chat for anonymous sessions. This reduces infrastructure costs.

Experimentation for pre-authentication paths

Use this when you run experiments on registration flows, shopping cart behavior, or add-to-cart functionality. Rolling out by session ensures consistent experience throughout the visitor’s journey.