Skip to content

Latest commit

Β 

History

History
48 lines (36 loc) Β· 1.18 KB

File metadata and controls

48 lines (36 loc) Β· 1.18 KB

no-document-cookie

πŸ“ Do not use document.cookie directly.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

It's not recommended to use document.cookie directly as it's easy to get the string wrong. Instead, you should use the Cookie Store API or a cookie library.

Examples

// ❌
document.cookie =
	'foo=bar' +
	'; Path=/' +
	'; Domain=example.com' +
	'; expires=Fri, 31 Dec 9999 23:59:59 GMT' +
	'; Secure';

// βœ…
await cookieStore.set({
	name: 'foo',
	value: 'bar',
	expires: Date.now() + 24 * 60 * 60 * 1000,
	domain: 'example.com'
});
// ❌
document.cookie += '; foo=bar';

// βœ…
await cookieStore.set('foo', 'bar');

// βœ…
import Cookies from 'js-cookie';

Cookies.set('foo', 'bar');
// βœ…
const array = document.cookie.split('; ');