Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent change listener for cookie removal #457

Open
niksauer opened this issue Feb 5, 2024 · 1 comment
Open

Inconsistent change listener for cookie removal #457

niksauer opened this issue Feb 5, 2024 · 1 comment

Comments

@niksauer
Copy link

niksauer commented Feb 5, 2024

Before upgrading to v7.0.2 (from v6.1.1), I observed the following events when removing a cookie:

console.log
    {
      name: 'testCookie',
      value: undefined,
      options: { path: '/', expires: 1970-01-31T23:00:01.000Z, maxAge: 0 }
    }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)

  console.log
    { name: 'testCookie', value: undefined }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)
          at Set.forEach (<anonymous>)

  console.log
    { name: 'Max-Age', value: undefined }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)
          at Set.forEach (<anonymous>)

  console.log
    { name: 'Path', value: undefined }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)
          at Set.forEach (<anonymous>)

  console.log
    { name: 'Expires', value: undefined }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)
          at Set.forEach (<anonymous>)

which has now changed to:

console.log
      {
        name: 'testCookie',
        value: undefined,
        options: { path: '/', expires: 1970-01-31T23:00:01.000Z, maxAge: 0 }
      }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)

    console.log
      { name: 'testCookie', value: '' }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)
          at Set.forEach (<anonymous>)

    console.log
      { name: 'Max-Age', value: 0 }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)
          at Set.forEach (<anonymous>)

    console.log
      { name: 'Path', value: '/' }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)
          at Set.forEach (<anonymous>)

    console.log
      { name: 'Expires', value: 'Sat, 31 Jan 1970 23:00:01 GMT' }

      at Array.onCookieChange (src/hooks/useCookieState.ts:75:15)
          at Set.forEach (<anonymous>)

The problem being that the new value can be both undefined and "" when a cookie is removed, thus making my React hook think that the value is the empty string instead of undefined. Is this expected? If so, I can always treat the empty string as undefined, although I would prefer the previous consistent bevahior.

Looking at the changes between versions, I believe this commit had an effect on this 99ba53f.

@eXon
Copy link
Collaborator

eXon commented Feb 20, 2024

Hi,

The previous code was unfortunately broken for many other use-cases. The way this library remove cookies is by setting them empty with a past expiration date. However, the browser might still have the cookie until you refresh the page making an empty and deleted cookie impossible to differeciate. I will need to dig more into this on how we can fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants