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

Provide a cookie ordering configuration option #828

Closed
mrooneytrimble opened this issue Jun 29, 2023 · 1 comment
Closed

Provide a cookie ordering configuration option #828

mrooneytrimble opened this issue Jun 29, 2023 · 1 comment

Comments

@mrooneytrimble
Copy link

Semi related to this issue:
#226

When a cookie is set on a parent domain and is expected to be overwritten in a sub domain, the parent domains cookie is always returned. This bit us due to some weirdnesses with a CEF web window, and we have a work around but it seems like it could come back to bite us again.

For example

Cookies.set('someName', 'a', {domain: 'example.com'});
Cookies.set('someName', 'b', {domain: 'bonk.example.com'});
// always returns 'a'
Cookies.get('someName');
Cookies.set('someName', 'c', {domain: 'bonk.example.com'});
// always returns 'a'
Cookies.get('someName');

Proposed Solution

Extending the cookie 'get' to allow for passing in a flag to read the last cookie with the given name.

// pseudo code
Cookies.get(name, {last: true});
  function get(name, options) {
    if (typeof document === 'undefined' || (arguments.length && !name)) {
      return
    }

    // To prevent the for loop in the first place assign an empty array
    // in case there are no cookies at all.
    var cookies = document.cookie ? document.cookie.split('; ') : []
    var jar = {}
    var findingLast = options ? options.last : false;
    for (var i = 0; i < cookies.length; i++) {
      var parts = cookies[i].split('=')
      var value = parts.slice(1).join('=')

      try {
        var found = decodeURIComponent(parts[0])
        if (!(found in jar) || findingLast) jar[found] = converter.read(value, found)
        if (name === found && !findingLast) {
          break
        }
      } catch (e) {
        // Do nothing...
      }
    }

    return name ? jar[name] : jar
  }

Alternative Solution

A Cookies.getAll which allows us to retrieve all of the cookies with the given name.
This would allow developers to use the most appropriate cookie (for example by encoding a date or version into the cookie value)

@carhartl
Copy link
Member

Obsolete by #813

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

No branches or pull requests

2 participants