-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnext.config.js
124 lines (116 loc) · 3.23 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
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-param-reassign */
require('dotenv').config();
const path = require('path');
const webpack = require('webpack');
const withSourceMaps = require('@zeit/next-source-maps');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const NextWorkboxPlugin = require('next-workbox-webpack-plugin');
const cached = require('./config/cached');
module.exports = withSourceMaps({
distDir: 'static/dist',
webpack(config, options) {
const { dev, buildId, isServer } = options;
if (!isServer) {
const envVariables = {};
Object.keys(process.env)
.filter(key => /REACT_APP_/.test(key))
.forEach((key) => {
envVariables[key] = process.env[key];
});
config.plugins.push(new webpack.EnvironmentPlugin(envVariables));
}
config.module.rules.push(
// Raster images (png, jpg, etc)
{
test: /\.(png|jpg|jpeg|gif|webp)$/,
exclude: [/\.svg$/, 'client/shared/media/fonts'],
use: [
{
loader: 'file-loader',
options: {
publicPath: '/dist/static/images',
outputPath: 'static/images',
name: dev ? '[name].[ext]' : '[name].[hash:15].[ext]',
},
},
],
},
// SVGs
{
test: /\.svg$/,
oneOf: [
{
include: path.resolve(__dirname, 'client/shared/media/images/backgrounds'),
use: 'url-loader',
},
{
exclude: path.resolve(__dirname, 'client/shared/media/images/backgrounds'),
use: 'svg-inline-loader',
},
],
},
// Web fonts
{
test: /\.(eot|ttf|woff|woff2|otf)$/,
use: [
{
loader: 'file-loader',
options: {
publicPath: '/dist/static/fonts',
outputPath: 'static/fonts',
name: dev ? '[name].[ext]' : '[name].[hash:15].[ext]',
},
},
],
},
);
const workboxOptions = {
clientsClaim: true,
removeDir: true,
skipWaiting: true,
globPatterns: ['.next/static/*', '.next/static/commons/*'],
modifyUrlPrefix: {
'.next': '/_next',
},
runtimeCaching: [
...Object.values(cached.pageRoutes).map(route => ({
urlPattern: route,
handler: 'networkFirst',
options: {
cacheName: 'html-cache',
},
})),
{
urlPattern: cached.stylesRoutes,
handler: 'networkFirst',
options: {
cacheName: 'css-cache',
},
},
{
urlPattern: cached.staticRoutes,
handler: 'cacheFirst',
options: {
cacheName: 'image-cache',
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
};
if (!isServer && !dev) {
config.plugins.push(
new NextWorkboxPlugin({
buildId,
...workboxOptions,
}),
);
}
if (process.env.ANALYZE && !isServer) {
config.plugins.push(new BundleAnalyzerPlugin({ analyzerPort: 3002 }));
}
return config;
},
});