-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathnext.config.ts
More file actions
106 lines (91 loc) · 2.56 KB
/
Copy pathnext.config.ts
File metadata and controls
106 lines (91 loc) · 2.56 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import bundleAnalyzer from '@next/bundle-analyzer';
import type { NextConfig } from 'next';
import createNextIntlPlugin from 'next-intl/plugin';
import { writeBuildConfig } from './build-config/writer';
import { client } from './client';
import { graphql } from './client/graphql';
import { cspHeader } from './lib/content-security-policy';
const withNextIntl = createNextIntlPlugin({
experimental: {
createMessagesDeclaration: './messages/en.json',
},
});
const SettingsQuery = graphql(`
query SettingsQuery {
site {
settings {
url {
vanityUrl
cdnUrl
checkoutUrl
}
locales {
code
isDefault
path
}
}
}
}
`);
async function writeSettingsToBuildConfig() {
const { data } = await client.fetch({ document: SettingsQuery });
const cdnEnvHostnames = process.env.NEXT_PUBLIC_BIGCOMMERCE_CDN_HOSTNAME;
const cdnUrls = (
cdnEnvHostnames
? cdnEnvHostnames.split(',').map((s) => s.trim())
: [data.site.settings?.url.cdnUrl]
).filter((url): url is string => !!url);
if (!cdnUrls.length) {
throw new Error(
'No CDN URLs found. Please ensure that NEXT_PUBLIC_BIGCOMMERCE_CDN_HOSTNAME is set correctly.',
);
}
return await writeBuildConfig({
locales: data.site.settings?.locales,
urls: {
...data.site.settings?.url,
cdnUrls,
},
});
}
export default async (): Promise<NextConfig> => {
const settings = await writeSettingsToBuildConfig();
let nextConfig: NextConfig = {
reactStrictMode: true,
experimental: {
optimizePackageImports: ['@icons-pack/react-simple-icons'],
},
typescript: {
ignoreBuildErrors: !!process.env.CI,
},
// default URL generation in BigCommerce uses trailing slash
trailingSlash: process.env.TRAILING_SLASH !== 'false',
// eslint-disable-next-line @typescript-eslint/require-await
async headers() {
const cdnLinks = settings.urls.cdnUrls.map((url) => ({
key: 'Link',
value: `<https://${url}>; rel=preconnect`,
}));
return [
{
source: '/(.*)',
headers: [
{
key: 'Content-Security-Policy',
value: cspHeader.replace(/\n/g, ''),
},
...cdnLinks,
],
},
];
},
};
// Apply withNextIntl to the config
nextConfig = withNextIntl(nextConfig);
if (process.env.ANALYZE === 'true') {
const withBundleAnalyzer = bundleAnalyzer();
nextConfig = withBundleAnalyzer(nextConfig);
}
return nextConfig;
};