Repository navigation
Expand file tree
/
Copy pathnext.config.js
More file actions
230 lines (221 loc) · 7.76 KB
/
Copy pathnext.config.js
File metadata and controls
230 lines (221 loc) · 7.76 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
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
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true"
});
/**
* @type {import('next').NextConfig}
*/
const config = {
productionBrowserSourceMaps: true,
env: {
// make the backend URL available to the frontend
NEXT_PUBLIC_BACKEND_URL: process.env.BACKEND_URL,
NEXT_PUBLIC_AUDIT_REPORT_URL:
process.env.NEXT_PUBLIC_AUDIT_REPORT_URL ||
"https://audit-report.app.flanksource.com",
NEXT_PUBLIC_ACCOUNTS_URL: "https://accounts.flanksource.com"
},
headers: async () => [
{
source: "/:path*",
headers: [
{
key: "X-Content-Type-Options",
value: "nosniff"
}
]
}
],
redirects: async () => {
if (process.env.NEXT_PUBLIC_APP_DEPLOYMENT === "CANARY_CHECKER") {
return [];
}
// Redirect early, for a better user experience
return [
{
source: "/config/:path*",
destination: "/catalog/:path*",
permanent: true
},
{
source: "/settings/feature-flags",
destination: "/settings/properties",
permanent: true
},
{
source: "/settings/notifications/:path*",
destination: "/notifications/:path*",
permanent: true
},
{
source: "/notifications/silence",
destination: "/notifications/silences/add",
permanent: true
}
];
},
async rewrites() {
const isClerkAuth = process.env.NEXT_PUBLIC_AUTH_IS_CLERK === "true";
const isBasicAuth = process.env.NEXT_PUBLIC_AUTH_IS_BASIC === "true";
// Read at build time. See Dockerfile for deployment related steps.
const backendURL = process.env.BACKEND_URL || "http://localhost:3000/";
const isCanary =
process.env.NEXT_PUBLIC_APP_DEPLOYMENT === "CANARY_CHECKER";
const canaryPrefix = isCanary ? "" : "/canary";
// The backend hosts its own static UI server at /ui. Proxy through so the
// browser can reach it without auth interference, in every deployment mode.
// BACKEND_URL may be set with a trailing /api (e.g. when pointed at another
// Next.js deployment that exposes the backend under /api); strip it so /ui
// lands on the backend's own /ui rather than its /api/ui.
// Two entries are needed: /:path* expansion adds a trailing slash when path
// is empty, which the upstream's trailingSlash:false redirects back, looping.
const uiHost = backendURL.replace(/\/api\/?$/, "");
const UI_REWRITES = [
{ source: "/ui", destination: `${uiHost}/ui` },
{ source: "/ui/:path*", destination: `${uiHost}/ui/:path*` }
];
// The /ui app (served from the backend) makes XHRs to bare backend paths
// (no /api prefix). Match by Referer so requests originating from /ui or
// /api/ui get proxied to the backend, while same-path requests from the
// main app fall through to Next.js routing.
const UI_REFERER_HAS = [
{
type: "header",
key: "referer",
value: "^https?://[^/]+(/api)?/ui([/?#].*)?$"
}
];
const localhostUiRefererRewrite = {
source: "/:path*",
has: UI_REFERER_HAS,
destination: `${backendURL}/api/:path*`
};
const prodUiRefererRewrite = {
source: "/:path*",
has: UI_REFERER_HAS,
destination: `${backendURL}/:path*`
};
const API_PLUGINS_REWRITES = [
{
source: "/api/plugins/:name/assets",
destination: `${backendURL}/api/plugins/:name/ui/assets`
},
{
source: "/api/plugins/:name/assets/:path*",
destination: `${backendURL}/api/plugins/:name/ui/assets/:path*`
},
{
source: "/api/plugins",
destination: `${backendURL}/api/plugins`
},
{
source: "/api/plugins/:path*",
destination: `${backendURL}/api/plugins/:path*`
}
];
// OIDC protocol endpoints are mounted at the root of the backend (matching the
// issuer URL). These rewrites let the browser reach those endpoints through the
// Next.js server without authentication interference.
const OIDC_REWRITES = [
{
source: "/.well-known/:path*",
destination: `${backendURL}/.well-known/:path*`
},
{ source: "/authorize", destination: `${backendURL}/authorize` },
{
source: "/authorize/:path*",
destination: `${backendURL}/authorize/:path*`
},
{ source: "/oauth/token", destination: `${backendURL}/oauth/token` },
{
source: "/oauth/introspect",
destination: `${backendURL}/oauth/introspect`
},
{ source: "/userinfo", destination: `${backendURL}/userinfo` },
{ source: "/revoke", destination: `${backendURL}/revoke` },
{
source: "/device_authorization",
destination: `${backendURL}/device_authorization`
},
{ source: "/keys", destination: `${backendURL}/keys` },
{ source: "/end_session", destination: `${backendURL}/end_session` },
// Some clients use /endsession (no underscore) — proxy both.
{ source: "/endsession", destination: `${backendURL}/endsession` },
// All OIDC sub-routes (login, callback, …)
{ source: "/oidc/:path*", destination: `${backendURL}/oidc/:path*` },
// MCP transport endpoint
{ source: "/mcp", destination: `${backendURL}/mcp` },
{ source: "/mcp/:path*", destination: `${backendURL}/mcp/:path*` }
];
// clerk and basic auth use next API routes for app endpoints, but OIDC protocol
// endpoints still need explicit rewrites.
if (isClerkAuth || isBasicAuth) {
// Route via internal /api so pages/api/[...paths].ts can resolve the
// per-org backend at request time (Clerk publicMetadata.backend_url).
// Static destinations would pin to build-time BACKEND_URL, which is
// private/unset on multi-tenant Clerk and yields DNS_HOSTNAME_RESOLVED_PRIVATE.
return [
{ source: "/ui", destination: "/api/ui" },
{ source: "/ui/:path*", destination: "/api/ui/:path*" },
...OIDC_REWRITES,
{
source: "/:path*",
has: UI_REFERER_HAS,
destination: "/api/:path*"
}
];
}
const LOCALHOST_ENV_URL_REWRITES = [
...API_PLUGINS_REWRITES,
{
source: "/api/:path*",
destination: `${backendURL}/api/:path*`
},
...UI_REWRITES,
...OIDC_REWRITES,
localhostUiRefererRewrite
];
const URL_REWRITES = [
{
source: "/api/canary/:path*",
destination: `${backendURL}/${canaryPrefix}/:path*`
},
{
source: "/api/.ory/:path*",
destination: `${backendURL}/kratos/:path*`
},
...API_PLUGINS_REWRITES,
// All other API requests are proxied to the backend on the same path
// as the request.
{
source: "/api/:path*",
destination: `${backendURL}/:path*`
},
...UI_REWRITES,
...OIDC_REWRITES,
prodUiRefererRewrite
];
// NODE_ENV is set to "development" when running locally, so we can use it
// to determine if we are running in a local environment.
const rewrites = ["localhost", "netlify", "development"].includes(
process.env.ENV || process.env.NODE_ENV
)
? LOCALHOST_ENV_URL_REWRITES
: URL_REWRITES;
return rewrites;
},
// https://github.com/vercel/next.js/tree/canary/examples/with-docker#in-existing-projects
...(process.env.NEXT_STANDALONE_DEPLOYMENT === "true"
? {
output: "standalone"
}
: {}),
experimental: {
// increase the default timeout for the proxy from 30s to 10m to allow for
// long running requests to the backend
proxyTimeout: 1000 * 60 * 10,
esmExternals: "loose",
optimizePackageImports: ["@flanksource/icons"]
},
transpilePackages: ["monaco-editor", "react-gauge-component"]
};
module.exports = withBundleAnalyzer(config);