From c5aa1f336a7b83603994b88fc744bab6a321f09b Mon Sep 17 00:00:00 2001 From: "Charles-P. Clermont" Date: Sat, 14 Nov 2020 13:22:03 -0500 Subject: [PATCH] Rethink the shouldTransform logic It's not the first time I'm getting bit by a missing Accept HTTP header. Instead, do the switching logic on the receiving end. On the content-type of the response. --- index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 83100bd..fc33a0e 100644 --- a/index.js +++ b/index.js @@ -173,12 +173,9 @@ async function handleRequest(request) { const bypassTransform = config['x-bypass-transform'] const acceptHeader = request.headers.get('accept') - const isHtmlRequest = - acceptHeader && - (acceptHeader.includes('text/html') || acceptHeader.includes('*/*')) const shouldBypassTransform = bypassTransform && bypassTransform.indexOf('true') !== -1 - const shouldTransform = isHtmlRequest && !shouldBypassTransform + const shouldTransform = !shouldBypassTransform const req = new Request(url, request) req.headers.delete('x-host') @@ -206,6 +203,12 @@ async function handleRequest(request) { const response = await getResponse(req, shouldPush, shopId) + const isHtmlResponse = response.headers.get('content-type').includes('html'); + if (!isHtmlResponse) { + console.log('Not an HTML response', req.url); + return response; + } + // Add the link header if (linkHeader) response.headers.append('Link', linkHeader)