Skip to content

Commit

Permalink
Tavano/improve wake proxy (#296)
Browse files Browse the repository at this point in the history
* Revert "encodingFormat optional"

This reverts commit 4c783b8.

* improve proxy at wake integration

* remove duplicate type
  • Loading branch information
guitavano authored Jan 11, 2024
1 parent ff0124d commit 141a285
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
78 changes: 78 additions & 0 deletions wake/handlers/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Proxy, { Header } from "../../website/handlers/proxy.ts";
import { ConnInfo } from "std/http/server.ts";
import { AppContext } from "../mod.ts";

const BASE_SITEMAP_URL =
"https://p-general-prod-public.s3.sa-east-1.amazonaws.com/Sitemap";

const xmlHeader =
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

const includeSiteMaps = (
currentXML: string,
origin: string,
includes?: string[],
) => {
const siteMapIncludeTags = [];

for (const include of (includes ?? [])) {
siteMapIncludeTags.push(`
<sitemap>
<loc>${
include.startsWith("/") ? `${origin}${include}` : include
}</loc>
<lastmod>${new Date().toISOString().substring(0, 10)}</lastmod>
</sitemap>
`);
}

return siteMapIncludeTags.length > 0
? currentXML.replace(
xmlHeader,
`${xmlHeader}\n${siteMapIncludeTags.join("\n")}`,
)
: currentXML;
};

export interface Props {
include?: string[];
customHeaders: Header[];
}
/**
* @title Sitemap Proxy
*/
export default function Sitemap(
{ include, customHeaders }: Props,
{ account }: AppContext,
) {
return async (
req: Request,
ctx: ConnInfo,
) => {
if (!account) {
throw new Error("Missing account");
}

const publicUrl = `${BASE_SITEMAP_URL}/${account}/`;

const response = await Proxy({
url: publicUrl,
customHeaders,
})(req, ctx);

const reqUrl = new URL(req.url);
const text = await response.text();

return new Response(
includeSiteMaps(
text.replaceAll(publicUrl, `${reqUrl.origin}/`),
reqUrl.origin,
include,
),
{
headers: response.headers,
status: response.status,
},
);
};
}
51 changes: 48 additions & 3 deletions wake/loaders/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,41 @@ const PATHS_TO_PROXY = [
["/Fechamento"],
["/Fechamento/*"],
["/Login"],
["/Login/*"],
["/login/*"],
["/Login/Authenticate"],
["/Carrinho/*"],
["/api/*"],
];

const decoSiteMapUrl = "/sitemap/deco.xml";

export interface Props {
extraPathsToProxy?: string[];
/**
* @title If deco site map should be exposed at /deco-sitemap.xml
*/
generateDecoSiteMap?: boolean;
/**
* @title Other site maps to include
*/
includeSiteMap?: string[];
}

/**
* @title Wake Proxy Routes
*/
function loader(
_props: unknown,
props: unknown,
_req: Request,
{ checkoutUrl }: AppContext,
): Route[] {
const checkout = PATHS_TO_PROXY.map(([pathTemplate, basePath]) => ({
const { generateDecoSiteMap = true, includeSiteMap, extraPathsToProxy = [] } =
props as Props;

const checkout = [...PATHS_TO_PROXY, ...extraPathsToProxy].map((
[pathTemplate, basePath],
) => ({
pathTemplate,
handler: {
value: {
Expand All @@ -34,7 +54,32 @@ function loader(
},
}));

return checkout;
const [include, routes] = generateDecoSiteMap
? [[...(includeSiteMap ?? []), decoSiteMapUrl], [{
pathTemplate: decoSiteMapUrl,
handler: {
value: {
__resolveType: "website/handlers/sitemap.ts",
},
},
}]]
: [includeSiteMap, []];

// TODO: include is not working, because wake return all urls directly at /Sitemap.xml
const sitemap = {
pathTemplate: "/Sitemap.xml",
handler: {
value: {
__resolveType: "wake/handlers/sitemap.ts",
include,
customHeaders: [{
Host: checkoutUrl,
}],
},
},
};

return [...routes, ...checkout, sitemap];
}

export default loader;
4 changes: 4 additions & 0 deletions wake/manifest.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as $$$6 from "./loaders/proxy.ts";
import * as $$$7 from "./loaders/suggestion.ts";
import * as $$$8 from "./loaders/cart.ts";
import * as $$$9 from "./loaders/user.ts";
import * as $$$$0 from "./handlers/sitemap.ts";
import * as $$$$$$$$$0 from "./actions/submmitForm.ts";
import * as $$$$$$$$$1 from "./actions/notifyme.ts";
import * as $$$$$$$$$2 from "./actions/review/create.ts";
Expand All @@ -38,6 +39,9 @@ const manifest = {
"wake/loaders/user.ts": $$$9,
"wake/loaders/wishlist.ts": $$$3,
},
"handlers": {
"wake/handlers/sitemap.ts": $$$$0,
},
"actions": {
"wake/actions/cart/addCoupon.ts": $$$$$$$$$3,
"wake/actions/cart/addItem.ts": $$$$$$$$$4,
Expand Down

0 comments on commit 141a285

Please sign in to comment.