From a2aff51deb928a9339bd288346d92d2d93e6f365 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Wed, 4 Jan 2023 18:08:47 +0400 Subject: [PATCH] Add sitemap generation --- astro.config.mjs | 5 ++- sitemap.mjs | 54 +++++++++++++++++++++++++ src/guides/http-basic-authentication.md | 2 +- src/lib/sitemap.ts | 16 -------- src/pages/404.astro | 2 +- 5 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 sitemap.mjs delete mode 100644 src/lib/sitemap.ts diff --git a/astro.config.mjs b/astro.config.mjs index 023f2a6e5..291bff25a 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,5 +1,5 @@ import { defineConfig } from 'astro/config'; -import { shouldIndexPage } from './src/lib/sitemap'; +import { serializeSitemap, shouldIndexPage } from './sitemap.mjs'; // https://astro.build/config import tailwind from '@astrojs/tailwind'; @@ -29,6 +29,7 @@ export default defineConfig({ }), sitemap({ filter: shouldIndexPage, + serialize: serializeSitemap, }), ], -}); \ No newline at end of file +}); diff --git a/sitemap.mjs b/sitemap.mjs new file mode 100644 index 000000000..c153e7017 --- /dev/null +++ b/sitemap.mjs @@ -0,0 +1,54 @@ +import path from 'node:path'; +import fs from 'node:fs/promises'; + +async function getRoadmapIds() { + return fs.readdir(path.join(process.cwd(), 'src/roadmaps')); +} + +export function shouldIndexPage(page) { + return ![ + 'https://roadmap.sh/404/', + 'https://roadmap.sh/terms/', + 'https://roadmap.sh/privacy/', + ].includes(page); +} + +export async function serializeSitemap(item) { + const highPriorityPages = [ + 'https://roadmap.sh/', + 'https://roadmap.sh/about/', + 'https://roadmap.sh/roadmaps/', + 'https://roadmap.sh/guides/', + 'https://roadmap.sh/videos/', + ...(await getRoadmapIds()).map((id) => `https://roadmap.sh/${id}/`), + ]; + + // Roadmaps and other high priority pages + for (let pageUrl of highPriorityPages) { + if (item.url === pageUrl) { + return { + ...item, + // @ts-ignore + changefreq: 'monthly', + priority: 1, + lastmod: new Date().toISOString(), + }; + } + } + + // Guide and video pages + if ( + item.url.startsWith('https://roadmap.sh/guides/') || + item.url.startsWith('https://roadmap.sh/videos/') + ) { + return { + ...item, + // @ts-ignore + changefreq: 'monthly', + priority: 0.9, + lastmod: new Date().toISOString(), + }; + } + + return undefined; +} diff --git a/src/guides/http-basic-authentication.md b/src/guides/http-basic-authentication.md index 9cd9568df..796df4da7 100644 --- a/src/guides/http-basic-authentication.md +++ b/src/guides/http-basic-authentication.md @@ -40,7 +40,7 @@ Now that we know what basic authentication is, the question is, how does it work ### Step 1 When the browser first requests the server, the server tries to check the availability of the `Authorization` header in the request. Because it is the first request, no `Authorization` header is found in the request. So the server responds with the `401 Unauthorized` response code and also sends the `WWW-Authenticate` header with the value set to `Basic`, which tells the browser that it needs to trigger the basic authentication flow. -```plaintext +``` 401 Unauthorized WWW-Authenticate: Basic realm='user_pages' ``` diff --git a/src/lib/sitemap.ts b/src/lib/sitemap.ts deleted file mode 100644 index edeebc183..000000000 --- a/src/lib/sitemap.ts +++ /dev/null @@ -1,16 +0,0 @@ -import type { SitemapItem } from '@astrojs/sitemap'; - -export function shouldIndexPage(page: string): boolean { - return ![ - 'https://roadmap.sh/404/', - 'https://roadmap.sh/terms/', - 'https://roadmap.sh/privacy/', - ].includes(page); -} - -export function serialize(item: SitemapItem): SitemapItem { - console.log(item); - return { - ...item, - }; -} diff --git a/src/pages/404.astro b/src/pages/404.astro index 0cdc82672..0f59b9619 100644 --- a/src/pages/404.astro +++ b/src/pages/404.astro @@ -11,7 +11,7 @@ import BaseLayout from '../layouts/BaseLayout.astro';