-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-sitemap.ts
More file actions
140 lines (126 loc) · 3.66 KB
/
generate-sitemap.ts
File metadata and controls
140 lines (126 loc) · 3.66 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
import fs from 'fs'
import path from 'path'
import globby from 'globby'
import { execSync } from 'child_process'
const SITEMAP_PATH: string = path.join(
__dirname,
'src/app',
'sitemap-routes.ts'
)
interface UrlEntry {
url: string
lastModified: string
changefreq: string
priority: number
}
function getLastModDate(filePath: string): string {
try {
const safePath = filePath.replace(/[\s()]/g, '\\$&')
const result: string = execSync(
`git log -1 --format=%cd --date=iso ${safePath}`
)
.toString()
.trim()
return new Date(result).toISOString()
} catch (error) {
console.error('Error fetching last modification date:', error)
return new Date().toISOString()
}
}
async function generateSitemap(): Promise<void> {
const pages: string[] = await globby([
path.join(__dirname, 'src', 'app', '**', 'page.tsx'),
`!${path.join(__dirname, 'src', 'app', 'api', '**')}`,
`!${path.join(__dirname, 'src', 'app', 'auth', '**')}`,
`!${path.join(__dirname, 'src', 'app', '[locale]', 'dashboard', '**')}`,
`!${path.join(__dirname, 'src', 'app', '[locale]', 'risk-warning', '**')}`,
`!${path.join(
__dirname,
'src',
'app',
'[locale]',
'maintenance-in-progress',
'**'
)}`,
`!${path.join(__dirname, 'src', 'app', '[locale]', 'scorefront', '**')}`,
`!${path.join(
__dirname,
'src',
'app',
'[locale]',
'scorefront-new',
'**'
)}`,
`!${path.join(
__dirname,
'src',
'app',
'[locale]',
'scorefront-form',
'**'
)}`,
`!${path.join(__dirname, 'src', 'app', '[locale]', 'sitemap', '**')}`,
`!${path.join(
__dirname,
'src',
'app',
'[locale]',
'[...not-found]',
'**'
)}`,
]).then(pages =>
pages.filter(
page =>
!page.includes('[slug]') &&
!page.includes('[...not-found]') &&
!page.includes('[score]')
)
)
const allPossibleRoutes: string[] = await globby([
path.join(__dirname, 'src', 'app', '**', 'page.tsx'),
])
const allPossibleRoutesUrls = allPossibleRoutes
.map((filePath: string) =>
filePath
.replace(path.join(__dirname, 'src', 'app'), '')
.replace('/page.tsx', '')
.replace(/\\/g, '/')
.replace('/index', '')
.replace(/\/$/, '')
.replace(/\(scores\)\//g, '')
.replace(/\(attestation-like-scores\)\//g, '')
.replace(/\(attestations\)\//g, '')
.replace(/\/\[locale\]/g, '')
)
.sort((a, b) => a.localeCompare(b))
const urls: UrlEntry[] = pages
.map((filePath: string) => {
const lastModified: string = getLastModDate(filePath)
const route: string = filePath
.replace(path.join(__dirname, 'src', 'app'), '')
.replace('/page.tsx', '')
.replace(/\\/g, '/')
.replace('/index', '')
.replace(/\/$/, '')
.replace(/\(scores\)\//g, '')
.replace(/\(attestation-like-scores\)\//g, '')
.replace(/\(attestations\)\//g, '')
.replace(/\/\[locale\]/g, '')
const depth: number = (route.match(/\//g) || []).length
const priority: number = route === '' ? 1.0 : depth === 1 ? 0.9 : 0.8
return {
url: `https://nomis.cc${route}`,
lastModified,
changefreq: 'weekly',
priority,
}
})
.sort((a, b) => a.url.localeCompare(b.url))
const sitemapContent: string = `export const sitemapUrls = ${JSON.stringify(
urls
)}
export const allPossibleRoutesUrls = ${JSON.stringify(allPossibleRoutesUrls)}`
fs.writeFileSync(SITEMAP_PATH, sitemapContent)
console.log('Sitemap generated at:', SITEMAP_PATH)
}
generateSitemap()