|
| 1 | +import { Env } from "handler/contributor"; |
| 2 | +import { environments } from "@dzcode.io/utils/dist/config/environment"; |
| 3 | +import { allLanguages, LanguageEntity } from "@dzcode.io/models/dist/language"; |
| 4 | +import { getContributorURL } from "@dzcode.io/web/dist/utils/contributor"; |
| 5 | +import { fsConfig } from "@dzcode.io/utils/dist/config"; |
| 6 | +import { fetchV2Factory } from "@dzcode.io/utils/dist/fetch/factory"; |
| 7 | +import { Endpoints } from "@dzcode.io/api/dist/app/endpoints"; |
| 8 | + |
| 9 | +export const onRequest: PagesFunction<Env> = async (context) => { |
| 10 | + let stage = context.env.STAGE; |
| 11 | + if (!environments.includes(stage)) { |
| 12 | + console.log(`⚠️ No STAGE provided, falling back to "development"`); |
| 13 | + stage = "development"; |
| 14 | + } |
| 15 | + const fullstackConfig = fsConfig(stage); |
| 16 | + const fetchV2 = fetchV2Factory<Endpoints>(fullstackConfig); |
| 17 | + |
| 18 | + const { contributors } = await fetchV2("api:contributors/for-sitemap", {}); |
| 19 | + |
| 20 | + const hostname = "https://www.dzCode.io"; |
| 21 | + const links = contributors.reduce<{ url: string; lang: LanguageEntity["code"] }[]>((pV, cV) => { |
| 22 | + return [ |
| 23 | + ...pV, |
| 24 | + ...allLanguages.map(({ baseUrl, code }) => ({ |
| 25 | + url: `${baseUrl}${getContributorURL(cV)}`, |
| 26 | + lang: code, |
| 27 | + })), |
| 28 | + ]; |
| 29 | + }, []); |
| 30 | + |
| 31 | + const xml = `<?xml version="1.0" encoding="UTF-8"?> |
| 32 | +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" |
| 33 | + xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" |
| 34 | + xmlns:xhtml="http://www.w3.org/1999/xhtml" |
| 35 | + xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" |
| 36 | + xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> |
| 37 | + ${links |
| 38 | + .map( |
| 39 | + (link) => ` |
| 40 | + <url> |
| 41 | + <loc>${hostname}${link.url}</loc> |
| 42 | + <xhtml:link rel="alternate" hreflang="${link.lang}" href="${hostname}${link.url}" /> |
| 43 | + </url>`, |
| 44 | + ) |
| 45 | + .join("")} |
| 46 | +</urlset>`; |
| 47 | + |
| 48 | + return new Response(xml, { headers: { "content-type": "application/xml; charset=utf-8" } }); |
| 49 | +}; |
0 commit comments