-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from dzcode-io/feat/sitemap-for-contributors
feat: sitemap for contributors pages
- Loading branch information
Showing
6 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Env } from "handler/contributor"; | ||
import { environments } from "@dzcode.io/utils/dist/config/environment"; | ||
import { allLanguages, LanguageEntity } from "@dzcode.io/models/dist/language"; | ||
import { getContributorURL } from "@dzcode.io/web/dist/utils/contributor"; | ||
import { fsConfig } from "@dzcode.io/utils/dist/config"; | ||
import { fetchV2Factory } from "@dzcode.io/utils/dist/fetch/factory"; | ||
import { Endpoints } from "@dzcode.io/api/dist/app/endpoints"; | ||
|
||
export const onRequest: PagesFunction<Env> = async (context) => { | ||
let stage = context.env.STAGE; | ||
if (!environments.includes(stage)) { | ||
console.log(`⚠️ No STAGE provided, falling back to "development"`); | ||
stage = "development"; | ||
} | ||
const fullstackConfig = fsConfig(stage); | ||
const fetchV2 = fetchV2Factory<Endpoints>(fullstackConfig); | ||
|
||
const { contributors } = await fetchV2("api:contributors/for-sitemap", {}); | ||
|
||
const hostname = "https://www.dzCode.io"; | ||
const links = contributors.reduce<{ url: string; lang: LanguageEntity["code"] }[]>((pV, cV) => { | ||
return [ | ||
...pV, | ||
...allLanguages.map(({ baseUrl, code }) => ({ | ||
url: `${baseUrl}${getContributorURL(cV)}`, | ||
lang: code, | ||
})), | ||
]; | ||
}, []); | ||
|
||
const xml = `<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" | ||
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" | ||
xmlns:xhtml="http://www.w3.org/1999/xhtml" | ||
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" | ||
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"> | ||
${links | ||
.map( | ||
(link) => ` | ||
<url> | ||
<loc>${hostname}${link.url}</loc> | ||
<xhtml:link rel="alternate" hreflang="${link.lang}" href="${hostname}${link.url}" /> | ||
</url>`, | ||
) | ||
.join("")} | ||
</urlset>`; | ||
|
||
return new Response(xml, { headers: { "content-type": "application/xml; charset=utf-8" } }); | ||
}; |