forked from blockscout/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
63 lines (48 loc) · 1.81 KB
/
proxy.ts
File metadata and controls
63 lines (48 loc) · 1.81 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
// SPDX-License-Identifier: LicenseRef-Blockscout
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { CSP_NONCE_HEADER } from 'nextjs/constants';
import * as csp from 'nextjs/csp/index';
import * as middlewares from 'nextjs/middlewares/index';
import appConfig from 'configs/app';
const adsBannerFeature = appConfig.features.adsBanner;
const adsTextFeature = appConfig.features.adsText;
const shouldUseCspNonce =
(adsBannerFeature.isEnabled && adsBannerFeature.provider === 'sevio') ||
(adsTextFeature.isEnabled && adsTextFeature.provider === 'sevio');
export async function proxy(req: NextRequest) {
const isPageRequest = req.headers.get('accept')?.includes('text/html');
const start = Date.now();
if (!isPageRequest) {
return;
}
const accountResponse = middlewares.account(req);
if (accountResponse) {
return accountResponse;
}
const res = NextResponse.next();
middlewares.appProfile(req, res);
middlewares.colorTheme(req, res);
middlewares.addressFormat(req, res);
middlewares.scamTokens(req, res);
middlewares.poorReputationTokens(req, res);
const end = Date.now();
const nonce = shouldUseCspNonce ? crypto.randomUUID().replaceAll('-', '') : undefined;
const cspHeader = await csp.get(req, nonce);
res.headers.append('Content-Security-Policy', cspHeader);
res.headers.append('Server-Timing', `middleware;dur=${ end - start }`);
res.headers.append('Docker-ID', process.env.HOSTNAME || '');
if (nonce) {
res.headers.append(CSP_NONCE_HEADER, nonce);
}
return res;
}
/**
* Configure which routes should pass through the proxy.
*/
export const config = {
matcher: [ '/', '/:notunderscore((?!_next).+)' ],
// matcher: [
// '/((?!.*\\.|api\\/|node-api\\/).*)', // exclude all static + api + node-api routes
// ],
};