-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsitemap.ts
More file actions
38 lines (32 loc) · 1013 Bytes
/
sitemap.ts
File metadata and controls
38 lines (32 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { MetadataRoute } from 'next';
import dbConnect from '@/app/lib/dbConnect';
import Post from '@/app/models/Post';
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const baseUrl = process.env.NEXT_PUBLIC_DEPLOYMENT_URL || process.env.NEXTAUTH_URL || 'http://localhost:3000';
const staticPages = [
{
url: baseUrl,
lastModified: new Date(),
changeFrequency: 'weekly' as const,
},
{
url: `${baseUrl}/posts`,
lastModified: new Date(),
changeFrequency: 'weekly' as const,
},
];
if (!process.env.DB_URI) {
console.error('Database URI is not defined');
return staticPages;
}
await dbConnect(process.env.DB_URI);
const posts = await Post.find({}).sort({ date: -1 });
const postUrls = posts.map((post) => {
const postDate = new Date(post.updatedAt || post.date);
return {
url: `${baseUrl}/posts/${post.slug}`,
lastModified: postDate,
};
});
return [...staticPages, ...postUrls];
}