Skip to content

Commit 151bf4c

Browse files
Attempt to fix dev console errors
1 parent 0542246 commit 151bf4c

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/SEBT.Portal.Web/src/app/layout.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import { headers } from 'next/headers'
1515
import './globals.css'
1616
import './styles.scss'
1717

18+
// CSP nonces are generated per-request in `src/proxy.ts` (via `x-nonce`).
19+
// Force dynamic rendering so Next can correctly attach the nonce to
20+
// inline styles/scripts generated during rendering (e.g. `next/font`).
21+
export const dynamic = 'force-dynamic'
22+
1823
const state = getState()
1924
const stateName = getStateName(state)
2025

src/SEBT.Portal.Web/src/proxy.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ import { NextRequest, NextResponse } from 'next/server'
1111
*/
1212
export function proxy(request: NextRequest) {
1313
const nonce = Buffer.from(crypto.randomUUID()).toString('base64')
14-
const isDev = process.env.NODE_ENV === 'development'
14+
// Some Next.js runtimes (or local dev setups) may not provide NODE_ENV reliably.
15+
// Treat anything that's not explicitly production as "dev-like" so local debugging
16+
// doesn't break CSP for inline styles.
17+
const isProduction = process.env.NODE_ENV === 'production'
18+
const isDevLike = !isProduction
1519
const proto =
1620
request.headers.get('x-forwarded-proto') ?? request.nextUrl.protocol.replace(':', '')
1721
const isHttps = proto === 'https'
1822
// Only add upgrade-insecure-requests when actually served over HTTPS.
19-
const upgradeInsecure = !isDev && isHttps ? 'upgrade-insecure-requests;' : ''
23+
const upgradeInsecure = !isDevLike && isHttps ? 'upgrade-insecure-requests;' : ''
2024

2125
// Build CSP header with nonce for script and style sources
2226
// Development: Allow unsafe-eval for Next.js hot reload, unsafe-inline for styles (no nonce for styles)
@@ -26,11 +30,15 @@ export function proxy(request: NextRequest) {
2630
// In dev, we skip style nonce to allow HMR style injection.
2731
const cspHeader = `
2832
default-src 'self';
29-
script-src 'self' 'nonce-${nonce}' 'strict-dynamic' https://www.googletagmanager.com ${isDev ? "'unsafe-eval'" : ''};
30-
style-src 'self' ${isDev ? "'unsafe-inline'" : `'nonce-${nonce}'`} https://fonts.googleapis.com;
33+
script-src 'self' 'nonce-${nonce}' 'strict-dynamic' https://www.googletagmanager.com ${
34+
isDevLike ? "'unsafe-eval'" : ''
35+
};
36+
style-src 'self' ${isDevLike ? "'unsafe-inline'" : `'nonce-${nonce}'`} https://fonts.googleapis.com;
3137
font-src 'self' https://fonts.gstatic.com;
3238
img-src 'self' data: https: https://www.google-analytics.com;
33-
connect-src 'self' https://www.google-analytics.com https://*.google-analytics.com https://www.googletagmanager.com https://auth.pingone.com ${isDev ? 'ws://localhost:* http://localhost:*' : ''};
39+
connect-src 'self' https://www.google-analytics.com https://*.google-analytics.com https://www.googletagmanager.com https://auth.pingone.com ${
40+
isDevLike ? 'ws://localhost:* http://localhost:*' : ''
41+
};
3442
frame-src 'none';
3543
child-src 'none';
3644
worker-src 'self';

0 commit comments

Comments
 (0)