Răsfoiți Sursa

fix: strip conditional cache headers in preview proxy

304 responses have no body, so HTML path rewriting can't happen.
Remove If-None-Match/If-Modified-Since and force Cache-Control: no-cache
to ensure Vite always returns 200 with full HTML for rewriting.
guoziyun 3 săptămâni în urmă
părinte
comite
e7828686cb

+ 1 - 1
platform/server/dist/middleware/previewProxy.d.ts.map

@@ -1 +1 @@
-{"version":3,"file":"previewProxy.d.ts","sourceRoot":"","sources":["../../src/middleware/previewProxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAmB5C;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,IAAI,CAmF9D"}
+{"version":3,"file":"previewProxy.d.ts","sourceRoot":"","sources":["../../src/middleware/previewProxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAmB5C;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,IAAI,CAsF9D"}

+ 5 - 1
platform/server/dist/middleware/previewProxy.js

@@ -28,7 +28,7 @@ function previewProxy(req, res) {
     // Express mount 会剥离 /preview 前缀,req.url 即 Vite 需要的路径
     const targetPath = req.url || "/";
     console.log(`[preview:proxy] ${req.method} ${targetPath}`);
-    // 过滤请求 headers(去掉 hop-by-hop,设置正确的 host)
+    // 过滤请求 headers(去掉 hop-by-hop、条件缓存头,设置正确的 host)
     const reqHeaders = {};
     for (const [key, value] of Object.entries(req.headers)) {
         if (value === undefined)
@@ -38,9 +38,13 @@ function previewProxy(req, res) {
             continue;
         if (lk === "connection" || lk === "keep-alive" || lk === "upgrade")
             continue;
+        // 强制完整响应,避免 304 缓存导致 HTML 无法被重写
+        if (lk === "if-none-match" || lk === "if-modified-since" || lk === "cache-control")
+            continue;
         reqHeaders[key] = Array.isArray(value) ? value.join(", ") : value;
     }
     reqHeaders["host"] = `${VITE_HOST}:${VITE_PORT}`;
+    reqHeaders["cache-control"] = "no-cache";
     const proxyReq = http_1.default.request({
         hostname: VITE_HOST,
         port: VITE_PORT,

Fișier diff suprimat deoarece este prea mare
+ 0 - 0
platform/server/dist/middleware/previewProxy.js.map


+ 4 - 1
platform/server/src/middleware/previewProxy.ts

@@ -27,16 +27,19 @@ export function previewProxy(req: Request, res: Response): void {
   const targetPath = req.url || "/";
   console.log(`[preview:proxy] ${req.method} ${targetPath}`);
 
-  // 过滤请求 headers(去掉 hop-by-hop,设置正确的 host)
+  // 过滤请求 headers(去掉 hop-by-hop、条件缓存头,设置正确的 host)
   const reqHeaders: Record<string, string> = {};
   for (const [key, value] of Object.entries(req.headers)) {
     if (value === undefined) continue;
     const lk = key.toLowerCase();
     if (lk === "host") continue;
     if (lk === "connection" || lk === "keep-alive" || lk === "upgrade") continue;
+    // 强制完整响应,避免 304 缓存导致 HTML 无法被重写
+    if (lk === "if-none-match" || lk === "if-modified-since" || lk === "cache-control") continue;
     reqHeaders[key] = Array.isArray(value) ? value.join(", ") : value;
   }
   reqHeaders["host"] = `${VITE_HOST}:${VITE_PORT}`;
+  reqHeaders["cache-control"] = "no-cache";
 
   const proxyReq = http.request(
     {

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff