This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
next-sitemap.config.js
125 lines (118 loc) · 3.33 KB
/
next-sitemap.config.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
// @ts-check
/**
*
* @returns {Promise<import("@thirdweb-dev/chains").Chain[]>}
*/
async function fetchChainsFromApi() {
const res = await fetch(`https://api.thirdweb.com/v1/chains`, {
headers: {
"Content-Type": "application/json",
},
});
const json = await res.json();
if (json.error) {
throw new Error(json.message);
}
return json.data;
}
/**
*
* @param {number|string} chainIdOrSlug
* @returns {Promise<import("@thirdweb-dev/chains").Chain>}
*/
async function getSingleChain(chainIdOrSlug) {
const res = await fetch(
`https://api.thirdweb.com/v1/chains/${chainIdOrSlug}`,
{
headers: {
"Content-Type": "application/json",
},
},
);
const json = await res.json();
if (json.error) {
throw new Error(json.message);
}
return json.data;
}
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: process.env.SITE_URL || "https://thirdweb.com",
generateRobotsTxt: true,
robotsTxtOptions: {
policies: [
{
userAgent: "*",
[process.env.VERCEL_ENV !== "preview" &&
process.env.VERCEL_ENV !== "development"
? "allow"
: "disallow"]: "/",
},
],
},
exclude: ["/chain/validate"],
transform: async (config, path) => {
// ignore og image paths
if (path.includes("_og")) {
return null;
}
// rewrite paths that include deployer to use thirdweb.eth directly
if (path.includes("deployer.thirdweb.eth")) {
path = path.replace("deployer.thirdweb.eth", "thirdweb.eth");
}
return {
// => this will be exported as http(s)://<config.siteUrl>/<path>
loc: path,
changefreq: config.changefreq,
priority: config.priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
alternateRefs: config.alternateRefs ?? [],
};
},
additionalPaths: async (config) => {
const allChains = await fetchChainsFromApi();
return [
...allChains.map((chain) => {
return {
loc: `/${chain.slug}`,
changefreq: config.changefreq,
priority: config.priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
};
}),
...(await createSearchRecordSitemaps(config)),
];
},
};
/**
* @param {{ changefreq?: any; priority?: any; autoLastmod?: any; }} config
*/
async function createSearchRecordSitemaps(config) {
if (!process.env.TYPESENSE_CONTRACT_EXPORT_API_KEY) {
return [];
}
const response = await fetch(
"https://search.thirdweb.com/collections/contracts/documents/export?filter_by=testnet:false",
{
headers: {
"x-typesense-api-key": process.env.TYPESENSE_CONTRACT_EXPORT_API_KEY,
},
},
);
const data = await response.text();
const parsedLines = data.split("\n").map((l) => JSON.parse(l));
const chainsForLines = await Promise.all(
parsedLines.map((parsedLine) => {
return getSingleChain(parsedLine.chain_id)
.then((parsedLineChain) => ({
loc: `/${parsedLineChain.slug}/${parsedLine.contract_address}`,
changefreq: config.changefreq,
priority: config.priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
}))
.catch(() => null);
}),
);
// filter out any failed requests
return chainsForLines.filter(Boolean);
}