Cookie MaxAge vs Expires 속성 차이

프론트에서 Cookie에 accessToken을 저장하여 회원 로그인 여부를 체크하고 있었는데 간혹 가다 expires에 적힌 date보다 먼저 쿠키가 만료되어 로그아웃이 되어버리는 이슈가 발생하였다.

해당 쿠키 말고.....

쿠키를 사용할 때 기존에는 expires를 주로 사용하였는데 찾다보니 HTTP 1.1 부터 expires는 deprecated 되었고, max-age 속성이 expires를 대체하였다고 되어있는데, 과거 IE가 주로 사용되던 시절에 IE가 max-age를 지원하지 않아서 expires가 계속 사용되었던 거 같다.

 

MaxAge

  • Cookie가 만료되기 까지의 초를 나타낸다.
  • 브라우저 창이 닫았다가 다시 해당 페이지를 열어도 로그인 상태 유지

Expires

  • Cookie가 만료되기까지의 날짜를 클라이언트 시간대 기준으로 설정한다.
  • Cookie가 설정된 상태에서 클라이언트의 시간대가 변경되면 예정된 시간보다 쿠키가 먼저 만료될 수 있다.
  • 브라우저에서 세션 쿠키처럼 관리되어서 브라우저 창을 닫으면 해당 값이 삭제되어 로그인 상태가 풀리게 됨 

 

Cookie에는 max-age, expires를 둘 다 지정할 수 있다. 두 속성이 모두 지정되어있을 시 우선 순위는 max-age가 가지고 있다.

 

 

Cookie 지원 같은 경우, Chrome 브라우저는 2022년 8월 릴리즈(M104) 버전부터는 Cookie의 expire 관련 속성이 400일 넘게 설정하는 것이 불가능해진다. 아마 Chromium 엔진을 쓰고 있는 브라우저들은 해당 사항이 동일하게 반영될 것 같다. https://developer.chrome.com/blog/cookie-max-age-expires/

 

Cookie Expires and Max-Age attributes now have upper limit - Chrome Developers

As of Chrome release M104 (August 2022) cookies can no longer set an expiration date more than 400 days in the future.

developer.chrome.com

 

출처)

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie

 

Set-Cookie - HTTP | MDN

The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.

developer.mozilla.org

https://mrcoles.com/blog/cookies-max-age-vs-expires/

 

HTTP Cookies: What's the difference between Max-age and Expires? - Peter Coles

Quick Answer: Expires sets an expiry date for when a cookie gets deleted Max-age sets the time in seconds for when a cookie will be deleted (use this, it’s no longer 2009) Internet Explorer (ie6, ie7, and ie8) does not support “max-age”, while (mostl

mrcoles.com