-
Notifications
You must be signed in to change notification settings - Fork 694
Expand file tree
/
Copy pathhandlers.ts
More file actions
85 lines (73 loc) · 2.96 KB
/
handlers.ts
File metadata and controls
85 lines (73 loc) · 2.96 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
import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
import type { AdBannerProviders } from 'types/client/adProviders';
import type { Route } from 'nextjs-routes';
import config from 'configs/app';
import * as cookies from 'lib/cookies';
import type * as metadata from 'lib/metadata';
import { isLikelyHumanBrowser, isKnownBotRequest } from '../utils/checkRealBrowser';
const adBannerFeature = config.features.adsBanner;
export interface Props<Pathname extends Route['pathname'] = never> {
query: Route['query'];
cookies: string;
referrer: string;
adBannerProvider: AdBannerProviders | null;
// if apiData is undefined, Next.js will complain that it is not serializable
// so we force it to be always present in the props but it can be null
apiData: metadata.ApiData<Pathname> | null;
uuid: string;
}
export const base = async <Pathname extends Route['pathname'] = never>({ req, res, query }: GetServerSidePropsContext):
Promise<GetServerSidePropsResult<Props<Pathname>>> => {
const appProfile = req.headers?.['x-app-profile'] || cookies.getFromCookieString(req.headers.cookie || '', cookies.NAMES.APP_PROFILE);
const adBannerProvider = (() => {
if (adBannerFeature.isEnabled) {
if ('additionalProvider' in adBannerFeature && adBannerFeature.additionalProvider) {
// we need to get a random ad provider on the server side to keep it consistent with the client side
const randomIndex = Math.round(Math.random());
return [ adBannerFeature.provider, adBannerFeature.additionalProvider ][randomIndex];
} else {
return adBannerFeature.provider;
}
}
return null;
})();
let uuid = cookies.getFromCookieString(req.headers.cookie || '', cookies.NAMES.UUID);
if (!uuid && appProfile !== 'private') {
uuid = crypto.randomUUID();
res.setHeader('Set-Cookie', `${ cookies.NAMES.UUID }=${ uuid }; Path=/${ config.app.protocol === 'https' ? '; Secure' : '' }`);
}
const isTrackingDisabled = process.env.DISABLE_TRACKING === 'true' || appProfile === 'private';
if (!isTrackingDisabled) {
const isRealUser = isLikelyHumanBrowser(req) && !isKnownBotRequest(req);
if (isRealUser) {
// log pageview
const hostname = req.headers.host;
const timestamp = new Date().toISOString();
const chainId = process.env.NEXT_PUBLIC_NETWORK_ID;
const chainName = process.env.NEXT_PUBLIC_NETWORK_NAME;
const publicRPC = process.env.NEXT_PUBLIC_NETWORK_RPC_URL;
fetch('https://monitor.blockscout.com/count', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
hostname,
timestamp,
chainId,
chainName,
publicRPC,
uuid,
}),
});
}
}
return {
props: {
query,
cookies: req.headers.cookie || '',
referrer: req.headers.referer || '',
adBannerProvider: adBannerProvider,
apiData: null,
uuid,
},
};
};