-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnext.config.ts
214 lines (204 loc) · 6.47 KB
/
next.config.ts
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
import withSerwistInit from "@serwist/next";
import type {NextConfig} from "next";
import {isTruthy} from "./utils/type-guards.ts";
interface CrossOriginResourcesConfig {
vercel: boolean;
vercelAnalytics: boolean;
}
type ContentSecurityPolicyConfig = Record<string, (string | false)[]>;
type PermissionsPolicyConfig = Record<string, (string | false)[]>;
const createPermissionsPolicy = (
crossOriginResources: CrossOriginResourcesConfig | undefined,
customRules: PermissionsPolicyConfig | undefined,
) => {
const rules = {
// Custom rules
...customRules,
"fullscreen": [
// Videos hosted on own domain
"self",
...customRules?.fullscreen ?? [],
],
};
const permissions: string[] = [];
for (const [key, rule] of Object.entries(rules)) {
const origins = (
Array.from(new Set(rule))
.filter(isTruthy)
.map(origin => origin === "self" ? origin : `"${origin}"`)
);
permissions.push(`${key}=(${origins.join(" ")})`);
}
return {
key: "Permissions-Policy",
value: permissions.join(", "),
};
};
const createContentSecurityPolicy = (
crossOriginResources: CrossOriginResourcesConfig | undefined,
customRules: ContentSecurityPolicyConfig | undefined,
) => {
const rules: Record<string, (string | false | undefined)[]> = {
// Default empty rules (allow `'self'`)
"default-src": [],
// Default to disallowed rules
"frame-ancestors": [
"none",
],
// Custom rules
...customRules,
"script-src": [
// Required by Next.js without nonces (requires dynamic mode)
"unsafe-inline",
// Required by Next.js hot reload
process.env.NODE_ENV === "development" && "unsafe-eval",
// Vercel preview comments
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.live",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.com",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "unsafe-inline",
// Vercel Analytics (dev mode)
crossOriginResources?.vercelAnalytics && process.env.NODE_ENV === "development" && "https://va.vercel-scripts.com",
...customRules?.["script-src"] ?? [],
],
"style-src": [
// Required by Next.js
"unsafe-inline",
// Vercel preview comments
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.live",
...customRules?.["style-src"] ?? [],
],
"font-src": [
// Vercel preview comments
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.live",
...customRules?.["font-src"] ?? [],
],
"connect-src": [
// Vercel preview comments
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.live",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.com",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://*.pusher.com",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "wss://*.pusher.com",
...customRules?.["connect-src"] ?? [],
],
"frame-src": [
// Vercel preview comments
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.live",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.com",
...customRules?.["frame-src"] ?? [],
],
"img-src": [
// Required by Next.js `<Image>` placeholders
"data:",
// Required by file upload previews
"blob:",
// Vercel preview comments
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.live",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://vercel.com",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "https://*.pusher.com",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "data:",
crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production" && "blob:",
...customRules?.["img-src"] ?? [],
],
};
const directives: string[] = [];
for (const [key, rule] of Object.entries(rules)) {
// Include self as a trusted origin for every directive without `'none'`
if (!rule.includes("none")) {
rule.unshift("self");
}
const origins = (
Array.from(new Set(rule))
.filter(isTruthy)
.map(origin => origin.includes(":") ? origin : `'${origin}'`)
);
directives.push(`${key} ${origins.join(" ")}`);
}
return {
key: "Content-Security-Policy",
value: directives.join("; "),
};
};
const crossOriginResources = {
vercel: true,
vercelAnalytics: false,
};
const withSerwist = withSerwistInit({
swSrc: "app/service-worker.ts",
swDest: "public/service-worker.js",
swUrl: "/service-worker.js",
// Don't reload the page when back online to prevent losing work
reloadOnOnline: false,
});
const nextConfig: NextConfig = {
reactStrictMode: true,
poweredByHeader: false,
// eslint-disable-next-line @typescript-eslint/require-await
async headers () {
return [
{
source: "/(.*)",
headers: [
createContentSecurityPolicy(crossOriginResources, {}),
createPermissionsPolicy(crossOriginResources, {}),
{
key: "X-XSS-Protection",
value: "1; mode=block",
},
{
key: "X-Content-Type-Options",
value: "nosniff",
},
{
key: "X-Frame-Options",
value: "DENY",
},
{
key: "Cross-Origin-Resource-Policy",
value: "same-origin",
},
{
key: "Cross-Origin-Opener-Policy",
// Vercel authentication for comments in preview environments requires a cross origin popup
value: crossOriginResources?.vercel && process.env.VERCEL_ENV !== "production"
? "same-origin-allow-popups"
: "same-origin",
},
{
key: "Strict-Transport-Security",
value: "max-age=31536000; includeSubDomains; preload",
},
{
key: "Referrer-Policy",
value: "strict-origin-when-cross-origin",
},
],
},
{
source: "/service-worker.js",
headers: [
{
key: "Content-Type",
value: "application/javascript; charset=utf-8",
},
{
key: "Cache-Control",
value: "no-cache, no-store, must-revalidate",
},
],
},
];
},
webpack: (config: {module: {rules: unknown[]}}) => {
config.module.rules.push({
test: /\.worker\.js$/,
exclude: /node_modules/,
loader: "worker-loader",
options: {
filename: "static/[chunkhash]-[name].js",
publicPath: "/_next/",
},
});
return config;
},
};
export default withSerwist(nextConfig);