Skip to content

Commit 1824fe8

Browse files
authored
Divide sitemaps and create a index sitemap (#99)
* sort answers in user page * remove post.createdAt' * sort answers in user page * resolve conflicts * divide sitemap * Create sitemap index * give some spacing * remove main url from ind ex sitemap * move index sitemap to posts/sitemap.xml * move again
1 parent eec1a14 commit 1824fe8

File tree

5 files changed

+87
-42
lines changed

5 files changed

+87
-42
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { getBaseUrl } from '@/utils/urls'
2+
import { db } from '@nextjs-forum/db'
3+
import { URL_PER_SITEMAP } from '@/consts'
4+
5+
export const revalidate = 604800 // 1 Week in seconds
6+
7+
export async function GET() {
8+
const { postCount } = await db
9+
.selectFrom('posts')
10+
.select(db.fn.count('id').as('postCount'))
11+
.where('isIndexed', '=', true)
12+
.executeTakeFirstOrThrow()
13+
14+
const sitemapCount = Math.ceil(Number(postCount) / URL_PER_SITEMAP)
15+
16+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
17+
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
18+
${Array.from(
19+
{ length: sitemapCount },
20+
(_, index) => `
21+
<sitemap>
22+
<loc>${getBaseUrl()}/post/sitemap/${index}.xml</loc>
23+
</sitemap>`,
24+
).join('\n')}
25+
</sitemapindex>`
26+
27+
return new Response(xml, {
28+
headers: {
29+
'Content-Type': 'application/xml',
30+
},
31+
})
32+
}

apps/web/app/post/sitemap.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { MetadataRoute } from 'next'
2+
import { getBaseUrl } from '@/utils/urls'
3+
import { db } from '@nextjs-forum/db'
4+
import { URL_PER_SITEMAP } from '@/consts'
5+
6+
// Update sitemap only once every 6 hours
7+
export const revalidate = 21600
8+
9+
export async function generateSitemaps() {
10+
const { postCount } = await db
11+
.selectFrom('posts')
12+
.select(db.fn.count('id').as('postCount'))
13+
.where('isIndexed', '=', true)
14+
.executeTakeFirstOrThrow()
15+
16+
const sitemapCount = Math.ceil(Number(postCount) / URL_PER_SITEMAP)
17+
const sitemaps = Array.from({ length: sitemapCount }, (_, index) => ({
18+
id: index,
19+
}))
20+
21+
return sitemaps
22+
}
23+
24+
export default async function sitemap({
25+
id,
26+
}: {
27+
id: number
28+
}): Promise<MetadataRoute.Sitemap> {
29+
const start = id * URL_PER_SITEMAP
30+
const posts = await db
31+
.selectFrom('posts')
32+
.select(['snowflakeId', 'lastActiveAt'])
33+
.where('isIndexed', '=', true)
34+
.offset(start)
35+
.limit(URL_PER_SITEMAP)
36+
.execute()
37+
38+
return posts.map((p) => ({
39+
url: `${getBaseUrl()}/post/${p.snowflakeId}`,
40+
changeFrequency: 'weekly',
41+
priority: 0.9,
42+
lastModified: p.lastActiveAt,
43+
})) satisfies MetadataRoute.Sitemap
44+
}

apps/web/app/sitemap.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
1-
import { MetadataRoute } from 'next'
21
import { getBaseUrl } from '@/utils/urls'
3-
import { db } from '@nextjs-forum/db'
4-
5-
// Update sitemap only once every 6 hours
6-
export const revalidate = 21600
7-
8-
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
9-
const posts = await db
10-
.selectFrom('posts')
11-
.select(['snowflakeId', 'lastActiveAt'])
12-
.where('isIndexed', '=', true)
13-
.limit(50_000) // we will probably need to chunk the sitemap in the future
14-
.execute()
2+
import { MetadataRoute } from 'next'
153

4+
export default function sitemap(): MetadataRoute.Sitemap {
165
return [
176
{
187
url: getBaseUrl(),
198
changeFrequency: 'daily',
209
priority: 1,
2110
},
22-
...posts.map((p) => {
23-
return {
24-
url: `${getBaseUrl()}/post/${p.snowflakeId}`,
25-
changeFrequency: 'weekly',
26-
priority: 0.9,
27-
lastModified: p.lastActiveAt,
28-
} satisfies MetadataRoute.Sitemap[0]
29-
}),
3011
]
3112
}

apps/web/consts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const URL_PER_SITEMAP = 10_000

pnpm-lock.yaml

Lines changed: 8 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)