Skip to content

Commit

Permalink
chore(vth-frontend): add sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
AliKdhim87 committed May 23, 2024
1 parent 5fbb8dd commit ece8a5d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
77 changes: 77 additions & 0 deletions apps/vth-frontend/src/app/[locale]/sitemap.ts
Original file line number Diff line number Diff line change
@@ -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<MetadataRoute.Sitemap> {
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;
}
36 changes: 35 additions & 1 deletion apps/vth-frontend/src/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit ece8a5d

Please sign in to comment.