forked from quran/quran.com-frontend-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next-sitemap.js
184 lines (172 loc) · 6.52 KB
/
next-sitemap.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* eslint-disable jsdoc/require-returns */
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable react-func/max-lines-per-function */
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
const range = require('lodash/range');
const fetch = require('node-fetch');
const englishChaptersData = require('./data/chapters/en.json');
const { locales } = require('./i18n.json');
const isProduction = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production';
const isDevelopment = process.env.NEXT_PUBLIC_VERCEL_ENV === 'development';
const BASE_PATH =
`${isDevelopment ? 'http' : 'https'}://${process.env.NEXT_PUBLIC_VERCEL_URL}` ||
'https://quran.com';
const chapters = range(1, 115);
const getAvailableTafsirs = async () => {
const res = await fetch(`https://api.qurancdn.com/api/qdc/resources/tafsirs`);
const data = await res.json();
return data;
};
const getAvailableReciters = async () => {
const res = await fetch(`https://api.qurancdn.com/api/qdc/audio/reciters`);
const data = await res.json();
return data;
};
/**
* Get the alternate ref objects for a path. We append "-remove-from-here" because
* next-sitemap library appends the alternate ref to the beginning
* of the url of the English version rather than replacing it completely.
* e.g. for english the url is /surah/al-fatihah/info
* but for Arabic it becomes /ar/surah/سورة-الفاتحة/info/surah/al-fatihah/info
* although we don't want "surah/al-fatihah/info" part at the end of the path.
* so appending "-remove-from-here" indicator would make it easier for us to manually
* process the .xml file and remove the characters starting from "-remove-from-here".
* e.g. /ar/سورة-الفاتحة/1-remove-from-here/al-fatihah/1
*
* @param {number} chapterId
* @param {boolean} appendSlug
* @param {string} prefix
* @param {string} suffix
*/
const getAlternateRefs = (chapterId = null, appendSlug = true, prefix = '', suffix = '') => {
return locales.map((locale) => {
let href = `${BASE_PATH}/${locale}`;
if (prefix) {
href = `${href}/${prefix}`;
}
if (appendSlug) {
const localeChaptersData = require(`./data/chapters/${locale}.json`);
href = `${href}/${localeChaptersData[chapterId].slug}`;
}
if (suffix) {
href = `${href}/${suffix}`;
}
return {
href: `${href}-remove-from-here`,
hreflang: locale,
};
});
};
module.exports = {
siteUrl: BASE_PATH,
sitemapSize: 20000,
generateRobotsTxt: isProduction,
exclude: [...locales.map((locale) => `/${locale}`), '/*/product-updates*', '/*/search'],
alternateRefs: locales.map((locale) => ({
href: `${BASE_PATH}/${locale}`,
hreflang: locale,
})),
transform: async (config, path) => {
return {
loc: path,
lastmod: new Date().toISOString(),
alternateRefs: config.alternateRefs ?? [],
};
},
additionalPaths: async (config) => {
const result = [];
let tafsirSlugs = [];
let reciterIds = [];
await getAvailableTafsirs().then((response) => {
tafsirSlugs = response.tafsirs.map((tafsir) => tafsir.slug);
});
await getAvailableReciters().then((response) => {
reciterIds = response.reciters.map((reciter) => reciter.id);
});
chapters.forEach((chapterId) => {
// 1. add the chapter slugs in English along with the localized slugs in every locale
const englishChapterSlug = englishChaptersData[chapterId].slug;
result.push({
loc: `/${englishChapterSlug}`,
alternateRefs: getAlternateRefs(chapterId),
});
// 2. add slugged /surah/[chapterSlug]/info in English along with the localized slugs in every locale
result.push({
loc: `/surah/${englishChapterSlug}/info`,
alternateRefs: getAlternateRefs(chapterId, true, 'surah', 'info'),
});
// 3. /reciters/[reciterId]/[chapterSlug]
reciterIds.forEach((reciterId) => {
const location = `/reciters/${reciterId}/${englishChapterSlug}`;
result.push({
loc: location,
alternateRefs: getAlternateRefs(chapterId, false, '', location),
});
});
// 4. generate the verses for each of the chapters in each locale as well
range(englishChaptersData[chapterId].versesCount).forEach((verseId) => {
const verseNumber = verseId + 1;
const verseIdValue = verseNumber;
const verseKey = `${chapterId}:${verseIdValue}`;
const isAyatulKursi = chapterId === 2 && verseNumber === 255;
if (isAyatulKursi) {
// instead of /al-baqarah/255, we push /ayatul-kursi
result.push({
loc: `/ayatul-kursi`,
alternateRefs: getAlternateRefs(null, false, '', 'ayatul-kursi'),
});
} else {
result.push({
loc: `/${englishChapterSlug}/${verseIdValue}`,
alternateRefs: getAlternateRefs(chapterId, true, '', verseIdValue),
});
}
// 5. add /[chapterId]/[verseId]/tafsirs route
result.push({
loc: `/${englishChapterSlug}/${verseIdValue}/tafsirs`,
alternateRefs: getAlternateRefs(chapterId, true, '', `${verseIdValue}/tafsirs`),
});
// 6. /[verseKey]/tafsirs/[tafsirSlug]
tafsirSlugs.forEach((tafsirSlug) => {
const location = `${verseKey}/tafsirs/${tafsirSlug}`;
result.push({
loc: location,
alternateRefs: getAlternateRefs(chapterId, false, '', location),
});
});
// 7. /[verseKey]/reflections
const reflectionsLocation = `${verseKey}/reflections`;
result.push({
loc: reflectionsLocation,
alternateRefs: getAlternateRefs(chapterId, false, '', reflectionsLocation),
});
});
});
// 7. /juz/[juzId]
range(1, 31).forEach(async (juzId) => {
result.push(await config.transform(config, `/juz/${juzId}`));
});
// 8. /hizb/[hizbId]
range(1, 61).forEach(async (hizbId) => {
result.push(await config.transform(config, `/hizb/${hizbId}`));
});
// 9. /rub/[rubId]
range(1, 241).forEach(async (rubId) => {
result.push(await config.transform(config, `/rub/${rubId}`));
});
// 10. /page/[pageId]
range(1, 605).forEach(async (pageId) => {
result.push(await config.transform(config, `/page/${pageId}`));
});
// 11. /reciters/[reciterId]
reciterIds.forEach((reciterId) => {
const location = `/reciters/${reciterId}`;
result.push({
loc: location,
alternateRefs: getAlternateRefs('', false, '', location),
});
});
return result;
},
};