{"id":11954,"date":"2025-11-28T16:16:03","date_gmt":"2025-11-28T08:16:03","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11954"},"modified":"2025-12-04T11:23:39","modified_gmt":"2025-12-04T03:23:39","slug":"js-cookiestore-cookie","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2025\/11\/js-cookiestore-cookie\/","title":{"rendered":"\u9192\u9192\uff0c\u8be5\u4f7f\u7528CookieStore\u65b0\u5efa\u548c\u7ba1\u7406cookie\u4e86"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11954\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11954<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0cAI\u6293\u53d6\u4fdd\u7559\u539f\u6587\u5730\u5740\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<h3>\u4e00\u3001\u4e3a\u4ec0\u4e48\u8981\u629b\u5f03\u4f20\u7edfCookie\u64cd\u4f5c\uff1f<\/h3>\n<p>\u5728\u524d\u7aef\u5f00\u53d1\u7684\u957f\u6cb3\u4e2d\uff0cCookie\u59cb\u7ec8\u626e\u6f14\u7740\u91cd\u8981\u89d2\u8272\u3002<\/p>\n<p>\u4ece\u7528\u6237\u8eab\u4efd\u8bc6\u522b\u5230\u72b6\u6001\u7ef4\u6301\uff0c\u5b83\u662f\u6d4f\u89c8\u5668\u4e0e\u670d\u52a1\u5668\u4e4b\u95f4\u8f7b\u91cf\u901a\u4fe1\u7684\u6838\u5fc3\u8f7d\u4f53\u3002<\/p>\n<p>\u4f46\u957f\u671f\u4ee5\u6765\uff0c\u6211\u4eec\u64cd\u4f5cCookie\u7684\u65b9\u5f0f\u59cb\u7ec8\u505c\u7559\u5728\u901a\u8fc7<code>document.cookie<\/code>\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u201c\u539f\u59cb\u9636\u6bb5\u201d\uff0c\u4e0d\u4ec5\u4ee3\u7801\u5197\u4f59\u6613\u51fa\u9519\uff0c\u8fd8\u65e0\u6cd5\u5e94\u5bf9\u5f02\u6b65\u573a\u666f\u4e0b\u7684\u590d\u6742\u9700\u6c42\u3002<\/p>\n<p>\u6bd4\u65b9\u8bf4\uff0c\u6211\u4eec\u8981\u8bbe\u7f6e\u4e00\u4e2aCookie\uff0c\u9700\u8981\u5148\u83b7\u53d6\uff0c\u7136\u540e\u518d\u624b\u52a8\u8fdb\u884c\u952e\u503c\u5bf9+\u5c5e\u6027\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>document.cookie = \"name=zhangxinxu; max-age=3600; path=\/\"<\/pre>\n<p>\u8fd9\u5c31\u5f88\u9ebb\u70e6\uff0c\u5c5e\u6027\u987a\u5e8f\u3001\u7b26\u53f7\u683c\u5f0f\u7a0d\u6709\u504f\u5dee\u5c31\u4f1a\u5bfc\u81f4\u5931\u6548\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202511\/2025-11-28_155913.jpeg\" width=\"150\" height=\"149\" alt=\"\u5b8b\u7389\u644a\u624b\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u8981\u83b7\u53d6Cookie\u7684\u65f6\u5019\uff0c\u4e5f\u662f\u5148\u83b7\u53d6\uff0c\u518d\u5b57\u7b26\u4e32split\u5206\u9694\uff0c\u518d\u53bb\u5339\u914d\uff0c\u4ee3\u7801\u5197\u957f\uff0c\u8fd8\u4e0d\u652f\u6301\u6279\u91cf\u64cd\u4f5c\u3002<\/p>\n<p>\u6bd4\u65b9\u8bf4\uff0c\u6211\u60f3\u4e00\u6b21\u6027\u5220\u9664\u591a\u4e2a\u7b26\u5408\u6761\u4ef6\u7684Cookie\uff0c\u9700\u5faa\u73af\u904d\u5386\u9010\u4e2a\u5904\u7406\u3002<\/p>\n<p>\u6b63\u662f\u56e0\u4e3a\u4e0a\u9762\u8fd9\u4e9b\u4e0d\u8db3\uff0cCookieStore API\u624d\u5e94\u8fd0\u800c\u751f\u3002<\/p>\n<p>\u5b83\u5c06Cookie\u64cd\u4f5c\u5c01\u88c5\u4e3a\u6807\u51c6\u5316\u7684\u5f02\u6b65\u65b9\u6cd5\uff0c\u8ba9Cookie\u7ba1\u7406\u53d8\u5f97\u7b80\u6d01\u3001\u53ef\u63a7\u3002<\/p>\n<h3>\u4e8c\u3001CookieStore\u5bf9\u8c61\u7279\u6027\u6982\u89c8<\/h3>\n<p>CookieStore\u662f\u6d4f\u89c8\u5668\u63d0\u4f9b\u7684\u73b0\u4ee3API\uff0c\u96b6\u5c5e\u4e8eWindow\u5bf9\u8c61\uff0c\u7528\u4e8e\u4ee5\u5f02\u6b65\u3001\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u64cd\u4f5cCookie\u3002<\/p>\n<p>\u5305\u542b\u7684\u65b9\u6cd5\u540d\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\/\/ \u5220\u9664cookie\r\ncookieStore.delete()\r\n\r\n\/\/ \u83b7\u53d6cookie\r\ncookieStore.get()\r\n\r\n\/\/ \u83b7\u53d6\u6240\u6709cookie\r\ncookieStore.getAll()\r\n\r\n\/\/ \u8bbe\u7f6ecookie\r\ncookieStore.set()<\/pre>\n<p>\u4ee5\u4e0a\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u662f\u57fa\u4e8ePromise\u8bbe\u8ba1\uff0c\u652f\u6301\u5bf9\u6240\u6709Cookie\u5c5e\u6027\u8fdb\u884c\u7cbe\u51c6\u63a7\u5236\uff0c\u540c\u65f6\u63d0\u4f9b\u4e86\u76d1\u542cCookie\u53d8\u5316\u7684\u80fd\u529b\uff0c\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>\/\/ cookieStore\u4e5f\u662f\u5168\u5c40\u7684\r\ncookieStore.addEventListener(\"change\", (event) => {\r\n  console.log(\"cookie\u6539\u53d8\");\r\n});<\/pre>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>CookieStore\u5df2\u88ab\u4e3b\u6d41\u6d4f\u89c8\u5668\u5168\u9762\u652f\u6301\uff0c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202511\/2025-11-28_145658.png\" width=\"653\" height=\"242\" alt=\"CookieStore\u7684\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5bf9\u4e8e\u9700\u8981\u517c\u5bb9\u4f4e\u7248\u672c\u6d4f\u89c8\u5668\u7684\u573a\u666f\uff0c\u53ef\u901a\u8fc7<code>@ungap\/cookie-store<\/code>\u7b49polyfill\u5e93\u8865\u5168\u529f\u80fd\uff0c\u65e0\u9700\u62c5\u5fc3\u6280\u672f\u843d\u5730\u95ee\u9898\u3002<\/p>\n<pre>if (!'CookieStore' in window) {\r\n  \/\/ \u4f7f\u7528\u964d\u7ea7\u65b9\u6848\r\n}<\/pre>\n<h3>\u4e09\u3001CookieStore\u5e38\u89c1\u4f7f\u7528\u793a\u610f<\/h3>\n<p>CookieStore\u7684API\u8bbe\u8ba1\u6781\u4e3a\u7b80\u6d01\uff0c\u6838\u5fc3\u56f4\u7ed5\u201c\u589e\u3001\u5220\u3001\u6539\u3001\u67e5\u3001\u76d1\u542c\u201d\u4e94\u5927\u573a\u666f\uff0c\u6240\u6709\u65b9\u6cd5\u5747\u8fd4\u56dePromise\uff0c\u652f\u6301<code>async\/await<\/code>\u8bed\u6cd5\u3002<\/p>\n<h4>1. \u65b0\u5efa\/\u4fee\u6539Cookie\uff1aset()\u65b9\u6cd5<\/h4>\n<p><code>set()<\/code>\u65b9\u6cd5\u662fCookieStore\u7684\u6838\u5fc3\uff0c\u65e2\u53ef\u4ee5\u65b0\u5efaCookie\uff0c\u4e5f\u53ef\u4ee5\u4fee\u6539\u5df2\u6709Cookie\uff08\u901a\u8fc7\u952e\u540d\u5339\u914d\uff09\u3002<\/p>\n<p>\u5b83\u652f\u6301\u4e24\u79cd\u53c2\u6570\u683c\u5f0f\uff1a\u952e\u503c\u5bf9+\u914d\u7f6e\u5bf9\u8c61\uff0c\u6216\u5305\u542b\u5b8c\u6574\u4fe1\u606f\u7684\u5355\u4e2a\u5bf9\u8c61\u3002<\/p>\n<pre>\r\n\/\/ \u65b9\u5f0f1\uff1a\u952e\u540d\u3001\u952e\u503c\u4e0e\u914d\u7f6e\u5206\u79bb\r\nasync function setUserCookie() {\r\n  await cookieStore.set(\r\n    'username', \/\/ \u952e\u540d\r\n    'zhangxinxu', \/\/ \u952e\u503c\r\n    {\r\n      maxAge: 3600, \/\/ \u5b58\u6d3b\u65f6\u95f4\uff08\u79d2\uff09\uff0c\u66ff\u4ee3\u4f20\u7edfexpires\r\n      path: '\/', \/\/ \u4f5c\u7528\u8def\u5f84\r\n      domain: 'zhangxinxu.com', \/\/ \u4f5c\u7528\u57df\r\n      secure: true, \/\/ \u4ec5HTTPS\u4e0b\u751f\u6548\r\n      sameSite: 'strict' \/\/ \u9632\u6b62CSRF\u653b\u51fb\uff0c\u53ef\u9009strict\/lax\/none\r\n    }\r\n  );\r\n  console.log('Cookie\u8bbe\u7f6e\u6210\u529f');\r\n}<\/pre>\n<pre>\/\/ \u65b9\u5f0f2\uff1a\u5355\u4e2a\u5bf9\u8c61\u53c2\u6570\uff08\u66f4\u63a8\u8350\uff0c\u7ed3\u6784\u6e05\u6670\uff09\r\nasync function setTokenCookie() {\r\n  await cookieStore.set({\r\n    name: 'token',\r\n    value: '\u6b22\u8fce\u8d2d\u4e70\u6211\u7684\u65b0\u4e66\uff1ahtml\u5e76\u4e0d\u7b80\u5355...',\r\n    expires: new Date(Date.now() + 24 * 60 * 60 * 1000), \/\/ \u8fc7\u671f\u65f6\u95f4\uff08Date\u5bf9\u8c61\uff09\r\n    secure: true,\r\n    sameSite: 'lax'\r\n  });\r\n}<\/pre>\n<p>\u6ce8\u610f\uff1a\u5f53\u8bbe\u7f6e\u7684Cookie\u952e\u540d\u5df2\u5b58\u5728\u65f6\uff0c<code>set()<\/code>\u65b9\u6cd5\u4f1a\u81ea\u52a8\u8986\u76d6\u539f\u6709Cookie\uff0c\u65e0\u9700\u624b\u52a8\u5220\u9664\uff1b<code>maxAge<\/code>\u4e0e<code>expires<\/code>\u4e8c\u9009\u4e00\u5373\u53ef\uff0c<code>maxAge<\/code>\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff0c\u66f4\u7b26\u5408\u524d\u7aef\u5f00\u53d1\u4e60\u60ef\u3002<\/p>\n<h4>2. \u8bfb\u53d6Cookie\uff1aget()\u4e0egetAll()\u65b9\u6cd5<\/h4>\n<p>\u8bfb\u53d6Cookie\u65f6\uff0c<code>get()<\/code>\u7528\u4e8e\u83b7\u53d6\u6307\u5b9a\u952e\u540d\u7684\u5355\u4e2aCookie\uff0c<code>getAll()<\/code>\u7528\u4e8e\u83b7\u53d6\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684Cookie\uff08\u652f\u6301\u6309path\u3001domain\u8fc7\u6ee4\uff09\uff0c\u5f7b\u5e95\u544a\u522b\u5b57\u7b26\u4e32\u62c6\u5206\u7684\u9ebb\u70e6\u3002<\/p>\n<pre>\r\n\/\/ \u8bfb\u53d6\u5355\u4e2aCookie\r\nasync function getUserCookie() {\r\n  const cookie = await cookieStore.get('username');\r\n  if (cookie) {\r\n    console.log('\u7528\u6237\u540d\uff1a', cookie.value);\r\n    console.log('\u8fc7\u671f\u65f6\u95f4\uff1a', cookie.expires);\r\n  } else {\r\n    console.log('Cookie\u4e0d\u5b58\u5728');\r\n  }\r\n}\r\n\r\n\/\/ \u8bfb\u53d6\u6240\u6709Cookie\r\nasync function getAllCookies() {\r\n  \/\/ \u83b7\u53d6\u6240\u6709Cookie\r\n  const allCookies = await cookieStore.getAll();\r\n  console.log('\u6240\u6709Cookie\uff1a', allCookies);\r\n\r\n  \/\/ \u6309\u8def\u5f84\u8fc7\u6ee4Cookie\r\n  const rootCookies = await cookieStore.getAll({ path: '\/' });\r\n  console.log('\u6839\u8def\u5f84Cookie\uff1a', rootCookies);\r\n}<\/pre>\n<p>\u8fd4\u56de\u7684Cookie\u5bf9\u8c61\u5305\u542b<code>name<\/code>\u3001<code>value<\/code>\u3001<code>expires<\/code>\u3001<code>path<\/code>\u3001<code>domain<\/code>\u7b49\u5b8c\u6574\u5c5e\u6027\uff0c\u53ef\u76f4\u63a5\u7528\u4e8e\u4e1a\u52a1\u903b\u8f91\u5904\u7406\uff0c\u65e0\u9700\u4e8c\u6b21\u89e3\u6790\u3002<\/p>\n<h4>3. \u5220\u9664Cookie\uff1adelete()\u65b9\u6cd5<\/h4>\n<p>\u5220\u9664Cookie\u65f6\uff0c\u53ea\u9700\u6307\u5b9aCookie\u7684\u952e\u540d\u53ca\u5bf9\u5e94\u7684<code>path<\/code>\u3001<code>domain<\/code>\uff08\u9700\u4e0e\u8bbe\u7f6e\u65f6\u4e00\u81f4\uff0c\u5426\u5219\u65e0\u6cd5\u5339\u914d\uff09\uff0c\u64cd\u4f5c\u7b80\u6d01\u4e14\u4e0d\u6613\u51fa\u9519\u3002<\/p>\n<pre>async function deleteUserCookie() {\r\n  await cookieStore.delete('username', {\r\n    path: '\/',\r\n    domain: 'example.com' \/\/ \u82e5\u8bbe\u7f6e\u65f6\u6307\u5b9a\u4e86domain\uff0c\u5220\u9664\u65f6\u5fc5\u987b\u4e00\u81f4\r\n  });\r\n  console.log('Cookie\u5220\u9664\u6210\u529f');\r\n}<\/pre>\n<p>\u6ce8\u610f\uff1a\u82e5\u5220\u9664\u65f6\u672a\u6307\u5b9a<code>path<\/code>\u6216<code>domain<\/code>\uff0c\u9ed8\u8ba4\u5339\u914d\u5f53\u524d\u9875\u9762\u7684<code>path<\/code>\u548c<code>domain<\/code>\uff0c\u82e5\u4e0e\u8bbe\u7f6e\u65f6\u7684\u5c5e\u6027\u4e0d\u5339\u914d\uff0c\u4f1a\u5bfc\u81f4\u5220\u9664\u5931\u8d25\uff0c\u8fd9\u4e00\u70b9\u4e0e\u4f20\u7edfCookie\u64cd\u4f5c\u89c4\u5219\u4e00\u81f4\u3002<\/p>\n<h4>4. \u76d1\u542cCookie\u53d8\u5316\uff1aaddEventListener()<\/h4>\n<p>CookieStore\u6700\u5f3a\u5927\u7684\u7279\u6027\u4e4b\u4e00\u662f\u652f\u6301\u76d1\u542cCookie\u7684\u53d8\u5316\uff08\u65b0\u589e\u3001\u4fee\u6539\u3001\u5220\u9664\uff09\uff0c\u8fd9\u5728\u9700\u8981\u5b9e\u65f6\u54cd\u5e94Cookie\u72b6\u6001\u7684\u573a\u666f\uff08\u5982\u767b\u5f55\u72b6\u6001\u540c\u6b65\uff09\u4e2d\u6781\u4e3a\u5b9e\u7528\u3002<\/p>\n<pre>\/\/ \u76d1\u542cCookie\u53d8\u5316\r\nfunction listenCookieChange() {\r\n  cookieStore.addEventListener('change', (event) => {\r\n    console.log('Cookie\u53d8\u5316\u7c7b\u578b\uff1a', event.changed.length ? '\u65b0\u589e\/\u4fee\u6539' : '\u5220\u9664');\r\n    \/\/ \u53d8\u5316\u7684Cookie\u5217\u8868\r\n    event.changed.forEach(cookie => {\r\n      console.log('\u4fee\u6539\u7684Cookie\uff1a', cookie.name, '->', cookie.value);\r\n    });\r\n    \/\/ \u5220\u9664\u7684Cookie\u5217\u8868\r\n    event.deleted.forEach(cookie => {\r\n      console.log('\u5220\u9664\u7684Cookie\uff1a', cookie.name);\r\n    });\r\n  });\r\n}\r\n\r\n\/\/ \u521d\u59cb\u5316\u76d1\u542c\r\nlistenCookieChange();<\/pre>\n<p>\u5f53\u9875\u9762\u4e2d\u4efb\u4f55Cookie\u53d1\u751f\u53d8\u5316\u65f6\uff0c<code>change<\/code>\u4e8b\u4ef6\u90fd\u4f1a\u88ab\u89e6\u53d1\uff0c\u901a\u8fc7<code>event.changed<\/code>\u548c<code>event.deleted<\/code>\u53ef\u6e05\u6670\u533a\u5206\u53d8\u5316\u7c7b\u578b\uff0c\u5b9e\u73b0\u767b\u5f55\u72b6\u6001\u540c\u6b65\u3001\u6743\u9650\u66f4\u65b0\u7b49\u573a\u666f\u7684\u65e0\u7f1d\u8854\u63a5\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202511\/2025-11-28_155218.jpeg\" width=\"150\" height=\"150\" alt=\"\u5b8b\u7389\u629b\u5a9a\u773c\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u56db\u3001CookieStore\u7684\u6838\u5fc3\u4f18\u52bf\u603b\u7ed3<\/h3>\n<p>\u5bf9\u6bd4\u4f20\u7edf\u7684<code>document.cookie<\/code>\uff0cCookieStore\u7684\u4f18\u52bf\u582a\u79f0\u964d\u7ef4\u6253\u51fb\uff1a<\/p>\n<ol>\n<li>\u5f02\u6b65\u975e\u963b\u585e\uff1a\u57fa\u4e8ePromise\u7684\u5f02\u6b65\u64cd\u4f5c\uff0c\u907f\u514d\u540c\u6b65\u64cd\u4f5c\u963b\u585e\u9875\u9762\u6e32\u67d3\uff0c\u63d0\u5347\u524d\u7aef\u6027\u80fd\u3002<\/li>\n<li>\u8bed\u6cd5\u7b80\u6d01\u76f4\u89c2\uff1a\u544a\u522b\u5b57\u7b26\u4e32\u62fc\u63a5\u4e0e\u62c6\u5206\uff0c\u901a\u8fc7\u65b9\u6cd5\u8c03\u7528\u5b9e\u73b0\u7cbe\u51c6\u64cd\u4f5c\uff0c\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u5927\u5e45\u63d0\u5347\u3002<\/li>\n<li>\u5b8c\u6574\u7684\u9519\u8bef\u5904\u7406\uff1a\u652f\u6301try\/catch\u6355\u83b7\u64cd\u4f5c\u5f02\u5e38\uff0c\u5982\u8bbe\u7f6eCookie\u65f6\u7684\u683c\u5f0f\u9519\u8bef\u3001\u8de8\u57dfCookie\u8bbf\u95ee\u9650\u5236\u7b49\uff0c\u6392\u9519\u66f4\u9ad8\u6548\u3002<\/li>\n<li>\u5f3a\u5927\u7684\u76d1\u542c\u80fd\u529b\uff1a\u5b9e\u65f6\u54cd\u5e94Cookie\u53d8\u5316\uff0c\u65e0\u9700\u8f6e\u8be2\uff0c\u7b80\u5316\u767b\u5f55\u3001\u6743\u9650\u7b49\u573a\u666f\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/li>\n<li>\u6279\u91cf\u64cd\u4f5c\u652f\u6301\uff1a\u901a\u8fc7<code>getAll()<\/code>\u5b9e\u73b0\u6279\u91cf\u8bfb\u53d6\uff0c\u914d\u5408\u8fc7\u6ee4\u6761\u4ef6\u53ef\u7cbe\u51c6\u83b7\u53d6\u76ee\u6807Cookie\u3002<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202511\/2025-11-28_161511.jpeg\" width=\"150\" height=\"150\" alt=\"\u5b8b\u7389\u5664\u58f0\u624b\u52bf\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e94\u3001\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<p>\u5c3d\u7ba1CookieStore\u4f18\u52bf\u663e\u8457\uff0c\u4f46\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u4ecd\u9700\u6ce8\u610f\u8fd9\u4e9b\u7ec6\u8282\uff1a<\/p>\n<ul>\n<li>\u540c\u6e90\u7b56\u7565\u9650\u5236\uff1a\u4e0e\u4f20\u7edfCookie\u4e00\u81f4\uff0cCookieStore\u65e0\u6cd5\u8bbf\u95ee\u8de8\u57dfCookie\uff0c\u4ec5\u80fd\u64cd\u4f5c\u5f53\u524d\u57df\u540d\u4e0b\u7684Cookie\u3002<\/li>\n<li>Secure\u5c5e\u6027\u8981\u6c42\uff1a\u5f53Cookie\u8bbe\u7f6e\u4e3asecure: true\u65f6\uff0c\u4ec5\u80fd\u5728HTTPS\u534f\u8bae\u4e0b\u64cd\u4f5c\uff0c\u672c\u5730\u5f00\u53d1\u65f6\u53ef\u4f7f\u7528localhost\u7ed5\u8fc7\u8be5\u9650\u5236\u3002<\/li>\n<li>SameSite\u5c5e\u6027\u914d\u7f6e\uff1a\u4e3a\u9632\u6b62CSRF\u653b\u51fb\uff0c\u5efa\u8bae\u8bbe\u7f6e<code>sameSite:'strict'<\/code>\u6216<code>'sameSite:'lax'<\/code>\uff0c\u82e5\u9700\u8981\u8de8\u57df\u8bbf\u95eeCookie\uff0c\u53ef\u8bbe\u7f6e\u4e3a<code>sameSite:'none'<\/code>\uff08\u9700\u914d\u5408<code>secure:true<\/code>\uff09\u3002<\/li>\n<li>\u517c\u5bb9\u6027\u5904\u7406\uff1a\u9488\u5bf9\u4e0d\u652f\u6301CookieStore\u7684\u6d4f\u89c8\u5668\uff0c\u53ef\u4f7f\u7528polyfill\u5e93\u515c\u5e95\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre>\/\/ \u5f15\u5165polyfill\uff08\u901a\u8fc7CDN\u6216npm\u5b89\u88c5\uff09\r\n&lt;script src=\"https:\/\/cdn.jsdelivr.net\/npm\/@whatwg-node\/cookie-store@0.2.3\/cjs\/index.min.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>\u6216\u8005\uff1a<\/p>\n<pre>\/\/ \u5c01\u88c5\u517c\u5bb9\u65b9\u6cd5\r\nasync function getCookie(name) {\r\n  if ('CookieStore' in window) {\r\n    return (await cookieStore.get(name))?.value;\r\n  } else {\r\n    \/\/ \u4f20\u7edf\u65b9\u5f0f\u515c\u5e95\r\n    const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`));\r\n    return match ? match[2] : null;\r\n  }\r\n}<\/pre>\n<h3>\u516d\u3001\u7ed3\u8bed<\/h3>\n<p>\u4ece<code>document.cookie<\/code>\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u5230CookieStore\u7684\u5f02\u6b65\u5316\u3001\u6807\u51c6\u5316\u64cd\u4f5c\uff0c\u524d\u7aefCookie\u7ba1\u7406\u7684\u6548\u7387\u5b9e\u73b0\u4e86\u8d28\u7684\u98de\u8dc3\u3002<\/p>\n<p>CookieStore\u4e0d\u4ec5\u89e3\u51b3\u4e86\u4f20\u7edf\u65b9\u5f0f\u7684\u8bf8\u591a\u75db\u70b9\uff0c\u8fd8\u901a\u8fc7\u76d1\u542c\u80fd\u529b\u62d3\u5c55\u4e86Cookie\u7684\u5e94\u7528\u573a\u666f\uff0c\u662f\u73b0\u4ee3\u524d\u7aef\u5f00\u53d1\u4e2d\u503c\u5f97\u4f18\u5148\u91c7\u7528\u7684\u6280\u672f\u65b9\u6848\u3002<\/p>\n<p>\u65e0\u8bba\u662f\u7b80\u5355\u7684\u767b\u5f55\u72b6\u6001\u5b58\u50a8\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6743\u9650\u7ba1\u7406\u573a\u666f\uff0cCookieStore\u90fd\u80fd\u4ee5\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u65b9\u5f0f\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<p>\u73b0\u5728\u5c31\u62ff\u8d77\u8fd9\u9879\u6280\u672f\uff0c\u544a\u522b\u7e41\u7410\u7684Cookie\u64cd\u4f5c\uff0c\u8ba9\u4ee3\u7801\u66f4\u4f18\u96c5\u3001\u66f4\u6613\u7ef4\u62a4\u5427\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202511\/2025-11-24_153722.jpeg\" width=\"150\" height=\"150\" alt=\"\u5b8b\u7389\u98de\u543b\" class=\"alignnone size-medium\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11954\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11954<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202511\/2025-11-28_161832.jpeg\" width=\"160\" height=\"103\" alt=\"cookiestore\u5c01\u9762\u5360\u4f4d\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u8fd8\u5728\u4f7f\u7528document.cookie\u7ba1\u7406cookie\u5417\uff1f\u592a\u8fc7\u65f6\u4e86\uff0c\u73b0\u5728\u6709\u4e13\u95e8\u7684Cookie\u64cd\u4f5cAPI\u4e86\uff0c\u5c31\u662fCookieStore\uff0c\u7b80\u5355\u53c8\u597d\u7528\uff0c\u7b80\u76f4\u5b8c\u7f8e\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1240,255,2090,641,903],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11954"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11954"}],"version-history":[{"count":13,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11954\/revisions"}],"predecessor-version":[{"id":11994,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11954\/revisions\/11994"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}