useCookie() BUG. Expired cookie still reads previous values instead of default, cookie in memory?
Seems I've discovered a bug. Expired cookies, where maxAge has passed still reads previously saved cookie value.
2 Replies
Hi @Thick Black W. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
You're correct in observing that sometimes, after a cookie expires (e.g., when
After you expect the cookie to have expired or changed, call
In your
This is a known limitation/quirk in Nuxt's cookie handling. Use
maxAge
is set to a few seconds), useCookie
may still return the previous value instead of the default. This behavior has been reported by other users and is related to how Nuxt manages cookie state between server and client, as well as how the browser handles expired cookies.
Key points from the knowledge sources:
- The useCookie
composable returns a Vue ref that is SSR-friendly and works on both client and server. Updating the ref updates the cookie, unless readonly
is set. However, the value is read from the request headers on the server and from document.cookie
on the client. If the cookie has expired in the browser, but the in-memory ref is not refreshed, you may see stale values until a full reload or manual refresh occurs Docs: useCookie - Return Values.
- There is a known issue where the initial value of a cookie may not be written if it has expired or if the default value is being used, leading to inconsistencies between the in-memory ref and the actual browser cookie GitHub PR: write initial cookie value if different from document.cookie.
- To address this, Nuxt introduced the refreshCookie
utility in v3.10, which allows you to manually refresh the value of a cookie ref from the browser's current cookie value. This is useful when you know the cookie value has changed or expired and want to ensure the ref is up to date Docs: refreshCookie.
- Additionally, enabling the experimental cookieStore
option in your nuxt.config.ts
can help automatically refresh useCookie
values when cookies change in the browser (if supported) Docs: cookieStore.
How to fix or work around this:
1. Manually refresh the cookie valueAfter you expect the cookie to have expired or changed, call
refreshCookie('downloads')
to update the ref from the browser's cookie store.
2. Enable automatic cookie refresh (experimental)In your
nuxt.config.ts
, enable the experimental cookieStore
:
This will keep your useCookie
refs in sync with browser cookies if the browser supports the CookieStore API.
Summary:This is a known limitation/quirk in Nuxt's cookie handling. Use
refreshCookie
to manually sync, or enable the experimental cookieStore
for automatic updates. If you still see issues, ensure your cookie options (like sameSite
, path
, etc.) are set correctly, as some users have reported that changing sameSite
from None
to Lax
resolved similar problems GitHub Issue: useCookie not updating value in browser.
If you need more robust or immediate cookie state, consider these workarounds until the behavior is improved in future Nuxt releases.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@1321238874634195004> Kapa.ai is still learning and improving, please let me know how I did by reacting below.