Skip to content

Commit

Permalink
feat: add sitemap and robots.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilsang committed Aug 24, 2024
1 parent 5b62f1f commit ff4d816
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 18 deletions.
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 { MyProfile } from '~/about/headline/data/profile';

export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
allow: '/',
disallow: ['/favicon', '/fonts', '/images', '/open.mp4'],
},
sitemap: `${MyProfile.blog.href}/sitemap.xml`,
};
}
60 changes: 60 additions & 0 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { MetadataRoute } from 'next';
import { MyProfile } from '~/about/headline/data/profile';
import { getAllPosts, getAllTags } from '~/shared/helpers/post';

export default function sitemap(): MetadataRoute.Sitemap {
const DOMAIN = MyProfile.blog.href;
const PRIORITY = {
VERY_HIGH: 1,
HIGH: 0.8,
MID: 0.5,
};

const posts = getAllPosts(['tags']);
const tags = getAllTags();

const postUrls: MetadataRoute.Sitemap = posts.map((post) => ({
url: `${DOMAIN}/posts/${post.url}`,
lastModified: new Date(post.updatedAt ?? post.date),
changeFrequency: 'daily',
priority: PRIORITY.VERY_HIGH,
}));
const tagUrls: MetadataRoute.Sitemap = tags.map((tag) => {
const post = posts.find((post) => post.tags.includes(tag))!;
return {
url: `${DOMAIN}/tags/${tag}`,
lastModified: new Date(post.date ?? post.updatedAt),
changeFrequency: 'weekly',
priority: PRIORITY.MID,
};
});

return [
{
url: DOMAIN,
lastModified: new Date(),
changeFrequency: 'monthly',
priority: PRIORITY.MID,
},
{
url: `${DOMAIN}/about`,
lastModified: new Date(),
changeFrequency: 'weekly',
priority: PRIORITY.VERY_HIGH,
},
{
url: `${DOMAIN}/posts`,
lastModified: new Date(),
changeFrequency: 'weekly',
priority: PRIORITY.HIGH,
},
{
url: `${DOMAIN}/tags`,
lastModified: new Date(),
changeFrequency: 'weekly',
priority: PRIORITY.MID,
},
...postUrls,
...tagUrls,
];
}
12 changes: 3 additions & 9 deletions src/app/tags/[tag]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { NextPage } from 'next';

import Navbar from '~/shared/components/nav/Navbar';
import { Footer } from '~/shared/components/Footer';
import { getAllPosts } from '~/shared/helpers/post';
import { getAllPosts, getAllTags } from '~/shared/helpers/post';
import TagDetailContainer from '~/tags/detail/Container';
import { MainLayout } from '~/shared/components/MainLayout';

Expand Down Expand Up @@ -34,14 +34,8 @@ const Tags: NextPage<TagsDetailProps> = ({ params: { tag } }) => {
export default Tags;

export async function generateStaticParams(): Promise<{ tag: string }[]> {
const allTags = getAllPosts(['tags'])
.map((item) => item.tags)
.flatMap((tags) => tags);
const tags = [...new Set(allTags)];
const paths = tags.map((tag) => {
return { tag };
});

const tags = getAllTags();
const paths = tags.map((tag) => ({ tag }));
return paths;
}

Expand Down
11 changes: 2 additions & 9 deletions src/app/tags/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ import { type NextPage } from 'next';
import { Footer } from '~/shared/components/Footer';
import { MainLayout } from '~/shared/components/MainLayout';
import Navbar from '~/shared/components/nav/Navbar';
import { getAllPosts, getPostBySlug } from '~/shared/helpers/post';
import { getAllTags } from '~/shared/helpers/post';
import TagListContainer from '~/tags/tagList/Container';

const Tags: NextPage = () => {
const rawPosts = getAllPosts(['slug']);
const allTags = rawPosts
.map((rawPost) => {
const data = getPostBySlug(rawPost.slug, ['tags']);
return data.tags;
})
.flatMap((tagList) => tagList);
const tags = [...new Set(allTags)];
const tags = getAllTags();

return (
<MainLayout>
Expand Down
6 changes: 6 additions & 0 deletions src/features/shared/helpers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export const getAllPosts = (fields: (keyof PostType)[] = []) => {
return posts;
};

export const getAllTags = () => {
const posts = getAllPosts(['tags']);
const tags = [...new Set(posts.map((post) => post.tags).flat())];
return tags;
};

export const getPost = async (url: PostType['url']): Promise<PostType> => {
if (!urlToSlugMap[url]) {
// NOTE: It will set urlToSlugMap
Expand Down

0 comments on commit ff4d816

Please sign in to comment.