-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
70 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export async function proxyLiquidRoute( | ||
request: Request, | ||
storefrontDomain: string, | ||
destinationPath: string, | ||
): Promise<Response> { | ||
const clientIP = request.headers.get('X-Shopify-Client-IP'); | ||
const clientIPSig = request.headers.get('X-Shopify-Client-IP-Sig'); | ||
|
||
const headers = new Headers(); | ||
const host = `${storefrontDomain}.myshopify.com`; | ||
|
||
const headersToFilterOut = ['connection']; | ||
|
||
for (const [key, value] of request.headers.entries()) { | ||
if (!headersToFilterOut.includes(key)) { | ||
headers.append( | ||
key, | ||
swapHostname(value, { | ||
hostname: new URL(request.url).host, | ||
newHostname: host, | ||
}), | ||
); | ||
} | ||
} | ||
|
||
if (!clientIP || !clientIPSig) { | ||
console.warn( | ||
'Proxying the online store is only available in Oxygen. This request is likely to be throttled.', | ||
); | ||
} | ||
|
||
return fetch( | ||
`https://${host}${ | ||
destinationPath.startsWith('/') ? destinationPath : '/' + destinationPath | ||
}`, | ||
{headers}, | ||
).then((resp) => { | ||
const headers = new Headers(resp.headers); | ||
headers.delete('content-encoding'); | ||
return new Response(resp.body, {headers}); | ||
}); | ||
} | ||
|
||
function swapHostname( | ||
str: string, | ||
{hostname, newHostname}: {hostname: string; newHostname: string}, | ||
) { | ||
return str.replaceAll(hostname, newHostname); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters