unicorn/no-document-cookie 正確性
✅ 此規則預設為開啟。
作用
禁止直接使用 document.cookie
。
為何這是不好的?
不建議直接使用 document.cookie
,因為很容易弄錯字串。您應該改用 Cookie Store API 或 cookie 函式庫。
範例
此規則的不正確程式碼範例
javascript
document.cookie =
"foo=bar" + "; Path=/" + "; Domain=example.com" + "; expires=Fri, 31 Dec 9999 23:59:59 GMT" + "; Secure";
此規則的正確程式碼範例
javascript
async function storeCookies() {
await cookieStore.set({
name: "foo",
value: "bar",
expires: Date.now() + 24 * 60 * 60 * 1000,
domain: "example.com",
});
}