-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathsitemap.ts
77 lines (68 loc) · 1.79 KB
/
sitemap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Proxy from "../../website/handlers/proxy.ts";
import { AppContext } from "../mod.ts";
type ConnInfo = Deno.ServeHandlerInfo;
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[];
}
/**
* @title Sitemap Proxy
*/
export default function Sitemap(
{ include }: Props,
{ publicUrl: url, usePortalSitemap, account }: AppContext,
) {
return async (
req: Request,
ctx: ConnInfo,
) => {
if (!url) {
throw new Error("Missing publicUrl");
}
const urlFromPublicUrl =
new URL(url?.startsWith("http") ? url : `https://${url}`).href;
/**
* Some stores were having problems with the IO sitemap (missing categories and brands)
*/
const publicUrl = usePortalSitemap
? `https://${account}.vtexcommercestable.com.br/`
: urlFromPublicUrl;
const response = await Proxy({
url: publicUrl,
})(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,
},
);
};
}