-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kohei Asai
committed
Feb 4, 2024
1 parent
4f615c5
commit 23182f2
Showing
8 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { type MetadataRoute } from "next"; | ||
import { getConfig } from "~/helpers/config"; | ||
|
||
function robots(): MetadataRoute.Robots { | ||
const config = getConfig(); | ||
|
||
return { | ||
rules: { | ||
userAgent: "*", | ||
allow: "/", | ||
}, | ||
sitemap: `${config.urlOrigin}/sitemap.xml`, | ||
}; | ||
} | ||
|
||
export default robots; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { type MetadataRoute } from "next"; | ||
import { getConfig } from "~/helpers/config"; | ||
import { queryAllPostsRegardlessLocale } from "~/queries/query-all-posts-regardless-locale"; | ||
|
||
async function sitemap(): Promise<MetadataRoute.Sitemap> { | ||
const config = getConfig(); | ||
const posts = await queryAllPostsRegardlessLocale(); | ||
|
||
const entries: MetadataRoute.Sitemap = [ | ||
{ | ||
url: `${config.urlOrigin}/`, | ||
lastModified: new Date(), | ||
changeFrequency: "yearly", | ||
priority: 1, | ||
}, | ||
]; | ||
|
||
for (const post of posts) { | ||
entries.push({ | ||
url: `${config.urlOrigin}/posts/${post.slug}`, | ||
lastModified: post.lastEditedAt, | ||
changeFrequency: "weekly", | ||
priority: 0.9, | ||
}); | ||
} | ||
|
||
return entries; | ||
} | ||
|
||
export default sitemap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import "server-only"; | ||
|
||
import { isAfter } from "date-fns"; | ||
import { availableLocales } from "~/helpers/locale"; | ||
import { type Locale } from "~/models/locale"; | ||
import { type Post } from "~/models/post"; | ||
import { findPosts } from "~/repositories/find-posts"; | ||
|
||
async function queryAllPostsRegardlessLocale(): Promise<Post[]> { | ||
const postsByLocale: Partial<Record<Locale, Post[]>> = {}; | ||
|
||
await Promise.all( | ||
availableLocales.map(async (locale) => { | ||
const posts = await findPosts({ locale, includeDrafts: false }); | ||
|
||
postsByLocale[locale] = posts; | ||
}), | ||
); | ||
|
||
const postsBySlug: Record<Post["slug"], Post> = {}; | ||
|
||
for (const posts of Object.values(postsByLocale)) { | ||
for (const post of posts) { | ||
postsBySlug[post.slug] ??= post; | ||
|
||
if (isAfter(post.createdAt, postsBySlug[post.slug].createdAt)) { | ||
postsBySlug[post.slug].createdAt = post.createdAt; | ||
} | ||
|
||
if (isAfter(post.lastEditedAt, postsBySlug[post.slug].lastEditedAt)) { | ||
postsBySlug[post.slug].lastEditedAt = post.lastEditedAt; | ||
} | ||
} | ||
} | ||
|
||
return Object.values(postsBySlug); | ||
} | ||
|
||
export { queryAllPostsRegardlessLocale }; |