Skip to content

Commit 51c378a

Browse files
committed
Move isSitemapEnabled to utils, restructure the current dynamic entity paginated sitemap, add usecases and contributors
1 parent 629cd5c commit 51c378a

File tree

4 files changed

+101
-73
lines changed

4 files changed

+101
-73
lines changed

app/robots.txt/route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// app/robots.txt/route.ts
2+
import { isSitemapEnabled } from '@/lib/utils';
3+
24
export async function GET() {
35
const baseUrl = process.env.NEXTAUTH_URL;
46

57
const robotsTxt = `User-agent: *
68
Allow: /
79
8-
Sitemap: ${baseUrl}/sitemap.xml
10+
Sitemap: ${baseUrl}/sitemap/main.xml
911
`;
1012

13+
if (!isSitemapEnabled()) {
14+
return new Response('Sitemaps are not enabled', { status: 404 });
15+
}
16+
1117
return new Response(robotsTxt, {
1218
headers: {
1319
'Content-Type': 'text/plain',
Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,42 @@
11
// app/sitemap-[entity]-[page].xml/route.ts
22
import { type NextRequest } from 'next/server';
33

4-
import { ENTITY_CONFIG, getSiteMapConfig } from '@/lib/utils';
5-
import {
6-
getGraphqlEntityCount,
7-
getSearchEntityCount,
8-
} from '../sitemap.xml/route';
9-
10-
// Check if sitemaps are enabled
11-
const isSitemapEnabled = () => {
12-
return (
13-
process.env.FEATURE_SITEMAPS === 'true' ||
14-
process.env.NODE_ENV === 'production'
15-
);
16-
};
4+
import { ENTITY_CONFIG, getSiteMapConfig, isSitemapEnabled } from '@/lib/utils';
5+
import { getGraphqlEntityCount, getSearchEntityCount } from '../main.xml/route';
176

187
interface EntityItem {
198
id: string;
209
slug?: string;
2110
updated_at?: string;
11+
__typename?: 'TypeUser' | 'TypeOrganization';
2212
}
2313

2414
async function fetchEntityData(
2515
entity: string,
2616
page: number
2717
): Promise<EntityItem[]> {
2818
const config = ENTITY_CONFIG[entity];
19+
20+
// If no config is found, return empty array
2921
if (!config) return [];
22+
3023
if (config.source === 'search') {
31-
const response = await getSearchEntityCount(entity, 5, page);
24+
// Fetch entity based on general rest query
25+
const response = await getSearchEntityCount(
26+
entity,
27+
getSiteMapConfig().itemsPerPage,
28+
page
29+
);
3230
if (!response || !response.list) return [];
33-
console.log(entity, response);
3431
return response.list;
3532
} else if (config.source === 'graphql') {
33+
// Fetch entity based on graphql query
3634
const response = await getGraphqlEntityCount(entity, config);
3735
if (!response || !response.list) return [];
38-
console.log(entity, response);
3936
return response.list;
4037
} else {
4138
return [];
4239
}
43-
// try {
44-
// const flags = getSiteMapConfig();
45-
// const response = await fetch(
46-
// `${process.env.FEATURE_SITEMAP_BACKEND_BASE_URL}${config.endpoint}?page=${page}&page_size=${flags.itemsPerPage}`,
47-
// {
48-
// method: 'GET',
49-
// headers: {
50-
// 'Content-Type': 'application/json',
51-
// },
52-
// next: { revalidate: flags.childCacheDuration },
53-
// }
54-
// );
55-
56-
// if (!response.ok) return [];
57-
58-
// const data = await response.json();
59-
// return data.results || data;
6040
}
6141

6242
function generateEntitySitemap(items: EntityItem[], entity: string): string {
@@ -71,7 +51,20 @@ function generateEntitySitemap(items: EntityItem[], entity: string): string {
7151

7252
const urls = items
7353
?.map((item) => {
74-
const loc = `${baseUrl}/${config.path}/${item.slug || item.id}`;
54+
console.log(item, entity);
55+
56+
// Function to handle loc or URLs for different types of entities especially for contributors or organizations
57+
const getLoc = () => {
58+
if (item.__typename === 'TypeOrganization') {
59+
return `${baseUrl}/${config.path}/organization/${item.id}`;
60+
} else if (item.__typename === 'TypeUser') {
61+
return `${baseUrl}/${config.path}/${item.id}`;
62+
} else {
63+
return `${baseUrl}/${config.path}/${item.slug || item.id}`;
64+
}
65+
};
66+
67+
const loc = getLoc();
7568
const lastmod = item.updated_at
7669
? new Date(item.updated_at).toISOString()
7770
: new Date().toISOString();
@@ -87,27 +80,33 @@ function generateEntitySitemap(items: EntityItem[], entity: string): string {
8780
})
8881
.join('');
8982

90-
return `<?xml version="1.0" encoding="UTF-8"?>
91-
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls}\n</urlset>`;
83+
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls}\n</urlset>`;
9284
}
9385

9486
export async function GET(
9587
request: NextRequest,
96-
{ params }: { params: { entity: string; page: string } }
88+
{ params }: { params: { entityPage: string } }
9789
) {
9890
// Check if sitemaps are enabled via feature flag
9991
if (!isSitemapEnabled()) {
10092
return new Response('Sitemaps are not enabled', { status: 404 });
10193
}
10294

10395
try {
104-
const { entity, page } = params;
96+
const { entityPage } = params;
97+
98+
const m = entityPage.match(/^([a-zA-Z0-9_]+)-(\d+)\.xml$/);
99+
if (!m) {
100+
return new Response('Invalid Route', { status: 404 });
101+
}
102+
103+
const entity = m[1];
104+
const pageNumber = Number(m[2]);
105105

106106
if (!ENTITY_CONFIG[entity]) {
107107
return new Response('Entity not found', { status: 404 });
108108
}
109109

110-
const pageNumber = parseInt(page);
111110
if (isNaN(pageNumber) || pageNumber < 1) {
112111
return new Response('Invalid page number', { status: 400 });
113112
}
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ import {
55
ENTITY_CONFIG,
66
ENTITY_CONFIG_TYPE,
77
getSiteMapConfig,
8+
isSitemapEnabled,
89
} from '@/lib/utils';
910

10-
// Check if sitemaps are enabled
11-
const isSitemapEnabled = () => {
12-
return (
13-
process.env.FEATURE_SITEMAPS === 'true' ||
14-
process.env.NODE_ENV === 'production'
15-
);
16-
};
17-
1811
const getAllEntityCounts = async (): Promise<Record<string, number>> => {
1912
const counts: Record<string, number> = {};
2013

@@ -57,6 +50,7 @@ export async function getGraphqlEntityCount(
5750
}
5851
);
5952
const data = await response.json();
53+
6054
return {
6155
entityName: entity,
6256
count: data?.data?.[config.queryResKey as string]?.length || 0,
@@ -74,8 +68,9 @@ export async function getSearchEntityCount(
7468
page: number
7569
): Promise<{ entityName: string; count: number; list: any }> {
7670
try {
71+
const config = ENTITY_CONFIG[entity];
7772
const response = await fetch(
78-
`${process.env.FEATURE_SITEMAP_BACKEND_BASE_URL}/search/dataset/?sort=recent&size=${size}&page=${page}`,
73+
`${process.env.FEATURE_SITEMAP_BACKEND_BASE_URL}${config.endpoint}?sort=recent&size=${size}&page=${page}`,
7974
{
8075
method: 'GET',
8176
headers: {
@@ -130,7 +125,8 @@ function generateSitemapIndex(
130125
)
131126
.join('');
132127

133-
return `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${staticUrls}\n${sitemapEntries}\n</urlset>`;
128+
return `<?xml version="1.0" encoding="UTF-8"?>\n
129+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${staticUrls}\n${sitemapEntries}\n</urlset>`;
134130
}
135131

136132
export async function GET(request: NextRequest) {
@@ -159,27 +155,29 @@ export async function GET(request: NextRequest) {
159155
if (entityCounts.datasets > 0) {
160156
const datasetPages = Math.ceil(entityCounts.datasets / ITEMS_PER_SITEMAP);
161157
for (let i = 1; i <= datasetPages; i++) {
162-
sitemapUrls.push(`${baseUrl}/sitemap-datasets-${i}.xml`);
158+
sitemapUrls.push(`${baseUrl}/sitemap/datasets-${i}.xml`);
163159
}
164160
}
165161

166162
// Usecases sitemaps
167-
// const usecasePages = Math.ceil(usecasesCount / ITEMS_PER_SITEMAP);
168-
// for (let i = 1; i <= usecasePages; i++) {
169-
// sitemapUrls.push(`${baseUrl}/sitemap-usecases-${i}.xml`);
170-
// }
163+
const usecasePages = Math.ceil(entityCounts.usecases / ITEMS_PER_SITEMAP);
164+
for (let i = 1; i <= usecasePages; i++) {
165+
sitemapUrls.push(`${baseUrl}/sitemap/usecases-${i}.xml`);
166+
}
171167

172-
// // Publishers sitemaps
173-
// const publisherPages = Math.ceil(publishersCount / ITEMS_PER_SITEMAP);
174-
// for (let i = 1; i <= publisherPages; i++) {
175-
// sitemapUrls.push(`${baseUrl}/sitemap-publishers-${i}.xml`);
176-
// }
168+
// Contributors sitemaps
169+
const contributorPages = Math.ceil(
170+
entityCounts.contributors / ITEMS_PER_SITEMAP
171+
);
172+
for (let i = 1; i <= contributorPages; i++) {
173+
sitemapUrls.push(`${baseUrl}/sitemap/contributors-${i}.xml`);
174+
}
177175

178176
// Sectors sitemaps
179177
if (entityCounts.sectors > 0) {
180178
const sectorPages = Math.ceil(entityCounts.sectors / ITEMS_PER_SITEMAP);
181179
for (let i = 1; i <= sectorPages; i++) {
182-
sitemapUrls.push(`${baseUrl}/sitemap-sectors-${i}.xml`);
180+
sitemapUrls.push(`${baseUrl}/sitemap/sectors-${i}.xml`);
183181
}
184182
}
185183

lib/utils.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ export type ENTITY_CONFIG_TYPE = Record<
197197
}
198198
>;
199199

200+
// Check if sitemap is enabled
201+
export const isSitemapEnabled = () => {
202+
return (
203+
process.env.FEATURE_SITEMAPS === 'true' ||
204+
process.env.NODE_ENV === 'production'
205+
);
206+
};
207+
200208
// Entity Config
201209
export const ENTITY_CONFIG: ENTITY_CONFIG_TYPE = {
202210
datasets: {
@@ -206,18 +214,35 @@ export const ENTITY_CONFIG: ENTITY_CONFIG_TYPE = {
206214
path: 'datasets',
207215
priority: '0.8',
208216
},
209-
// usecases: {
210-
// source: 'graphql',
211-
// graphqlQuery: 'usecases',
212-
// path: '/usecases',
213-
// priority: '0.7',
214-
// },
215-
// publishers: {
216-
// source: 'graphql',
217-
// graphqlQuery: 'publishers',
218-
// path: '/publishers',
219-
// priority: '0.6',
220-
// },
217+
usecases: {
218+
source: 'graphql',
219+
graphqlQuery: `query UseCasesList {
220+
useCases {
221+
id
222+
slug
223+
}
224+
}`,
225+
queryResKey: 'useCases',
226+
path: 'usecases',
227+
priority: '0.7',
228+
},
229+
contributors: {
230+
source: 'graphql',
231+
graphqlQuery: `query getContributors {
232+
getPublishers {
233+
__typename
234+
... on TypeOrganization {
235+
id
236+
}
237+
... on TypeUser {
238+
id
239+
}
240+
}
241+
}`,
242+
queryResKey: 'getPublishers',
243+
path: 'publishers',
244+
priority: '0.6',
245+
},
221246
sectors: {
222247
source: 'graphql',
223248
graphqlQuery: `query SectorsLists {

0 commit comments

Comments
 (0)