Skip to content

Commit

Permalink
feat(seo): add robots and sitemap configs
Browse files Browse the repository at this point in the history
  • Loading branch information
onetdev committed Nov 26, 2024
1 parent af1cc8c commit 19e734f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 6 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"react-timeago": "^7.2.0",
"react-use": "^17.5.1",
"sanitize-html": "^2.13.1",
"slugify": "^1.6.6",
"yaml": "^2.6.1",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.5",
Expand Down
1 change: 0 additions & 1 deletion scripts/build-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import fs from 'fs/promises';
import path from 'path';

import eslintRules from '@/root/eslint.config.mjs';

import manifestConfig from '@/root/manifest.config.js';

const main = async () => {
Expand Down
13 changes: 13 additions & 0 deletions src/app/robots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { MetadataRoute } from 'next';

import config from '@/config';

export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
crawlDelay: 10,
},
sitemap: `${config.url}/sitemap.xml`,
};
}
59 changes: 59 additions & 0 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { MetadataRoute } from 'next';
import { Languages } from 'next/dist/lib/metadata/types/alternative-urls-types';

import config from '@/config';
import { ArticleIndexEntrySchema } from '@/lib/schemas/article-index-entry';
import articlesRaw from '@/public/assets/articles/index.json';
import i18nConfig from '@/root/next-i18next.config';

const extraLangs = i18nConfig.i18n.locales.filter(
(lang) => lang !== i18nConfig.i18n.defaultLocale,
);
const genLangAlternates = (path: string): Languages<string> => {
const items = extraLangs.map((lang) => [
lang,
`${config.url}/${lang}/${path}`,
]);

return Object.fromEntries(items);
};

const commonPageMeta = (path: string): MetadataRoute.Sitemap[0] => ({
url: `${config.url}/${path}`,
lastModified: new Date(),
changeFrequency: 'daily',
alternates: {
languages: genLangAlternates(path),
},
});

export default function sitemap(): MetadataRoute.Sitemap {
const articles = articlesRaw.map((article: ArticleIndexEntrySchema) => {
const prefix =
article.locale === i18nConfig.i18n.defaultLocale
? `${config.url}`
: `${config.url}/${article.locale}`;

return {
url: `${prefix}/articles/${article.slug}`,
lastModified: new Date(),
} satisfies MetadataRoute.Sitemap[0];
});

return [
commonPageMeta(''),
commonPageMeta('about'),
commonPageMeta('contact'),
commonPageMeta('dilf'),
commonPageMeta('donate'),
commonPageMeta('flaim-a-phone'),
commonPageMeta('hot-things'),
commonPageMeta('privacy-policy'),
commonPageMeta('search'),
commonPageMeta('settings'),
commonPageMeta('user/login'),
commonPageMeta('user/password-reminder'),
commonPageMeta('user/registration'),
...articles,
];
}
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import manifest from '@/root/package.json';

const isDev = process.env.NEXT_PUBLIC_IS_DEV === 'true';
let url =
process.env.NEXT_PUBLIC_SITE_URL || 'https://www.themostannoyingwebsite.com';
if (isDev) {
url = `https://localhost:${process.env.PORT || 3000}`;
}

const config = {
contactEmail: 'info@themostannoyingwebsite.com',
githubRepo: manifest.repository.url,
defaultColorScheme: 'dark' as AppTheme,
isBrowser: typeof window !== 'undefined',
isDev,
url,
};

export default config;
6 changes: 2 additions & 4 deletions src/features/articles/services/ArticleService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import slugify from 'slugify';

import {
ArticleData,
ArticleDatum,
Expand Down Expand Up @@ -41,9 +39,9 @@ class ArticleService {
isOnCover: article.isOnCover,
locale: article.locale,
publishedAt: new Date(article.publishedAt),
slug: slugify(article.title),
slug: article.slug,
title: article.title,
url: `/articles/${slugify(article.title)}`,
url: `/articles/${article.slug}`,
}) satisfies ArticleDatum,
);
}
Expand Down
File renamed without changes.

0 comments on commit 19e734f

Please sign in to comment.