Skip to content

Commit 931d99e

Browse files
committed
feat: handle trusted types in pages dir
1 parent 80d141c commit 931d99e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/next-pagesdir.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export function createNextDsfrIntegrationApi(
182182
/>
183183
</>
184184
)}
185-
{isProduction && (
185+
{isProduction && !isBrowser && (
186186
<script
187187
nonce={nonce}
188188
dangerouslySetInnerHTML={{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
3+
export function middleware(request: NextRequest) {
4+
const cspHeader = `
5+
require-trusted-types-for 'script';
6+
trusted-types react-dsfr react-dsfr-asap nextjs nextjs#bundler;
7+
`
8+
9+
const requestHeaders = new Headers()
10+
requestHeaders.set(
11+
'Content-Security-Policy',
12+
// Replace newline characters and spaces
13+
cspHeader.replace(/\s{2,}/g, ' ').trim()
14+
)
15+
16+
return NextResponse.next({
17+
headers: requestHeaders,
18+
request: {
19+
headers: requestHeaders,
20+
},
21+
})
22+
}
23+
24+
export const config = {
25+
matcher: [
26+
/*
27+
* Match all request paths except for the ones starting with:
28+
* - api (API routes)
29+
* - _next/static (static files)
30+
* - _next/image (image optimization files)
31+
* - favicon.ico (favicon file)
32+
*/
33+
{
34+
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
35+
missing: [
36+
{ type: 'header', key: 'next-router-prefetch' },
37+
{ type: 'header', key: 'purpose', value: 'prefetch' },
38+
],
39+
},
40+
],
41+
}

0 commit comments

Comments
 (0)