{"record":{"id":"5b5b2c3ea995c7c1","repo":"angular/angular.js","slug":"cookie-possibly-not-set-or-overflowed-because","errorCode":null,"errorMessage":"Cookie '{}' possibly not set or overflowed because it was too large ({} > 4096 bytes)!","messagePattern":"Cookie '(.+?)' possibly not set or overflowed because it was too large \\((.+?) > 4096 bytes\\)!","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/ngCookies/cookieWriter.js","lineNumber":44,"sourceCode":"    }\n    if (angular.isString(expires)) {\n      expires = new Date(expires);\n    }\n\n    var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);\n    str += path ? ';path=' + path : '';\n    str += options.domain ? ';domain=' + options.domain : '';\n    str += expires ? ';expires=' + expires.toUTCString() : '';\n    str += options.secure ? ';secure' : '';\n    str += options.samesite ? ';samesite=' + options.samesite : '';\n\n    // per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:\n    // - 300 cookies\n    // - 20 cookies per unique domain\n    // - 4096 bytes per cookie\n    var cookieLength = str.length + 1;\n    if (cookieLength > 4096) {\n      $log.warn('Cookie \\'' + name +\n        '\\' possibly not set or overflowed because it was too large (' +\n        cookieLength + ' > 4096 bytes)!');\n    }\n\n    return str;\n  }\n\n  return function(name, value, options) {\n    rawDocument.cookie = buildCookieString(name, value, options);\n  };\n}\n\n$$CookieWriter.$inject = ['$document', '$log', '$browser'];\n\nangular.module('ngCookies').provider('$$cookieWriter', /** @this */ function $$CookieWriterProvider() {\n  this.$get = $$CookieWriter;\n});\n","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/angular/angular.js/blob/d8f77817eb5c98dec5317bc3756d1ea1812bcfbe/src/ngCookies/cookieWriter.js#L26-L62","documentation":"ngCookies' cookieWriter builds the full Set-Cookie string (name=value plus escaped value, path, domain, expiry, secure, samesite) and checks its length against 4096 bytes — the RFC 2109 minimum every browser must support. It still assigns the cookie, but warns via $log.warn that the browser may silently drop or overflow it, so the cookie 'possibly' did not stick. This is a warning, not a thrown exception.","triggerScenarios":"$cookies.put(key, bigString) or $cookies.putObject(key, largeObject) whose serialized, URI-escaped form plus cookie attributes exceeds 4096 bytes; large values combined with long domain/path/expiry/samesite options eating into the budget (the check is str.length + 1).","commonSituations":"Storing JSON session state (user profile, preferences, shopping cart) directly in a cookie; putting a long JWT plus its attributes into one cookie; encoding non-Latin text whose escape sequences triple the size; cookies that work in one browser but vanish in another because implementations cap at exactly 4096.","solutions":["Store only a compact session/id key in the cookie and keep the payload server-side or in localStorage/sessionStorage.","Shrink the payload: drop redundant fields, shorten keys, or compress before putObject.","Trim cookie attributes (omit path/domain/samesite when the defaults suffice) to reclaim bytes — but treat the value itself as the main budget."],"exampleFix":"// before\n$cookies.putObject('profile', user); // serialized JSON > 4096 bytes -> warn\n\n// after\nvar sessionId = Session.create(user); // server stores the profile\n$cookies.put('sid', sessionId);        // small opaque key instead","handlingStrategy":"validation","validationCode":"// Check the encoded size before writing the cookie\nfunction cookieSizeOk(name, value) {\n  return encodeURIComponent(name).length +\n         encodeURIComponent(value).length + 1 <= 4096;\n}\nif (cookieSizeOk('profile', json)) {\n  $cookies.put('profile', json);\n} else {\n  $cookies.put('profileId', id); // small key; payload lives elsewhere\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep cookies small and opaque: store an ID, keep data server-side or in web storage.","Budget ~4KB total per cookie including name, escaping, and attributes (path/domain/samesite).","Remember non-ASCII values grow ~3x under percent-encoding; measure the encoded length."],"tags":["ngcookies","cookies","size-limit","browser-limits","warning"],"backgroundTag":"cookie-size-limit-exceeded","analyzedSha":"d8f77817eb5c98dec5317bc3756d1ea1812bcfbe","analyzedAt":"2026-08-21T20:36:31.651Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}