forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.ts
245 lines (226 loc) · 6.98 KB
/
sitemap.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { flatten } from 'lodash-es';
import { MetadataRoute } from 'next';
import qs from 'query-string';
import urlJoin from 'url-join';
import { serverFeatureFlags } from '@/config/featureFlags';
import { DEFAULT_LANG } from '@/const/locale';
import { SITEMAP_BASE_URL } from '@/const/url';
import { Locales, locales as allLocales } from '@/locales/resources';
import { DiscoverService } from '@/server/services/discover';
import { getCanonicalUrl } from '@/server/utils/url';
import { AssistantCategory, PluginCategory } from '@/types/discover';
import { isDev } from '@/utils/env';
export interface SitemapItem {
alternates?: {
languages?: string;
};
changeFrequency?: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never';
lastModified?: string | Date;
priority?: number;
url: string;
}
export enum SitemapType {
Assistants = 'assistants',
Models = 'models',
Pages = 'pages',
Plugins = 'plugins',
Providers = 'providers',
}
export const LAST_MODIFIED = new Date().toISOString();
export class Sitemap {
sitemapIndexs = [
{ id: SitemapType.Pages },
{ id: SitemapType.Assistants },
{ id: SitemapType.Plugins },
{ id: SitemapType.Models },
{ id: SitemapType.Providers },
];
private discoverService = new DiscoverService();
private _generateSitemapLink(url: string) {
return [
'<sitemap>',
`<loc>${url}</loc>`,
`<lastmod>${LAST_MODIFIED}</lastmod>`,
'</sitemap>',
].join('\n');
}
private _formatTime(time?: string) {
try {
if (!time) return LAST_MODIFIED;
return new Date(time).toISOString() || LAST_MODIFIED;
} catch {
return LAST_MODIFIED;
}
}
private _genSitemapItem = (
lang: Locales,
url: string,
{
lastModified,
changeFrequency = 'monthly',
priority = 0.4,
noLocales,
locales = allLocales,
}: {
changeFrequency?: SitemapItem['changeFrequency'];
lastModified?: string;
locales?: typeof allLocales;
noLocales?: boolean;
priority?: number;
} = {},
) => {
const sitemap = {
changeFrequency,
lastModified: this._formatTime(lastModified),
priority,
url:
lang === DEFAULT_LANG
? getCanonicalUrl(url)
: qs.stringifyUrl({ query: { hl: lang }, url: getCanonicalUrl(url) }),
};
if (noLocales) return sitemap;
const languages: any = {};
for (const locale of locales) {
if (locale === lang) continue;
languages[locale] = qs.stringifyUrl({
query: { hl: locale },
url: getCanonicalUrl(url),
});
}
return {
alternates: {
languages,
},
...sitemap,
};
};
private _genSitemap(
url: string,
{
lastModified,
changeFrequency = 'monthly',
priority = 0.4,
noLocales,
locales = allLocales,
}: {
changeFrequency?: SitemapItem['changeFrequency'];
lastModified?: string;
locales?: typeof allLocales;
noLocales?: boolean;
priority?: number;
} = {},
) {
if (noLocales)
return [
this._genSitemapItem(DEFAULT_LANG, url, {
changeFrequency,
lastModified,
locales,
noLocales,
priority,
}),
];
return locales.map((lang) =>
this._genSitemapItem(lang, url, {
changeFrequency,
lastModified,
locales,
noLocales,
priority,
}),
);
}
getIndex(): string {
return [
'<?xml version="1.0" encoding="UTF-8"?>',
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
...this.sitemapIndexs.map((item) =>
this._generateSitemapLink(
getCanonicalUrl(SITEMAP_BASE_URL, isDev ? item.id : `${item.id}.xml`),
),
),
'</sitemapindex>',
].join('\n');
}
async getAssistants(): Promise<MetadataRoute.Sitemap> {
const list = await this.discoverService.getAssistantList(DEFAULT_LANG);
const sitmap = list.map((item) =>
this._genSitemap(urlJoin('/discover/assistant', item.identifier), {
lastModified: item?.createdAt || LAST_MODIFIED,
}),
);
return flatten(sitmap);
}
async getPlugins(): Promise<MetadataRoute.Sitemap> {
const list = await this.discoverService.getPluginList(DEFAULT_LANG);
const sitmap = list.map((item) =>
this._genSitemap(urlJoin('/discover/plugin', item.identifier), {
lastModified: item?.createdAt || LAST_MODIFIED,
}),
);
return flatten(sitmap);
}
async getModels(): Promise<MetadataRoute.Sitemap> {
const list = await this.discoverService.getModelList(DEFAULT_LANG);
const sitmap = list.map((item) =>
this._genSitemap(urlJoin('/discover/model', item.identifier), {
lastModified: item?.createdAt || LAST_MODIFIED,
}),
);
return flatten(sitmap);
}
async getProviders(): Promise<MetadataRoute.Sitemap> {
const list = await this.discoverService.getProviderList(DEFAULT_LANG);
const sitmap = list.map((item) =>
this._genSitemap(urlJoin('/discover/provider', item.identifier), {
lastModified: item?.createdAt || LAST_MODIFIED,
}),
);
return flatten(sitmap);
}
async getPage(): Promise<MetadataRoute.Sitemap> {
const hideDocs = serverFeatureFlags().hideDocs;
const assistantsCategory = Object.values(AssistantCategory);
const pluginCategory = Object.values(PluginCategory);
const modelCategory = await this.discoverService.getProviderList(DEFAULT_LANG);
return [
...this._genSitemap('/', { noLocales: true }),
...this._genSitemap('/chat', { noLocales: true }),
...(!hideDocs ? this._genSitemap('/changelog', { noLocales: true }) : []),
/* ↓ cloud slot ↓ */
/* ↑ cloud slot ↑ */
...this._genSitemap('/discover', { changeFrequency: 'daily', priority: 0.7 }),
...this._genSitemap('/discover/assistants', { changeFrequency: 'daily', priority: 0.7 }),
...assistantsCategory.flatMap((slug) =>
this._genSitemap(`/discover/assistants/${slug}`, {
changeFrequency: 'daily',
priority: 0.7,
}),
),
...this._genSitemap('/discover/plugins', { changeFrequency: 'daily', priority: 0.7 }),
...pluginCategory.flatMap((slug) =>
this._genSitemap(`/discover/plugins/${slug}`, {
changeFrequency: 'daily',
priority: 0.7,
}),
),
...this._genSitemap('/discover/models', { changeFrequency: 'daily', priority: 0.7 }),
...modelCategory.flatMap((slug) =>
this._genSitemap(`/discover/models/${slug}`, {
changeFrequency: 'daily',
priority: 0.7,
}),
),
...this._genSitemap('/discover/providers', { changeFrequency: 'daily', priority: 0.7 }),
].filter(Boolean);
}
getRobots() {
return [
getCanonicalUrl('/sitemap-index.xml'),
...this.sitemapIndexs.map((index) =>
getCanonicalUrl(SITEMAP_BASE_URL, isDev ? index.id : `${index.id}.xml`),
),
];
}
}
export const sitemapModule = new Sitemap();