diff --git a/tunnel-server/src/proxy/html-manipulation/index.ts b/tunnel-server/src/proxy/html-manipulation/index.ts
index f1881373..83d7c178 100644
--- a/tunnel-server/src/proxy/html-manipulation/index.ts
+++ b/tunnel-server/src/proxy/html-manipulation/index.ts
@@ -28,22 +28,26 @@ export const injectScripts = async (
req: Pick,
res: stream.Writable & Pick, 'writeHead'>,
) => {
+ res.writeHead(proxyRes.statusCode as number, proxyRes.headers)
+
const injectsStr = req.headers[INJECT_SCRIPTS_HEADER] as string | undefined
- if (!injectsStr) {
+ const contentTypeHeader = proxyRes.headers['content-type']
+
+ if (!injectsStr || !contentTypeHeader) {
+ proxyRes.pipe(res)
return undefined
}
const {
type: contentType,
parameters: { charset: reqCharset },
- } = parseContentType(proxyRes)
+ } = parseContentType(contentTypeHeader)
if (contentType !== 'text/html') {
+ proxyRes.pipe(res)
return undefined
}
- res.writeHead(proxyRes.statusCode as number, proxyRes.headers)
-
const compress = compressionsForContentEncoding(proxyRes.headers['content-encoding'] || 'identity')
const [input, output] = compress
diff --git a/tunnel-server/src/proxy/index.ts b/tunnel-server/src/proxy/index.ts
index 2d131e6b..1fdf1b92 100644
--- a/tunnel-server/src/proxy/index.ts
+++ b/tunnel-server/src/proxy/index.ts
@@ -139,8 +139,7 @@ export const proxy = ({
?.filter(({ pathRegex }) => !pathRegex || pathRegex.test(mutatedReq.url || ''))
?.map(({ src, defer, async }) => ({ src, defer, async }))
- const shouldInject = Boolean(injects?.length)
- if (shouldInject) {
+ if (injects?.length) {
mutatedReq.headers[INJECT_SCRIPTS_HEADER] = JSON.stringify(injects)
}
@@ -153,7 +152,7 @@ export const proxy = ({
target: {
socketPath: activeTunnel.target,
},
- selfHandleResponse: shouldInject,
+ selfHandleResponse: true, // handled by the injectScripts onProxyRes hook
},
err => errorHandler(log, err, req, res)
)