-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathnext.config.ts
More file actions
147 lines (132 loc) · 4.95 KB
/
next.config.ts
File metadata and controls
147 lines (132 loc) · 4.95 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
141
142
143
144
145
146
147
import { withSentryConfig } from "@sentry/nextjs";
import type { NextConfig } from "next";
import list from "./list.json";
const nextConfig: NextConfig = {
output: "standalone",
basePath: "",
images: {
remotePatterns: [
{
protocol: "https",
hostname: "api.apis.guru",
pathname: "/**",
},
{
protocol: "https",
hostname: "cdn.sanity.io",
pathname: "/**",
},
],
},
webpack: (config, { webpack }) => {
config.plugins.push(
new webpack.DefinePlugin({
__SENTRY_DEBUG__: false,
__SENTRY_TRACING__: false,
__RRWEB_EXCLUDE_IFRAME__: true,
__RRWEB_EXCLUDE_SHADOW_DOM__: true,
__SENTRY_EXCLUDE_REPLAY_WORKER__: true,
})
);
return config;
},
async redirects() {
const redirectList = [];
// Normalizes text to a URL-friendly slug
// e.g., "Example API" -> "example-api"
// With preserveDots=true: "twilio.com" -> "twilio.com"
const normalizeSlug = (text: string, preserveDots = false) => {
return text
.toLowerCase()
.replace(preserveDots ? /[^a-z0-9.]+/g : /[^a-z0-9]+/g, "-")
.replace(/-+/g, "-")
.replace(/^-+|-+$/g, "");
};
// Converts service name to legacy format (all special chars become dashes)
// e.g., "twilio_media_v1" -> "twilio-media-v1"
// e.g., "subscriptions-api-(v2)" -> "subscriptions-api-v2"
const toLegacyServiceSlug = (service: string) => {
return service
.toLowerCase()
.replace(/[\(\)]/g, "")
.replace(/[^a-z0-9]+/g, "-")
.replace(/-+/g, "-")
.replace(/^-+|-+$/g, "");
};
// Normalizes service slug for current URLs (removes parentheses, preserves underscores)
// e.g., "twilio_media_v1" -> "twilio_media_v1"
// e.g., "subscriptions-api-(v2)" -> "subscriptions-api-v2"
const toCurrentServiceSlug = (service: string) => {
return service
.toLowerCase()
.replace(/[\(\)]/g, "")
.replace(/--+/g, "-")
.replace(/^-+|-+$/g, "");
};
for (const key in list) {
if (!Object.prototype.hasOwnProperty.call(list, key)) continue;
// Parse the key format "provider:service" or just "provider"
const [provider, service] = key.split(":");
if (!provider) {
console.warn(`Invalid key format: ${key}`);
continue;
}
// Generate different slug formats for redirects
// e.g., key="twilio.com:twilio_media_v1"
const legacyFullSlug = normalizeSlug(key); // "twilio-com-twilio-media-v1"
const currentProviderSlug = normalizeSlug(provider, true); // "twilio.com"
const legacyProviderSlug = normalizeSlug(provider.replace(/\./g, "-")); // "twilio-com"
const currentServiceSlug = service ? toCurrentServiceSlug(service) : null; // "twilio_media_v1"
const legacyServiceSlug = service ? toLegacyServiceSlug(service) : null; // "twilio-media-v1"
// Build the canonical destination URL
const destination = service
? `/apis/${encodeURIComponent(currentProviderSlug)}/${encodeURIComponent(currentServiceSlug!)}`
: `/apis/${encodeURIComponent(currentProviderSlug)}`;
// Redirect duplicate provider-only URLs
// e.g., /apis/example.com/example.com -> /apis/example.com
if (!service) {
redirectList.push({
source: `/apis/${encodeURIComponent(currentProviderSlug)}/${encodeURIComponent(currentProviderSlug)}`,
destination: `/apis/${encodeURIComponent(currentProviderSlug)}`,
permanent: true,
});
}
// Redirect from legacy service slug (dashes) to current service slug (underscores preserved)
// e.g., /apis/twilio.com/twilio-media-v1 -> /apis/twilio.com/twilio_media_v1
if (service && legacyServiceSlug !== currentServiceSlug) {
redirectList.push({
source: `/apis/${encodeURIComponent(currentProviderSlug)}/${encodeURIComponent(legacyServiceSlug!)}`,
destination,
permanent: true,
});
}
// Redirect from legacy full slug format
// e.g., /apis/twilio-com-twilio-media-v1 -> /apis/twilio.com/twilio_media_v1
if (legacyFullSlug !== currentProviderSlug) {
redirectList.push({
source: `/apis/${encodeURIComponent(legacyFullSlug)}`,
destination,
permanent: true,
});
}
// Redirect from legacy provider slug (dots replaced with dashes)
// e.g., /apis/twilio-com -> /apis/twilio.com
if (legacyProviderSlug !== currentProviderSlug) {
redirectList.push({
source: `/apis/${encodeURIComponent(legacyProviderSlug)}`,
destination: `/apis/${encodeURIComponent(currentProviderSlug)}`,
permanent: true,
});
}
}
return redirectList;
},
};
export default withSentryConfig(nextConfig, {
org: "apisguru",
project: "apis-guru",
silent: !process.env.CI,
widenClientFileUpload: true,
disableLogger: true,
automaticVercelMonitors: true,
});