forked from codebucks27/Nextjs-tailwindcss-blog-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext-sitemap.config.js
More file actions
88 lines (75 loc) · 2.32 KB
/
Copy pathnext-sitemap.config.js
File metadata and controls
88 lines (75 loc) · 2.32 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const siteMetadata = require("./src/utils/siteMetaData");
const mongoose = require("mongoose");
const SITEMAP_PRIORITY = 0.7;
const SITEMAP_CHANGEFREQ = "daily";
const buildBlogEntry = (slug, lastmod) => {
if (!slug) return null;
return {
loc: `/blogs/${slug}`,
lastmod: lastmod || new Date().toISOString(),
changefreq: SITEMAP_CHANGEFREQ,
priority: SITEMAP_PRIORITY,
};
};
const getMongoBlogEntries = async () => {
const mongoUri = process.env.MONGODB_URI;
if (!mongoUri) return [];
try {
if (mongoose.connection.readyState === 0) {
await mongoose.connect(mongoUri, { bufferCommands: false });
}
const docs = await mongoose.connection.db
.collection("blogs")
.find(
{ status: "published", slug: { $exists: true, $ne: "" } },
{ projection: { slug: 1, updatedAt: 1, publishedAt: 1 } }
)
.toArray();
return docs
.map((doc) =>
buildBlogEntry(doc.slug, doc.updatedAt || doc.publishedAt)
)
.filter(Boolean);
} catch (error) {
console.error("next-sitemap: failed to load Mongo blogs", error);
return [];
}
};
const getVeliteBlogEntries = async () => {
try {
const generated = await import("./.velite/generated/index.js");
const blogs = generated.blogs || [];
return blogs
.filter((blog) => blog?.isPublished !== false && blog?.slug)
.map((blog) => buildBlogEntry(blog.slug, blog.updatedAt || blog.publishedAt))
.filter(Boolean);
} catch {
return [];
}
};
module.exports = {
siteUrl: siteMetadata.siteUrl,
generateRobotsTxt: true,
exclude: ["/icon.ico", "/apple-icon.png", "/manifest.webmanifest"],
additionalPaths: async () => {
const [mongoEntries, veliteEntries] = await Promise.all([
getMongoBlogEntries(),
getVeliteBlogEntries(),
]);
const deduped = new Map();
[...veliteEntries, ...mongoEntries].forEach((entry) => {
if (!entry?.loc) return;
const existing = deduped.get(entry.loc);
if (!existing) {
deduped.set(entry.loc, entry);
return;
}
const existingDate = new Date(existing.lastmod || 0).getTime();
const nextDate = new Date(entry.lastmod || 0).getTime();
if (nextDate > existingDate) {
deduped.set(entry.loc, entry);
}
});
return Array.from(deduped.values());
},
};