Unverified 提交 d837abe5 authored 作者: Will Chen's avatar Will Chen 提交者: GitHub

Inject proxy script in all routes (#1623)

<!-- CURSOR_SUMMARY --> > [!NOTE] > Broaden injection to extensionless and .html routes, gate by Content-Type, and conditionally strip caching/compression headers only when injecting. > > - **Proxy HTML injection**: > - Update `needsInjection(pathname)` to inject for routes without extensions and `.html` files (e.g., `/`, `/foo`, `/foo/bar`, `*.html`). > - Only request uncompressed content and remove `if-none-match` when `needsInjection(target.pathname)` is true. > - During upstream response, inject only if `Content-Type` includes `text/html`; otherwise pass through without modification. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 2986ceccea64e7881a2197a2a0b202216f21d27d. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
上级 ab85c4be
......@@ -85,7 +85,9 @@ try {
/* ---------------------- helper: need to inject? ------------------------ */
function needsInjection(pathname) {
return pathname.endsWith("index.html") || pathname === "/";
// Inject for routes without a file extension (e.g., "/foo", "/foo/bar", "/")
const ext = path.extname(pathname).toLowerCase();
return ext === "" || ext === ".html";
}
function injectHTML(buf) {
......@@ -171,13 +173,12 @@ const server = http.createServer((clientReq, clientRes) => {
delete headers.referer;
}
}
if (needsInjection) {
if (needsInjection(target.pathname)) {
// Request uncompressed content from upstream
delete headers["accept-encoding"];
}
if (headers["if-none-match"] && needsInjection(target.pathname))
// Avoid getting cached resources.
delete headers["if-none-match"];
}
const upOpts = {
protocol: target.protocol,
......@@ -189,7 +190,16 @@ const server = http.createServer((clientReq, clientRes) => {
};
const upReq = lib.request(upOpts, (upRes) => {
const inject = needsInjection(target.pathname);
const wantsInjection = needsInjection(target.pathname);
// Only inject when upstream indicates HTML content
const contentTypeHeader = upRes.headers["content-type"];
const contentType = Array.isArray(contentTypeHeader)
? contentTypeHeader[0]
: contentTypeHeader || "";
const isHtml =
typeof contentType === "string" &&
contentType.toLowerCase().includes("text/html");
const inject = wantsInjection && isHtml;
if (!inject) {
clientRes.writeHead(upRes.statusCode, upRes.headers);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论