Skip to content

Commit 5e5259a

Browse files
committed
fix: sitemap test
Signed-off-by: ivelin <[email protected]>
1 parent adcef3b commit 5e5259a

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

next-sitemap.config.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
// next-sitemap.config.js
22
// File path: next-sitemap.config.js
3-
// Configures sitemap for K12Beast, including only root and public pages
4-
// Scans directories for page.tsx and page.mdx files, excluding dynamic routes and non-public paths
3+
// Configures sitemap for K12Beast, including only root and /public/* pages
4+
// Scans src/app/public for page.tsx and src/content/docs for page.mdx, excluding all other routes
55

66
const fs = require('fs');
77
const path = require('path');
88

99
const PUBLIC_DIR = path.join(process.cwd(), 'src/app/public');
1010
const DOCS_DIR = path.join(process.cwd(), 'src/content/docs');
1111

12-
function addPaths(dir, prefix, fileName) {
12+
function getPublicPaths(dir, prefix, fileName) {
13+
console.log(`[Sitemap Config] Scanning directory: ${dir}`);
1314
const paths = [];
15+
if (!fs.existsSync(dir)) {
16+
console.log(`[Sitemap Config] Directory not found: ${dir}`);
17+
return paths;
18+
}
1419
fs.readdirSync(dir).forEach(file => {
1520
const filePath = path.join(dir, file);
1621
if (fs.statSync(filePath).isDirectory()) {
17-
paths.push(...addPaths(filePath, `${prefix}/${file}`, fileName));
22+
paths.push(...getPublicPaths(filePath, `${prefix}/${file}`, fileName));
1823
} else if (file === fileName) {
1924
const relativePath = prefix.endsWith('/public') ? '/public' : prefix;
20-
// Exclude dynamic routes and non-public paths
25+
// Only include paths starting with /public/ or /public
2126
if (!relativePath.includes('[') && (relativePath === '/public' || relativePath.startsWith('/public/'))) {
27+
console.log(`[Sitemap Config] Including path: ${relativePath}`);
2228
paths.push({
2329
loc: relativePath,
2430
lastmod: new Date().toISOString(),
2531
changefreq: 'daily',
2632
priority: relativePath === '/public' ? 0.8 : 0.7,
2733
});
34+
} else {
35+
console.log(`[Sitemap Config] Excluding path: ${relativePath}`);
2836
}
2937
}
3038
});
@@ -35,7 +43,11 @@ module.exports = {
3543
siteUrl: 'https://k12beast.com',
3644
generateRobotsTxt: true,
3745
generateIndexSitemap: false,
46+
// Prevent automatic inclusion of src/app routes
47+
exclude: ['/*'], // Exclude all routes by default
3848
additionalPaths: async () => {
49+
console.log('[Sitemap Config] Generating sitemap paths');
50+
console.log(`[Sitemap Config] Environment: CI=${process.env.CI || 'false'}`);
3951
const paths = [
4052
{
4153
loc: '/',
@@ -44,8 +56,10 @@ module.exports = {
4456
priority: 1.0,
4557
},
4658
];
47-
paths.push(...addPaths(PUBLIC_DIR, '/public', 'page.tsx'));
48-
paths.push(...addPaths(DOCS_DIR, '/public/docs', 'page.mdx'));
59+
// Include all /public/* paths
60+
paths.push(...getPublicPaths(PUBLIC_DIR, '/public', 'page.tsx'));
61+
paths.push(...getPublicPaths(DOCS_DIR, '/public/docs', 'page.mdx'));
62+
console.log(`[Sitemap Config] Total paths generated: ${paths.length}`);
4963
return paths;
5064
},
5165
};

public/sitemap.xml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
3-
<url><loc>https://k12beast.com</loc><lastmod>2025-05-30T22:28:15.243Z</lastmod><changefreq>daily</changefreq><priority>1</priority></url>
4-
<url><loc>https://k12beast.com/public/about</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
5-
<url><loc>https://k12beast.com/public/confirm-success</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
6-
<url><loc>https://k12beast.com/public/login</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
7-
<url><loc>https://k12beast.com/public/privacy</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
8-
<url><loc>https://k12beast.com/public/reset-password</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
9-
<url><loc>https://k12beast.com/public/signup</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
10-
<url><loc>https://k12beast.com/public/terms</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
11-
<url><loc>https://k12beast.com/public/docs/getting-started</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
12-
<url><loc>https://k12beast.com/public/docs</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
13-
<url><loc>https://k12beast.com/public/docs/parents/introduction</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
14-
<url><loc>https://k12beast.com/public/docs/parents</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
15-
<url><loc>https://k12beast.com/public/docs/students</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
16-
<url><loc>https://k12beast.com/public/docs/subjects</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
17-
<url><loc>https://k12beast.com/public/docs/teachers/classroom</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
18-
<url><loc>https://k12beast.com/public/docs/teachers</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
19-
<url><loc>https://k12beast.com/public/docs/tutors/guide</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
20-
<url><loc>https://k12beast.com/public/docs/tutors</loc><lastmod>2025-05-30T22:28:15.244Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
3+
<url><loc>https://k12beast.com</loc><lastmod>2025-05-30T23:17:22.113Z</lastmod><changefreq>daily</changefreq><priority>1</priority></url>
4+
<url><loc>https://k12beast.com/public/about</loc><lastmod>2025-05-30T23:17:22.113Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
5+
<url><loc>https://k12beast.com/public/confirm-success</loc><lastmod>2025-05-30T23:17:22.113Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
6+
<url><loc>https://k12beast.com/public/login</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
7+
<url><loc>https://k12beast.com/public/privacy</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
8+
<url><loc>https://k12beast.com/public/reset-password</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
9+
<url><loc>https://k12beast.com/public/signup</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
10+
<url><loc>https://k12beast.com/public/terms</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
11+
<url><loc>https://k12beast.com/public/docs/getting-started</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
12+
<url><loc>https://k12beast.com/public/docs</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
13+
<url><loc>https://k12beast.com/public/docs/parents/introduction</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
14+
<url><loc>https://k12beast.com/public/docs/parents</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
15+
<url><loc>https://k12beast.com/public/docs/students</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
16+
<url><loc>https://k12beast.com/public/docs/subjects</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
17+
<url><loc>https://k12beast.com/public/docs/teachers/classroom</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
18+
<url><loc>https://k12beast.com/public/docs/teachers</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
19+
<url><loc>https://k12beast.com/public/docs/tutors/guide</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
20+
<url><loc>https://k12beast.com/public/docs/tutors</loc><lastmod>2025-05-30T23:17:22.114Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
2121
</urlset>

tests/server/sitemap.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ describe('Sitemap Generation', () => {
5050
'https://k12beast.com/public/terms',
5151
];
5252

53+
// Check for non-public URLs
54+
const nonPublicUrls = urls.filter(url => !url.match(/^https:\/\/k12beast\.com(\/?|\/public\/.*)$/));
55+
if (nonPublicUrls.length > 0) {
56+
console.log(`[Sitemap Test] Non-public URLs found: ${nonPublicUrls.join(', ')}`);
57+
throw new Error(`Non-public URLs found in sitemap: ${nonPublicUrls.join(', ')}`);
58+
}
59+
5360
// Verify total number of URLs
5461
if (urls.length !== expectedUrls.length) {
5562
console.log(`[Sitemap Test] URL count mismatch. Expected: ${expectedUrls.length}, Received: ${urls.length}`);
@@ -59,9 +66,6 @@ describe('Sitemap Generation', () => {
5966

6067
// Verify all URLs are either root or start with /public/
6168
for (const url of urls) {
62-
if (!url.match(/^https:\/\/k12beast\.com(\/?|\/public\/.*)$/)) {
63-
console.log(`[Sitemap Test] Invalid URL format: ${url}`);
64-
}
6569
expect(url).toMatch(/^https:\/\/k12beast\.com(\/?|\/public\/.*)$/);
6670
expect(expectedUrls).toContain(url);
6771
}

0 commit comments

Comments
 (0)