umami/next.config.js

180 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2023-02-15 18:27:18 +08:00
/* eslint-disable @typescript-eslint/no-var-requires */
2020-07-17 16:03:38 +08:00
require('dotenv').config();
2023-08-22 18:36:49 +08:00
const path = require('path');
const pkg = require('./package.json');
2020-07-17 16:03:38 +08:00
const basePath = process.env.BASE_PATH || '';
const forceSSL = process.env.FORCE_SSL || '';
const collectApiEndpoint = process.env.COLLECT_API_ENDPOINT || '';
const defaultLocale = process.env.DEFAULT_LOCALE || '';
const trackerScriptName = process.env.TRACKER_SCRIPT_NAME || '';
const cloudMode = process.env.CLOUD_MODE || '';
const cloudUrl = process.env.CLOUD_URL || '';
const frameAncestors = process.env.ALLOWED_FRAME_URLS || '';
const disableLogin = process.env.DISABLE_LOGIN || '';
const disableUI = process.env.DISABLE_UI || '';
const hostURL = process.env.HOST_URL || '';
const privateMode = process.env.PRIVATE_MODE || '';
2023-11-12 12:45:09 +08:00
const contentSecurityPolicy = [
`default-src 'self'`,
`img-src *`,
`script-src 'self' 'unsafe-eval' 'unsafe-inline'`,
`style-src 'self' 'unsafe-inline'`,
2023-12-21 01:31:33 +08:00
`connect-src 'self' api.umami.is cloud.umami.is`,
`frame-ancestors 'self' ${frameAncestors}`,
2023-11-12 12:45:09 +08:00
];
2022-08-01 14:29:47 +08:00
const headers = [
{
key: 'X-DNS-Prefetch-Control',
value: 'on',
},
2023-11-14 06:12:05 +08:00
{
2023-12-01 15:40:58 +08:00
key: 'Content-Security-Policy',
value: contentSecurityPolicy
.join(';')
.replace(/\s{2,}/g, ' ')
.trim(),
2022-08-01 14:29:47 +08:00
},
2023-11-14 06:12:05 +08:00
];
2022-08-01 14:29:47 +08:00
if (forceSSL) {
2022-08-01 14:29:47 +08:00
headers.push({
key: 'Strict-Transport-Security',
value: 'max-age=63072000; includeSubDomains; preload',
});
}
const rewrites = [];
if (collectApiEndpoint) {
rewrites.push({
source: collectApiEndpoint,
2023-03-09 08:48:44 +08:00
destination: '/api/send',
});
}
if (trackerScriptName) {
const names = trackerScriptName?.split(',').map(name => name.trim());
2023-04-19 23:49:16 +08:00
2023-04-20 16:12:43 +08:00
if (names) {
names.forEach(name => {
rewrites.push({
2023-04-20 16:52:49 +08:00
source: `/${name.replace(/^\/+/, '')}`,
2023-04-20 16:12:43 +08:00
destination: '/script.js',
});
2023-04-19 23:49:16 +08:00
});
}
}
2023-04-22 06:14:30 +08:00
const redirects = [
{
source: '/settings',
2024-02-15 14:13:13 +08:00
destination: '/settings/websites',
permanent: true,
},
{
source: '/teams/:id',
2024-02-03 13:06:55 +08:00
destination: '/teams/:id/dashboard',
2023-04-22 06:14:30 +08:00
permanent: true,
},
2024-02-03 09:49:17 +08:00
{
source: '/teams/:id/settings',
2024-02-15 14:13:13 +08:00
destination: '/teams/:id/settings/team',
2024-02-03 09:49:17 +08:00
permanent: true,
},
2023-04-22 06:14:30 +08:00
];
2024-02-15 14:13:13 +08:00
if (cloudMode && cloudUrl) {
redirects.push({
2024-02-15 14:13:13 +08:00
source: '/settings/:path*',
destination: `${cloudUrl}/settings/:path*`,
permanent: false,
});
2024-02-15 14:13:13 +08:00
redirects.push({
source: '/teams/:id/settings/:path*',
destination: `${cloudUrl}/teams/:id/settings/:path*`,
permanent: false,
});
if (disableLogin) {
redirects.push({
source: '/login',
destination: cloudUrl,
permanent: false,
});
}
}
2023-09-29 20:29:22 +08:00
/** @type {import('next').NextConfig} */
const config = {
2023-09-30 10:18:44 +08:00
reactStrictMode: false,
env: {
basePath,
cloudMode,
cloudUrl,
2023-08-28 10:56:44 +08:00
configUrl: '/config',
2022-06-24 16:54:55 +08:00
currentVersion: pkg.version,
defaultLocale,
disableLogin,
disableUI,
hostURL,
privateMode,
2020-09-27 11:46:20 +08:00
},
2023-09-29 20:29:22 +08:00
basePath,
2022-07-08 00:24:32 +08:00
output: 'standalone',
2021-11-23 06:53:36 +08:00
eslint: {
ignoreDuringBuilds: true,
},
typescript: {
ignoreBuildErrors: true,
},
2020-07-17 16:03:38 +08:00
webpack(config) {
2023-09-29 20:29:22 +08:00
const fileLoaderRule = config.module.rules.find(rule => rule.test?.test?.('.svg'));
config.module.rules.push(
{
...fileLoaderRule,
test: /\.svg$/i,
resourceQuery: /url/,
},
{
test: /\.svg$/i,
issuer: fileLoaderRule.issuer,
resourceQuery: { not: [...fileLoaderRule.resourceQuery.not, /url/] },
use: ['@svgr/webpack'],
},
);
fileLoaderRule.exclude = /\.svg$/i;
2020-07-17 16:03:38 +08:00
2023-08-22 18:36:49 +08:00
config.resolve.alias['public'] = path.resolve('./public');
2020-07-17 16:03:38 +08:00
return config;
},
async headers() {
return [
2022-08-01 14:29:47 +08:00
{
source: '/:path*',
2023-11-14 06:12:05 +08:00
headers,
2023-11-12 12:45:09 +08:00
},
2022-08-02 15:24:17 +08:00
];
},
async rewrites() {
return [
...rewrites,
{
2022-08-02 15:24:17 +08:00
source: '/telemetry.js',
destination: '/api/scripts/telemetry',
},
2020-12-04 14:28:05 +08:00
];
},
async redirects() {
return [...redirects];
},
2020-07-17 16:03:38 +08:00
};
module.exports = config;