From ece8a5d00cda814e910852aaba292c2c494de9d1 Mon Sep 17 00:00:00 2001 From: Ali Amori Kadhim Date: Wed, 22 May 2024 14:53:48 +0200 Subject: [PATCH] chore(vth-frontend): add sitemap --- apps/vth-frontend/src/app/[locale]/sitemap.ts | 77 +++++++++++++++++++ apps/vth-frontend/src/query/index.ts | 36 ++++++++- 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 apps/vth-frontend/src/app/[locale]/sitemap.ts diff --git a/apps/vth-frontend/src/app/[locale]/sitemap.ts b/apps/vth-frontend/src/app/[locale]/sitemap.ts new file mode 100644 index 00000000..cd208386 --- /dev/null +++ b/apps/vth-frontend/src/app/[locale]/sitemap.ts @@ -0,0 +1,77 @@ +import { MetadataRoute } from 'next'; +import { cookies } from 'next/headers'; +import { GET_ALL_ARTICLES_SLUGS, GET_ALL_THEME_SLUGS, GET_NAVIGATION_PAGES } from '@/query'; +import { createStrapiURL } from '@/util/createStrapiURL'; +import { fetchData } from '@/util/fetchData'; + +const { origin } = new URL(process.env.FRONTEND_PUBLIC_URL || 'http://localhost:3000'); +export type Attributes = { + slug: string; + updatedAt: string; +}; +export type Urls = { + attributes: Attributes; +}; +export interface GenerateUrlParameter { + locale?: string; + origin: string; + segment?: string; + urls: Urls[]; +} +const constructHref = ({ segments, baseURL }: { segments: any[]; baseURL: string | URL }) => { + const href = new URL( + segments + .filter(Boolean) // Removes falsy values like undefined, null, or empty strings + .join('/') + '/', // Joins with '/' and ensures a trailing slash + baseURL, + ).href; + return href; +}; + +export const generateUrl = ({ locale, origin, segment, urls }: GenerateUrlParameter) => { + const isURls = Array.isArray(urls); + + return isURls + ? urls.map((url) => ({ + url: constructHref({ segments: [origin, locale, segment, url.attributes.slug], baseURL: new URL(origin) }), + lastModified: url.attributes.updatedAt, + })) + : []; +}; + +export default async function sitemap(): Promise { + const locale = cookies().get('i18next')?.value; + const staticPages = generateUrl({ + locale, + origin, + urls: [ + { + attributes: { slug: '', updatedAt: new Date().toISOString() }, // home page + }, + ], + }); + const { data: articleSlugsData } = await fetchData({ + url: createStrapiURL(), + query: GET_ALL_ARTICLES_SLUGS, + }); + const { data: navigationSlugsData } = await fetchData({ + url: createStrapiURL(), + query: GET_NAVIGATION_PAGES, + }); + const { data: themeSlugsData } = await fetchData({ + url: createStrapiURL(), + query: GET_ALL_THEME_SLUGS, + }); + + const articlePages = generateUrl({ locale, origin, segment: 'article', urls: articleSlugsData.articlePages.data }); + const navigationPages = generateUrl({ + locale, + origin, + urls: navigationSlugsData?.navigationPages?.data, + }); + const themePages = generateUrl({ locale, origin, segment: 'theme', urls: themeSlugsData?.themePages?.data }); + + const fields = [...articlePages, ...themePages, ...staticPages, ...navigationPages]; + + return fields; +} diff --git a/apps/vth-frontend/src/query/index.ts b/apps/vth-frontend/src/query/index.ts index 9479f723..eddff66e 100644 --- a/apps/vth-frontend/src/query/index.ts +++ b/apps/vth-frontend/src/query/index.ts @@ -52,12 +52,17 @@ query getHomepage($pageMode: PublicationState) { export const GET_NAVIGATION_PAGES = gql(` query getNavigationPages($pageMode: PublicationState) { - navigationPages(publicationState: $pageMode, sort: ["order:asc", "title:asc"]) { + navigationPages( + publicationState: $pageMode + sort: ["order:asc", "title:asc"] + pagination: { start: 0, limit: -1 } + ) { data { id attributes { title slug + updatedAt } } } @@ -200,6 +205,35 @@ query GET_THEME_BY_SLUG($slug: String, $pageMode: String) { } `); +export const GET_ALL_THEME_SLUGS = gql(` +query getAllThemeSlugs { + themePages(sort: ["title:asc"], pagination: { start: 0, limit: -1 }) { + data { + id + attributes { + title + slug + updatedAt + } + } + } +} +`); + +export const GET_ALL_ARTICLES_SLUGS = gql(` +query getAllAriclesSlugs { + articlePages(pagination: { start: 0, limit: -1 }, sort: ["title:asc"]) { + data { + attributes { + title + slug + updatedAt + } + } + } +} +`); + export const GET_ARTICLE_BY_SLUG = gql(` query GET_ARTICLE_BY_SLUG($slug: String, $pageMode: String) { findSlug(