-
Notifications
You must be signed in to change notification settings - Fork 1
/
next.config.js
292 lines (270 loc) · 9.47 KB
/
next.config.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE_BUNDLE === 'true',
});
const { buildCspHeader } = require('@navikt/nav-dekoratoren-moduler/ssr');
const { DATA, UNSAFE_INLINE, UNSAFE_EVAL } = require('csp-header');
const path = require('path');
// Remove dashes from js variable names for classnames generated from CSS-modules
// Enables all CSS-classes to be accessed from javascript with dot-notation
const cssModulesNoDashesInClassnames = (config) => {
const rules = config.module.rules
.find((rule) => typeof rule.oneOf === 'object')
.oneOf.filter((rule) => Array.isArray(rule.use));
rules.forEach((rule) => {
rule.use.forEach((moduleLoader) => {
if (/css-loader([\/\\])(cjs|dist|src)/.test(moduleLoader.loader)) {
if (typeof moduleLoader.options.modules === 'object') {
moduleLoader.options.modules = {
...moduleLoader.options.modules,
exportLocalsConvention: 'dashesOnly',
};
}
}
});
});
};
// Prevents errors due to client-side imports of server-side only libraries
const resolveNodeLibsClientSide = (config, options) => {
if (!options.isServer) {
config.resolve.fallback = {
buffer: false,
fs: false,
process: false,
};
}
};
const csp = async () => {
const prodHost = 'nav.no';
const prodWithSubdomains = `*.${prodHost}`;
const appHost = new URL(process.env.APP_ORIGIN).host;
const adminHost = new URL(process.env.ADMIN_ORIGIN).host;
const xpHost = new URL(process.env.XP_ORIGIN).host;
const qbrickHosts = [
'video.qbrick.com',
'play2.qbrick.com',
'analytics.qbrick.com',
'*.ip-only.net',
'blob:',
];
const salesforceVideoHost = 'ihb.nav.no';
// These are used by a Nav-funded research project for accessibility-related feedback
const tingtunHost = '*.tingtun.no';
const termerHost = 'termer.no';
const tiTiHosts = [tingtunHost, termerHost];
const uxSignalsScriptHost = 'uxsignals-frontend.uxsignals.app.iterate.no';
const uxSignalsApiHost = 'api.uxsignals.com';
// Filter duplicates, as some origins may be identical, depending on
// deployment environment
const internalHosts = [
prodWithSubdomains,
...[appHost, adminHost, xpHost].filter(
(origin, index, array) => !origin.endsWith(prodHost) && array.indexOf(origin) === index
),
];
const envMap = {
localhost: 'localhost',
dev1: 'dev',
dev2: 'beta',
prod: 'prod',
};
const scriptSrc = [...internalHosts, ...tiTiHosts, uxSignalsScriptHost];
const directives = {
'default-src': internalHosts,
'script-src': scriptSrc,
'script-src-elem': [...scriptSrc, ...qbrickHosts],
'worker-src': internalHosts,
'style-src': [...internalHosts, UNSAFE_INLINE],
'font-src': [...internalHosts, DATA, ...qbrickHosts],
'img-src': [...internalHosts, DATA, ...qbrickHosts],
'object-src': [...qbrickHosts],
'connect-src': [...internalHosts, ...qbrickHosts, uxSignalsApiHost],
'media-src': [...qbrickHosts, salesforceVideoHost],
};
if (process.env.NODE_ENV === 'development') {
directives['default-src'].push('ws:');
directives['connect-src'].push('ws:');
directives['script-src'].push(UNSAFE_INLINE, UNSAFE_EVAL);
}
return buildCspHeader(directives, {
env: envMap[process.env.ENV],
localUrl: process.env.DECORATOR_URL,
});
};
const isFailover = process.env.IS_FAILOVER_INSTANCE === 'true';
const isLocal = process.env.ENV === 'localhost';
const corsHeaders = [
{
key: 'Access-Control-Allow-Origin',
value: isFailover ? process.env.APP_ORIGIN : process.env.ADMIN_ORIGIN,
},
];
console.log(
`Env: ${process.env.ENV} - Node env: ${process.env.NODE_ENV} - Is failover instance? ${isFailover}`
);
const config = {
...(!isFailover && {
cacheHandler: path.resolve(__dirname, 'server', '.dist', 'page-cache-handler.cjs'),
cacheMaxMemorySize: 0,
}),
experimental: {
optimizePackageImports: [
'@navikt/ds-react',
'@navikt/aksel-icons',
'@navikt/nav-office-reception-info',
],
scrollRestoration: true,
},
transpilePackages: [
'@navikt/aksel-icons',
'@navikt/ds-react',
'@navikt/nav-office-reception-info',
],
productionBrowserSourceMaps: true,
distDir: isFailover && isLocal ? '.next-static' : '.next',
assetPrefix: process.env.ASSET_PREFIX,
env: {
ENV: process.env.ENV,
APP_ORIGIN: process.env.APP_ORIGIN,
XP_ORIGIN: process.env.XP_ORIGIN,
ADMIN_ORIGIN: process.env.ADMIN_ORIGIN,
FAILOVER_ORIGIN: process.env.FAILOVER_ORIGIN,
IS_FAILOVER_INSTANCE: process.env.IS_FAILOVER_INSTANCE,
INNLOGGINGSSTATUS_URL: process.env.INNLOGGINGSSTATUS_URL,
NAVNO_API_URL: process.env.NAVNO_API_URL,
NAVNO_SEARCH_API_URL: process.env.NAVNO_SEARCH_API_URL,
DECORATOR_URL: process.env.DECORATOR_URL,
TELEMETRY_URL: process.env.TELEMETRY_URL,
},
images: {
minimumCacheTTL: isFailover ? 3600 * 24 * 365 : 3600 * 24,
dangerouslyAllowSVG: true,
remotePatterns: [
process.env.APP_ORIGIN,
process.env.XP_ORIGIN,
process.env.ADMIN_ORIGIN,
process.env.FAILOVER_ORIGIN,
].map((origin) => {
const url = new URL(origin);
return {
protocol: url.protocol.replace(':', ''),
hostname: url.hostname,
port: url.port,
};
}),
deviceSizes: [480, 768, 1024, 1440],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
},
webpack: (config, options) => {
cssModulesNoDashesInClassnames(config);
resolveNodeLibsClientSide(config, options);
const { webpack, buildId } = options;
config.plugins.push(
new webpack.DefinePlugin({
'process.env.BUILD_ID': JSON.stringify(buildId),
})
);
return config;
},
redirects: async () => [
{
source: '/forsiden',
destination: '/',
permanent: false,
},
{
source: '/www.nav.no',
destination: '/',
permanent: false,
},
{
source: '/www.nav.no/:path*',
destination: '/:path*',
permanent: false,
},
{
source: '/Global%20Utlogging',
destination: '/global-utlogging',
permanent: true,
},
// Redirects from an old invalid link which must be resolved for the time being
{
source: '/no/nav-og-samfunn/samarbeid/for-kommunen/digisos/til-kommuner-som-onsker-a-ta-i-bruk-digital-soknad-om-okonomisk-sosialhjelp/_/attachment/download/ec690be3-b949-4945-9e73-e7fd7437fc94(:)91ebdeaf85cd092e2c96f4ecc82d5bf88b1838b3/handbok-for-innforing-av-digital-soknad---oppdatert-25.05.2020.pdf',
destination: `${process.env.APP_ORIGIN}/no/nav-og-samfunn/samarbeid/for-kommunen/digisos/til-kommuner-som-onsker-a-ta-i-bruk-digital-soknad-om-okonomisk-sosialhjelp/Håndbok for innføring av digital søknad %2800B%29 12.12.19.pdf`,
permanent: false,
},
// /_/* should point to XP services. Redirect only if XP is on a different origin
...(process.env.XP_ORIGIN !== process.env.APP_ORIGIN
? [
{
source: '/_/:path*',
destination: `${process.env.XP_ORIGIN}/_/:path*`,
permanent: false,
},
]
: []),
],
rewrites: async () => [
{
source: '/sitemap.xml',
destination: '/api/sitemap',
},
{
source: '/rss',
destination: '/api/rss',
},
// The historic url for RSS
{
source: '/no/rss',
destination: '/api/rss',
},
// Send some very common invalid requests directly to 404
{
source: '/autodiscover/autodiscover.xml',
destination: '/404',
},
{
source: '/Forsiden/driftsmelding',
destination: '/404',
},
{
source: '/_public/beta.nav.no/:path*',
destination: '/404',
},
{
source: '/frontendlogger/:path*',
destination: '/404',
},
{
source: '/feilside',
destination: '/404',
},
...(isLocal
? [
{
source: '/admin/site/preview/default/draft/:path*',
destination: 'http://localhost:8080/admin/site/preview/default/draft/:path*',
},
]
: []),
],
headers: async () => [
{
source: '/_next/(.*)',
headers: corsHeaders,
},
{
source: '/api/jsonCache',
headers: corsHeaders,
},
{
source: '/:path*',
headers: [
{
key: 'Content-Security-Policy',
value: await csp(),
},
],
},
],
};
module.exports = withBundleAnalyzer(config);