This repository has been archived by the owner on Apr 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinpoint.next.js
164 lines (155 loc) · 4.2 KB
/
pinpoint.next.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
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
const fs = require('fs');
const path = require('path');
const { apihost, siteId } = require('./pinpoint.config.js');
const homeurl = (apihost || '').includes('.edge.') ? `https://home.edge.pinpoint.com` : `https://home.pinpoint.com`;
const apiRules = [
{
source: '/site-api/v1/feedback/submit',
destination: `https://${apihost}/feedback/${siteId}/submit`,
},
{
source: '/site-api/v1/site',
destination: `https://${apihost}/site/v1/${siteId}`,
},
{
source: '/site-api/v1/site/:slug*',
destination: `https://${apihost}/site/v1/${siteId}/:slug*`,
},
{
source: '/site-api/v1/content',
destination: `https://${apihost}/content/v1/${siteId}`,
},
{
source: '/site-api/v1/content/:slug*',
destination: `https://${apihost}/content/v1/${siteId}/:slug*`,
},
{
source: '/subscription/subscribe',
destination: `/subscribe`,
},
{
source: '/subscription/manage/:subId',
destination: '/subscription/:subId/manage',
},
{
source: '/subscription/unsubscribe/:subId',
destination: '/subscription/:subId/unsubscribe',
},
{
source: '/api/event',
destination: `https://${apihost}/analytics/track`,
},
{
source: '/rss',
destination: `https://${apihost}/rss/${siteId}`,
},
{
source: '/a.js',
destination: `https://cdn.pinpoint.com/beacon2/index.min.js`,
},
{
source: '/api/widget/rules',
destination: `https://${apihost}/widget/trigger/${siteId}`,
},
{
source: '/:any*/api/widget/rules',
destination: `https://${apihost}/widget/trigger/${siteId}`,
},
{
source: '/:any*/pinpt-beacon-module/:slug*',
destination: `https://cdn.pinpoint.com/beacon2/pinpt-beacon-module/:slug*`,
},
{
source: '/:any*/pinpt-beacon-nomodule/:slug*',
destination: `https://cdn.pinpoint.com/beacon2/pinpt-beacon-nomodule/:slug*`,
},
];
const apiRewrites = {
beforeFiles: apiRules,
afterFiles: [],
fallback: [],
};
const cacheableRoutes = ['/', '/search', '/entry/(.*)', '/entries/(.*)'];
const securityHeaders = [
{
key: 'X-DNS-Prefetch-Control',
value: 'on',
},
{
key: 'Strict-Transport-Security',
value: 'max-age=63072000; includeSubDomains; preload',
},
{
key: 'X-XSS-Protection',
value: '1; mode=block',
},
{
key: 'X-Frame-Options',
value: 'SAMEORIGIN',
},
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
];
async function pinpointHeaders() {
if (process.env.NODE_ENV !== 'production') {
return [];
}
return [
// Apply these headers to all routes in your application.
...[{ source: '/(.*)', headers: securityHeaders }],
...cacheableRoutes.map((source) => ({
source,
headers: [
// this header is used by the pinpoint proxy to increase cachability of content at the edge proxies w/o messing with the client or intermediate caches
{
key: 'x-proxy-cache-control',
value: 's-maxage=432000', // 5 days in seconds
},
],
})),
];
}
const createHeaderWrapper = (headers) => {
return async () => {
// invoke the original headers (if any)
const _headers = headers ? await headers() : [];
const _ourheaders = await pinpointHeaders();
// merge in our headers + your headers
return [..._ourheaders, ..._headers];
};
};
const createRewriteWrapper = (rewrites) => {
return async () => {
// invoke the original rewrite (if any)
const _rewrites = rewrites ? await rewrites() : {};
// merge in our rewrites + your rewrites
let _a = _rewrites;
if (Array.isArray(_rewrites)) {
_a = { beforeFiles: _rewrites, afterFiles: [], fallback: [] };
}
let _b = { ...apiRewrites };
const dest = {
fallback: [...(_b.fallback || []), ...(_a.fallback || [])],
beforeFiles: [...(_b.beforeFiles || []), ...(_a.beforeFiles || [])],
afterFiles: [...(_b.afterFiles || []), ...(_a.afterFiles || [])],
};
return dest;
};
};
/**
* @type {import('next').NextConfig}
*/
const withPinpointConfig = (config) => {
const _config = { ...config };
_config.headers = createHeaderWrapper(_config.headers);
_config.rewrites = createRewriteWrapper(_config.rewrites);
// load up build-time config
if (fs.existsSync(path.join(__dirname, 'pinpoint.config.json'))) {
const buildConfig = JSON.parse(fs.readFileSync(path.join(__dirname, 'pinpoint.config.json')).toString());
return { ..._config, ...buildConfig };
}
return _config;
};
module.exports = withPinpointConfig;