浏览器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 都没有相关的介绍,如果有明确的来源欢迎评论👏

相关推荐
Setsuna_F_Seiei30 分钟前
前端的 AI 学习之路 01 之 Agent API 调用 - 和 Agent 的基础对话
前端·人工智能·ai编程
threerocks1 小时前
AI 原生软件开发生命周期手册 - 如何借助 AI,逐阶段改造软件开发生命周期
前端·javascript·后端
徐小夕1 小时前
3分钟从想法到Agent上线:我们开源了一款AI可视化工作流“IDE”
前端·算法·github
Logintern091 小时前
网络通讯协议—TCP/IP协议
网络·网络协议·tcp/ip
why技术3 小时前
eli5,我觉得这个全网在吹的技能,使用体验真的很一般啊。
前端·人工智能·后端
excel3 小时前
记录升级 nuxt3 到 nuxt4 记录
前端
剑胆琴心静水深流4 小时前
全栈之路6---web集成与呈现
前端·vue.js·spring boot·分布式·spring·前端框架·npm
前端snow4 小时前
ai agent -- Memory汇总
前端
ITresearchGuest4 小时前
AI 焦虑下,前端该何去何从
前端·人工智能
Codiggerworld5 小时前
Chrome DevTools 隐藏神技:这 5 个调试技巧你可能从来没用过
前端·chrome·chrome devtools