Files
parsaaghayi-front/next.config.mjs
T
root 80b3be48ea Stop next/image from 400-ing every SVG in the site
The logo, the burger button and the social icons are SVGs, and the image
optimizer refuses SVG by default, so each of those requests answered 400 and
the images silently never appeared.

Allow SVG on the optimiser together with the restrictive CSP the image
endpoint then requires (script-src 'none'; sandbox), so an SVG served from
_next/image cannot carry script. Verified: all three paths now return 200
with that CSP on the response.
2026-10-01 17:15:12 -04:00

128 lines
4.1 KiB
JavaScript

import withBundleAnalyzer from '@next/bundle-analyzer';
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
reactStrictMode: false,
poweredByHeader: false,
turbopack: {},
images: {
// The logo, the burger button and the social icons are SVGs. Without this
// flag next/image refuses to optimise them and answers 400, so every SVG
// in the site silently fails to load. Serving them from the optimiser needs
// a restrictive CSP on the image endpoint, because an SVG can carry script.
dangerouslyAllowSVG: true,
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
remotePatterns: [
{
protocol: 'https',
hostname: 'via.placeholder.com',
port: '',
pathname: '/**',
},
{
protocol: 'https',
hostname: 'backend.parsaaghayi.ir',
port: '',
pathname: '/**',
},
{
protocol: 'https',
hostname: 'parsaaghayi.ir',
port: '',
pathname: '/**',
},
],
},
async rewrites() {
return [
{
source: '/api/website/:path*',
destination: `${process.env.NEXT_PUBLIC_BACK_URL}/api/website/:path*`,
},
{
source: '/api/blog/:path*',
destination: `${process.env.NEXT_PUBLIC_BACK_URL}/api/blog/:path*`,
},
];
},
async headers() {
// `upgrade-insecure-requests` is a CSP directive, not a header, so it lives inside
// the policy value below. `connect-src` allows the production backend only; the dev
// API runs on plain http://localhost:8000 (see .env.development), so that origin is
// appended outside of production to keep `next dev` working.
const connectSrc = ["'self'", 'https://backend.parsaaghayi.ir'];
if (process.env.NODE_ENV === 'development') {
connectSrc.push('http://localhost:8000', 'http://127.0.0.1:8000');
}
// TODO follow-up: ship a nonce-based policy (`script-src 'nonce-<random>'` plus
// `'strict-dynamic'`). `'unsafe-inline'` is still required for scripts because
// Next.js emits inline bootstrap scripts (hydration payload, next-themes script)
// and framer-motion/next-themes are not wired up to receive a nonce yet. The nonce
// can be threaded through the root layout + middleware, which is what lets us drop
// `'unsafe-inline'` from script-src.
const contentSecurityPolicy = [
"default-src 'self'",
"script-src 'self' 'unsafe-inline'",
"style-src 'self' 'unsafe-inline'",
"img-src 'self' data: blob: https:",
"font-src 'self' data:",
`connect-src ${connectSrc.join(' ')}`,
"media-src 'self' blob:",
"object-src 'none'",
"base-uri 'self'",
"frame-ancestors 'none'",
"form-action 'self'",
'upgrade-insecure-requests',
].join('; ');
return [
{
source: '/:path*',
headers: [
{
key: 'Strict-Transport-Security',
value: 'max-age=63072000; includeSubDomains; preload',
},
{ key: 'X-Content-Type-Options', value: 'nosniff' },
{ key: 'X-Frame-Options', value: 'DENY' },
{ key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
{
key: 'Permissions-Policy',
value: 'camera=(), microphone=(), geolocation=(), interest-cohort=()',
},
{ key: 'Content-Security-Policy', value: contentSecurityPolicy },
],
},
];
},
webpack(config, { isServer }) {
if (!isServer) {
config.optimization.splitChunks = {
chunks: 'all',
minSize: 30000,
maxAsyncRequests: 30,
maxInitialRequests: 30,
automaticNameDelimiter: '-',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
};
}
return config;
},
};
const withBundle = withBundleAnalyzer({
enabled: process.env.ANALYZE === 'true',
// می‌توانید تنظیمات اضافی پلاگین را اینجا اضافه کنید
})(nextConfig);
export default withBundle;