Skip to content

Add refresh option to useCookie() to extend cookie expiration #33811

@riabinkovihor

Description

@riabinkovihor

Describe the feature

Add a refresh option to the useCookie composable to allow extending the cookie expiration
even when the value remains unchanged. This is useful for cookies that should stay alive
as long as the user is active, without changing the value.

Current behavior

When updating a cookie with the same value, its expiration time does not reset.

Expected behavior

With a new option refresh: true, setting the cookie even to the same value
should update the expiration time according to maxAge.

Example

<script setup lang="ts">
const testCookie = useCookie('test-cookie', {
  maxAge: 60 * 60,
  default: () => 'defaultValue',
  refresh: true,
})

if (import.meta.server) {
  testCookie.value = 'defaultValue' // cookie expiration should be extended
}

const changeCookie = () => {
  testCookie.value = 'defaultValue' // cookie expiration should be extended
}
</script>

<template>
  <div>
    testCookie:  {{ testCookie }}
    <button @click="changeCookie">
      change cookie
    </button>
  </div>
</template>

Additional information

  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?

Final checks

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions