This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathnext-sitemap.config.cjs
More file actions
159 lines (140 loc) · 4.24 KB
/
Copy pathnext-sitemap.config.cjs
File metadata and controls
159 lines (140 loc) · 4.24 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const path = require('path');
const fs = require('fs');
const matter = require('gray-matter');
/**
* Get published blog posts for sitemap
* Note: This runs at build time, so recently-scheduled posts may lag
*/
function getPublishedBlogPosts() {
const BLOG_DIR = path.join(process.cwd(), 'src/content/blog');
if (!fs.existsSync(BLOG_DIR)) {
return [];
}
const files = fs.readdirSync(BLOG_DIR).filter(f => f.endsWith('.md'));
const posts = [];
// Get current time in PT for publish check
const formatter = new Intl.DateTimeFormat('en-US', {
timeZone: 'America/Los_Angeles',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hour12: false,
});
const parts = formatter.formatToParts(new Date());
const get = (type) => parts.find(p => p.type === type)?.value ?? '';
const nowDate = `${get('year')}-${get('month')}-${get('day')}`;
const nowMinutes = parseInt(get('hour')) * 60 + parseInt(get('minute'));
for (const file of files) {
const filepath = path.join(BLOG_DIR, file);
const raw = fs.readFileSync(filepath, 'utf8');
const { data } = matter(raw);
// Check if post is published
if (data.status !== 'published') continue;
// Parse publish time
const timeMatch = data.publish_time_pt?.match(/^(1[0-2]|[1-9]):([0-5][0-9])(am|pm)$/i);
if (!timeMatch) continue;
let hours = parseInt(timeMatch[1]);
const mins = parseInt(timeMatch[2]);
const isPm = timeMatch[3].toLowerCase() === 'pm';
if (hours === 12) hours = isPm ? 12 : 0;
else if (isPm) hours += 12;
const postMinutes = hours * 60 + mins;
// Check if post is past publish date/time
const isPublished = nowDate > data.publish_date ||
(nowDate === data.publish_date && nowMinutes >= postMinutes);
if (isPublished && data.slug) {
posts.push(data.slug);
}
}
return posts;
}
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: process.env.NEXT_PUBLIC_SITE_URL || 'https://roocode.com',
generateRobotsTxt: true,
generateIndexSitemap: false, // We don't need index sitemap for a small site
changefreq: 'monthly',
priority: 0.7,
sitemapSize: 5000,
exclude: [
'/api/*',
'/server-sitemap-index.xml',
'/404',
'/500',
'/_not-found',
],
robotsTxtOptions: {
policies: [
{
userAgent: '*',
allow: '/',
},
],
additionalSitemaps: [
// Add any additional sitemaps here if needed in the future
],
},
// Custom transform function to set specific priorities and change frequencies
transform: async (config, path) => {
// Set custom priority for specific pages
let priority = config.priority;
let changefreq = config.changefreq;
if (path === '/') {
priority = 1.0;
changefreq = 'yearly';
} else if (path === '/evals') {
priority = 0.8;
changefreq = 'monthly';
} else if (path === '/privacy' || path === '/terms') {
priority = 0.5;
changefreq = 'yearly';
} else if (path === '/blog') {
priority = 0.8;
changefreq = 'weekly';
} else if (path.startsWith('/blog/')) {
priority = 0.7;
changefreq = 'monthly';
}
return {
loc: path,
changefreq,
priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
alternateRefs: config.alternateRefs ?? [],
};
},
additionalPaths: async (config) => {
const result = [];
// Add the /evals page since it's a dynamic route
result.push({
loc: '/evals',
changefreq: 'monthly',
priority: 0.8,
lastmod: new Date().toISOString(),
});
// Add /blog index
result.push({
loc: '/blog',
changefreq: 'weekly',
priority: 0.8,
lastmod: new Date().toISOString(),
});
// Add published blog posts
try {
const slugs = getPublishedBlogPosts();
for (const slug of slugs) {
result.push({
loc: `/blog/${slug}`,
changefreq: 'monthly',
priority: 0.7,
lastmod: new Date().toISOString(),
});
}
} catch (e) {
console.warn('Could not load blog posts for sitemap:', e.message);
}
return result;
},
};