Skip to content

Commit f61b9d5

Browse files
committed
generate the rest of sitemaps in ./web-server
1 parent 7e67739 commit f61b9d5

6 files changed

Lines changed: 107 additions & 45 deletions

File tree

web-server/src/app/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import express from "express";
22
import path from "path";
3-
import { generateProjectsSitemap } from "src/handler/projects-sitemap";
3+
import { generateContributionsSitemap } from "src/handler/sitemap/contributions";
4+
import { generateContributorsSitemap } from "src/handler/sitemap/contributors";
5+
import { generateProjectsSitemap } from "src/handler/sitemap/projects";
46

57
const app = express();
68
const port = process.env.PORT || 6060;
79

810
const staticPath = path.join(__dirname, "../../../web/bundle");
911
const indexPath = path.join(staticPath, "index.html");
1012

11-
app.get("/w/contributions-sitemap.xml", (req, res) => {
12-
res.status(200).send("OK");
13+
app.get("/w/contributions-sitemap.xml", async (req, res) => {
14+
const xml = await generateContributionsSitemap();
15+
res.setHeader("Content-Type", "application/xml; charset=utf-8");
16+
res.status(200).send(xml);
1317
});
1418

15-
app.get("/w/contributors-sitemap.xml", (req, res) => {
16-
res.status(200).send("OK");
19+
app.get("/w/contributors-sitemap.xml", async (req, res) => {
20+
const xml = await generateContributorsSitemap();
21+
res.setHeader("Content-Type", "application/xml; charset=utf-8");
22+
res.status(200).send(xml);
1723
});
1824

1925
app.get("/w/projects-sitemap.xml", async (req, res) => {

web-server/src/handler/projects-sitemap.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { LANGUAGES } from "@dzcode.io/models/dist/language";
2+
import { fullstackConfig } from "src/utils/config";
3+
import { fetchV2Factory } from "@dzcode.io/utils/dist/fetch/factory";
4+
import { Endpoints } from "@dzcode.io/api/dist/app/endpoints";
5+
import { getContributionURL } from "@dzcode.io/web/dist/utils/contribution";
6+
import { generateXmlSitemap, SiteMapLink } from "./generate-xml-sitemap";
7+
8+
export const generateContributionsSitemap = async () => {
9+
const links: SiteMapLink[] = [];
10+
for (const lang of LANGUAGES) {
11+
const fetchV2 = fetchV2Factory<Endpoints>(fullstackConfig, lang.code);
12+
const { contributions } = await fetchV2("api:contributions/for-sitemap", {});
13+
for (const contribution of contributions) {
14+
links.push({
15+
url: `${lang.baseUrl}${getContributionURL(contribution)}`,
16+
lang: lang.code,
17+
});
18+
}
19+
}
20+
21+
return generateXmlSitemap(links, fullstackConfig.web.url);
22+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { LANGUAGES } from "@dzcode.io/models/dist/language";
2+
import { fullstackConfig } from "src/utils/config";
3+
import { fetchV2Factory } from "@dzcode.io/utils/dist/fetch/factory";
4+
import { Endpoints } from "@dzcode.io/api/dist/app/endpoints";
5+
import { getContributorURL } from "@dzcode.io/web/dist/utils/contributor";
6+
import { generateXmlSitemap, SiteMapLink } from "./generate-xml-sitemap";
7+
8+
export const generateContributorsSitemap = async () => {
9+
const links: SiteMapLink[] = [];
10+
for (const lang of LANGUAGES) {
11+
const fetchV2 = fetchV2Factory<Endpoints>(fullstackConfig, lang.code);
12+
const { contributors } = await fetchV2("api:contributors/for-sitemap", {});
13+
for (const contributor of contributors) {
14+
links.push({
15+
url: `${lang.baseUrl}${getContributorURL(contributor)}`,
16+
lang: lang.code,
17+
});
18+
}
19+
}
20+
21+
return generateXmlSitemap(links, fullstackConfig.web.url);
22+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { LanguageCode } from "@dzcode.io/utils/dist/language";
2+
3+
export interface SiteMapLink {
4+
url: string;
5+
lang: LanguageCode;
6+
}
7+
8+
export function generateXmlSitemap(
9+
links: Array<{ url: string; lang: LanguageCode }>,
10+
hostname: string,
11+
) {
12+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
13+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
14+
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
15+
xmlns:xhtml="http://www.w3.org/1999/xhtml"
16+
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
17+
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
18+
${links
19+
.map(
20+
(link) => `
21+
<url>
22+
<loc>${hostname}${link.url}</loc>
23+
<xhtml:link rel="alternate" hreflang="${link.lang}" href="${hostname}${link.url}" />
24+
</url>`,
25+
)
26+
.join("")}
27+
</urlset>`;
28+
29+
return xml;
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { LANGUAGES } from "@dzcode.io/models/dist/language";
2+
import { fullstackConfig } from "src/utils/config";
3+
import { fetchV2Factory } from "@dzcode.io/utils/dist/fetch/factory";
4+
import { Endpoints } from "@dzcode.io/api/dist/app/endpoints";
5+
import { getProjectURL } from "@dzcode.io/web/dist/utils/project";
6+
import { generateXmlSitemap, SiteMapLink } from "./generate-xml-sitemap";
7+
8+
export const generateProjectsSitemap = async () => {
9+
const links: SiteMapLink[] = [];
10+
for (const lang of LANGUAGES) {
11+
const fetchV2 = fetchV2Factory<Endpoints>(fullstackConfig, lang.code);
12+
const { projects } = await fetchV2("api:projects/for-sitemap", {});
13+
for (const project of projects) {
14+
links.push({
15+
url: `${lang.baseUrl}${getProjectURL(project)}`,
16+
lang: lang.code,
17+
});
18+
}
19+
}
20+
21+
return generateXmlSitemap(links, fullstackConfig.web.url);
22+
};

0 commit comments

Comments
 (0)