浏览器HTTP缓存规则导致的跨域

浏览器HTTP缓存规则导致的跨域

现象

  • 两个域名
    • localhost:5500(下面简称5500)
    • localhost:5501(下面简称5501)
  • 后台跨域:正确的配置了5500和5501允许跨域访问
  • 当再5500中缓存一个跨域请求后,打开5501后请求相同的请求,结果在5501中出现跨域

⚠️ 在5501中的跨域错误显示的是Access-Control-Allow-Origin: http://localhost:5500 (???? WTF ???)

  • 猜想:浏览器的缓存是基于主域名
ts 复制代码
localhost // 子域名命中: a.localhost,b.localhost...; 不同端口命中: localhost:5500, localhost: 5501...
	- test跨域请求

baidu.com // 不同域名互不干扰
	- ...

源码

  1. page1.html
html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Page1</title>
</head>

<body>
  <h1>this is page1.html</h1>
  <button onclick="window.open('http://localhost:5501/page2.html')">打开page2</button>
</body>
<script type="module">
  const res = await fetch("http://127.0.0.1:3000");
  const json = await res.json();
  const div = document.createElement("div");
  div.innerText = json.message;
  document.body.appendChild(div);
</script>

</html>
  1. page2.html
html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Page2</title>
</head>

<body>
  <h1>this is page2.html</h1>
</body>
<script type="module">
  const res = await fetch("http://127.0.0.1:3000");
  const json = await res.json();
  const div = document.createElement("div");
  div.innerText = json.message;
  document.body.appendChild(div);
</script>

</html>
  1. nodejs后台 (我的环境是node v20+)
ts 复制代码
const { log } = require("console");
const http = require("http");

const server = http.createServer((req, res) => {
  const origin = req.headers.origin;

  log("origin", req.headers.origin);
  res.writeHead(200, {
    "Content-Type": "application/json; charset=utf-8",
    "Access-Control-Allow-Origin": origin,
    "Cache-Control": "private, max-age=60", // 强制缓存60s
  });
  res.end(JSON.stringify({ message: "Hello, World!" }));
});

server.listen(3000, () => {
  console.log("Server running on http://127.0.0.1:3000");
});

验证

  1. localhost:5500是page1.html
  2. localhost:5501是page2.html
  3. 先在5500中访问,一切正常
  1. 点击"打开page2"访问localhost:5501的page2.html,不出意外的话,打开控制台就会出现CORS错误

当强制刷新5501的page2.html后,会正常显示

解决方案

  1. 请求后拼接上随机数(适合应急上线,治标不治本)
ts 复制代码
fetch(`http://127.0.0.1:3000/?random=${Math.random()}`)
  1. 对于静态资源加上缓存完全没问题,如果是业务请求或者是业务相关的资源,个人觉得最好不要加上响应头去缓存。如果有缓存业务,可以采用JS代码去缓存
    • localForage库(本地缓存在localStorage、IndexedDB...)
    • MDN Caches 浏览器提供的缓存相关的接口
  2. 后台配置 Vary: Origin(未经过测试,理论上协商缓存可行,强缓存不行)

💡 查阅了MDN、stackoverflow、chromeium issue、WHATWG 都没有相关的介绍,如果有明确的来源欢迎评论👏

相关推荐
护国神蛙43 分钟前
给你一个页面如何定时刷新
前端·javascript·浏览器
一直游到海水变蓝丿1 小时前
el-select下拉框 添加 el-checkbox 多选框
前端·javascript·vue.js
游戏开发爱好者81 小时前
iOS App上线前的安全防线:项目后期如何用Ipa Guard与其他工具完成高效混淆部署
websocket·网络协议·tcp/ip·http·网络安全·https·udp
Amy.Wang2 小时前
常见的网络协议有哪些
网络·网络协议
阿奇__2 小时前
element 跨页选中,回显el-table选中数据
前端·vue.js·elementui
谢尔登2 小时前
【React】SWR 和 React Query(TanStack Query)
前端·react.js·前端框架
断竿散人2 小时前
专题一、HTML5基础教程-Viewport属性深入理解:移动端网页的魔法钥匙
前端
3Katrina2 小时前
理解Promise:让异步编程更优雅
前端·javascript
星之金币2 小时前
关于我用Cursor优化了一篇文章:30 分钟学会定制属于你的编程语言
前端·javascript
天外来物2 小时前
实战分享:用CI/CD实现持续部署
前端·nginx·docker