v145 · Web APIs · maxAge Demo

maxAge Demo

Set cookies using the Cookie Store API with different maxAge values. The API resolves with no return value — use cookieStore.get() to read back the cookie and verify its expires timestamp was computed correctly from the maxAge you provided.

Chrome 145 required for the maxAge option in cookieStore.set(). Older browsers that support Cookie Store API (Chrome 87+) must use the expires option with an absolute Date. Browsers without Cookie Store API will show an error.

set a cookie with maxAge

seconds (0 = delete, omit = session cookie)
Actions and results will appear here…

cookies visible on this page

code

// Chrome 145+: relative maxAge in seconds
await cookieStore.set({
  name: 'session',
  value: 'abc123',
  maxAge: 3600, // 1 hour from now — no Date arithmetic needed
  path: '/',
  sameSite: 'lax',
});

// Read it back
const cookie = await cookieStore.get('session');
console.log(cookie.expires); // Date object — computed from maxAge
console.log(cookie.value);   // 'abc123'

// Listen for changes
cookieStore.addEventListener('change', event => {
  for (const changed of event.changed) {
    console.log('Changed:', changed.name, changed.value);
  }
  for (const deleted of event.deleted) {
    console.log('Deleted:', deleted.name);
  }
});

see also