Skip to content

Commit

Permalink
Rethink the shouldTransform logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
charlespwd committed Nov 14, 2020
1 parent b7c2b86 commit c5aa1f3
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit c5aa1f3

Please sign in to comment.