Skip to content

Commit

Permalink
vtex proxy: replace only text/html and add referer for AviseMe path (#…
Browse files Browse the repository at this point in the history
…864)

* check content type before replace

* add referer

* fix: transfer referer logic to vtex loader

* add comment

---------

Co-authored-by: Gustavo Martins Vasconcellos <gustavo@carcara.tech>
  • Loading branch information
guitavano and carcara-gustavo authored Sep 24, 2024
1 parent ac67a2f commit 877e238
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
42 changes: 31 additions & 11 deletions vtex/loaders/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,38 @@ const buildProxyRoutes = (
const urlToProxy = `https://${hostname}`;
const hostToUse = hostname;

const routeFromPath = (pathTemplate: string): Route => ({
pathTemplate,
handler: {
value: {
__resolveType: "website/handlers/proxy.ts",
url: urlToProxy,
host: hostToUse,
includeScriptsToHead,
removeDirtyCookies: true,
const routeFromPath = (pathTemplate: string): Route => {
const handlerValue = {
__resolveType: "website/handlers/proxy.ts",
url: urlToProxy,
host: hostToUse,
includeScriptsToHead,
removeDirtyCookies: true,
};
// we have this check because we need to add
// the referer header to the AviseMe route
if (pathTemplate.includes("AviseMe")) {
return {
pathTemplate,
handler: {
value: {
...handlerValue,
customHeaders: [{
key: "referer",
value: urlToProxy,
}],
},
},
};
}

return ({
pathTemplate,
handler: {
value: handlerValue,
},
},
});
});
};
const routesFromPaths = [...PATHS_TO_PROXY, ...extraPaths].map(
routeFromPath,
);
Expand Down
2 changes: 1 addition & 1 deletion website/handlers/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export default function Proxy({
}
}
let text: undefined | string = undefined;
if (replaces && replaces.length > 0) {
if (replaces && replaces.length > 0 && contentType?.includes("text/html")) {
if (response.ok) {
text = await new Response(newBody).text();
replaces.forEach(({ from, to }) => {
Expand Down

0 comments on commit 877e238

Please sign in to comment.